gcc 和 g++
用gcc 编译链接cpp源码
先给出源码及编译链接结果:
me.h:
extern "C" void CppPrintf(void);
me.cpp:
#include
#include "me.h"
using namespace std;
void CppPrintf(void)
{
cout << "Hello\n";
}
test.cpp:
#include "me.h"
int main(void)
{
CppPrintf();
return 0;
}
g++:
g++ test.cpp me.cpp -o test
g++ -g -Wall -std=c++11 test.cpp me.cpp -o test
gcc:
gcc -g -Wall -std=c++11 test.cpp me.cpp -o test -lstdc++
gcc -g -Wall -std=c++11 test.cpp me.cpp -o test -lstdc++ -shared-libgcc
注意,下面的是行不通的(-lstdc++ -shared-libgcc一定要放后面)
gcc -g -Wall -std=c++11 -lstdc++ -shared-libgcc test.cpp me.cpp -o test
说明关系
平时使用gcc/g++,看了网上的一些回答和gcc的关系,感觉很多不太准确,或者缺少例子,就记录一下。stackoverflow上大致意思是,g++
<<=等价于=>> gcc -xc++ -lstdc++ -shared-libgcc
(the 1st is a compiler option, the 2nd two are linker options)
有一哥列出了下面这些区别,
The main differences:
1.gcc will compile: *.c/*.cpp files as C and C++ respectively.
2.g++ will compile: *.c/*.cpp files but they will all be treated as C++ files.
3.Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).
4.gcc compiling C files has fewer predefined macros.
5.gcc compiling *.cpp and g++ compiling *.c/*.cpp files has a few extra macros.
Extra Macros when compiling *.cpp files:
#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern
但我没找到官方的出处。
参考:
https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc
官方文档中提到g++也没明确说明,只在第3.3节说明对C++文件应该使用g++,参考:
https://gcc.gnu.org/onlinedocs/gcc-8.2.0/gcc.pdf