What compiler argument shall be provided to gcc for it to think it is g++?
Is it possible to write an argument to gcc such that it w开发者_开发百科ould think its G++?
Why would you want to do this? As far as I know, the only difference is that g++
in addition passes -lstdc++
to the linker, to link the C++ Standard library.
So if you pass that option to GCC, it should forward it to the linker, and all should be fine. But I really don't see a reason in doing this. Just use g++
.
That said, I used gcc
myself in the past, to compile C++ code. When I had a bunch of files, mixed C++ and C code, I just compiled everything using gcc
, because g++
by default treats .c
as being C++ code, which breaks the build when the C code uses such things as new
as identifiers. Adding -lstdc++
always worked nicely.
See the GCC manpage
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.
This documentation states there is a flag -x
to specify the language:
gcc -x c++ ...
'gcc' is 'Gnu Compiler Collection'. If you pass it a C++ file, it invokes the C++ compiler, 'g++'.
Despite this, 'gcc' and 'g++' not interchangeable for c++ files. gcc runs g++ with a few additional arguments that I can't remember off the top of my head.
When you compile c++ files, I would always use g++
精彩评论