是否可以为 gcc 编写一个参数,使其认为它是 G++?
最佳答案
你为什么要这样做?据我所知,唯一的区别是 g++
另外将 -lstdc++
传递给链接器,以链接 C++ 标准库。
因此,如果您将该选项传递给 GCC,它应该将其转发给链接器,一切都应该没问题。但我真的没有看到这样做的理由。只需使用 g++
。
也就是说,我过去使用 gcc
来编译 C++ 代码。当我有一堆混合 C++ 和 C 代码的文件时,我只是使用 gcc
编译所有内容,因为 g++
默认将 .c
视为是 C++ 代码,当 C 代码使用诸如 new
之类的东西作为标识符时,它会破坏构建。添加 -lstdc++
总是很有效。
查看 GCC 联机帮助页
Compiling C++ Programs
C++ source files conventionally use one of the suffixes .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx; C++ header files often use .hh, .hpp, .H, or (for shared template code) .tcc; and preprocessed C++ files use the suffix .ii. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).
However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and treats .c, .h and .i files as C++ source files instead of C source files unless -x is used, and automatically specifies linking against the C++ library. This program is also useful when precompiling a C header file with a .h extension for use in C++ compilations. On many systems, g++ is also installed with the name c++.
When you compile C++ programs, you may specify many of the same command-line options that you use for compiling programs in any language; or command-line options meaningful for C and related languages; or options that are meaningful only for C++ programs.
关于c++ - 应该向 gcc 提供什么编译器参数让它认为它是 g++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5824613/