我需要在大学课程中使用C++98,但是即使在将-std=c++98标志传递给clang++或g++时,它似乎仍然可以用c++11编译,并且如果我使用c++11特性,它不会给出错误。下面是一个简单的例子:
#include <string>
#include <sstream>
using namespace std;
int main()
{
int i;
string number = "12";
i = stoi(number);
}我的makefile:
all:
clang++ -std=c++98 -c *.cpp
clang++ -o main *.o
clean:
rm -f *.o main
run: clean all
./main然后,我从终端运行命令make (我尝试使用clang++而不是g++,但它产生相同的结果),并收到以下输出:
➜ 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我相信如果-std=c++98标志在工作,这段代码就不应该被编译。如何使用c++98强制编译代码?
下面是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\以下是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我还尝试添加标志-pedantic,但它不能解决这个问题。
使用标志-stdlib=libc++会产生以下结果:
➜ 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如果我将其更改为仅-stdlib=libc++,则它仍然可以编译:
➜ cppversion make
clang++ -stdlib=libc++ -std=c++98 -c *.cpp
clang++ -o main *.o
➜ cppversion我找到了一个简单的解决方案:使用自制软件安装gcc,然后使用g++-11编译。
发布于 2021-09-07 09:49:54
尝试使用-std=c++98 -pedantic。
这应该严格执行特定的标准。
发布于 2021-09-08 21:13:12
我找到了一个简单的解决方案:使用自制软件安装gcc,然后使用g++-11编译。
https://stackoverflow.com/questions/69085857
复制相似问题