I need to use C 98 for university programs, however even when passing the -std=c 98 flag to clang or to g it still seems to compile with c 11 and does not give errors if I use c 11 features. Here is a simple example:
#include <string>
#include <sstream>
using namespace std;
int main()
{
int i;
string number = "12";
i = stoi(number);
}
My makefile:
all:
clang -std=c 98 -c *.cpp
clang -o main *.o
clean:
rm -f *.o main
run: clean all
./main
Then I run the command make from Terminal (I tried using clang instead of g but it yields the same result) and receive the following output:
➜ cppversion make
g -std=c 98 -c *.cpp
g -o main *.o
➜ cppversion make
clang -std=c 98 -c *.cpp
clang -o main *.o
➜ cppversion
I believe this code should not have compiled if the -std=c 98 flag was working. How do I force code to compile with c 98?
Here is the version of clang:
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin\
Here is the version of g :
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include/c /4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I have also tried adding the flag -pedantic but it does not fix the problem.
Using the flag -stdlib=libc yields the following:
➜ cppversion make
clang -stdlib=libstdc -std=c 98 -c *.cpp
clang: warning: include path for libstdc headers not found; pass '-stdlib=libc ' on the command line to use the libc standard library instead [-Wstdlibcxx-not-found]
main.cpp:1:10: fatal error: 'string' file not found
#include <string>
^~~~~~~~
1 error generated.
make: *** [all] Error 1
If I change it to just -stdlib=libc then it still compiles:
➜ cppversion make
clang -stdlib=libc -std=c 98 -c *.cpp
clang -o main *.o
➜ cppversion
I found an easy solution: Use homebrew to install gcc and use g -11 to compile.
CodePudding user response:
Try using -std=c 98 -pedantic
.
This should strictly enforce the specific standard.
CodePudding user response:
Disclaimer: This is partly guesswork since I don't have a Mac
From my understanding, clang
is the default compiler on Mac and I would therefore not be surprised if even g
uses LLVM:s libc
and headers by default. std::stoi
is unconditionaly declared in the libc
headers.
If you instead useg
:s libstdc
toolchain, you will probably get the error you want:
clang -stdlib=libstdc -std=c 98 -o main main.cpp
CodePudding user response:
I found an easy solution: Use homebrew to install gcc and use g -11 to compile.