c++ - 应该向 gcc 提供什么编译器参数让它认为它是 g++?

标签 c++ gcc g++

是否可以为 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/

相关文章:

c++ - gcc 4.7 的可选和变体

c++ - g++ unicode 字符 ifstream

c++ - 引用命名空间内的结构类型

c++ - 为什么 std::vector 在离开不同范围时调用析构函数?

gcc - llvm-gcc 和 clang 二进制文件是否与 gcc 兼容? - 特别是 Windows 上的 mingw gcc

c++ - 在 C++ 中的合并排序中传递数组时出错

gcc - 强制相对调试符号路径(maven-native-plugin)

windows - 如何阻止使用 MinGW (g++) 编译的程序在 Windows 中打开控制台窗口

c++ - 预处理器 "invalid integer constant expression"比较 int 和 double

C++ <map> vs <unordered_map> vs <tr1/unordered_map> vs <ext/unordered_map>