您的位置:首页 > 其它

[GCC]_[初级]_[gcc(g++)相关使用参数技巧]

2014-11-06 13:54 351 查看

1. 最快的编译单个文件:

g++ -o testgcc testgcc.cpp

testgcc.cpp

#include <stdio.h>
#include <iostream>
#include "testshare.h"
using namespace std;

int main(int argc, char const *argv[])
{
// int i = 10;
// char* uu = "llll"; //1.let compiler warning.
cout << "name spacestd; main: " << endl;

cout << GetHandler() << endl;

return 0;
}


2. 两步骤,先编译后链接,适合多个文件,关键参数-c.

g++ -c -o testgcc.o testgcc.cpp

g++ -o testgcc testgcc.o

3. 编译Debug文件,关键参数-g. 之后可以gdb testgcc试试.

g++ -g -o testgcc testgcc.cpp

或者

g++ -g -c -o testgcc.o testgcc.cpp

g++ -o testgcc testgcc.o

4. 开编译警告,让编译器做静态分析,提示所有警告,关键参数-Wall.

g++ -g -Wall -o testgcc testgcc.cpp

输出:

$ g++ -g -Wall -o testgcc testgcc.cpp

testgcc.cpp: In function 'int main(int, const char**)':
testgcc.cpp:10:13: warning: deprecated conversion from string constant to 'char*
' [-Wwrite-strings]
char* uu = "llll"; //1.let compiler warning.
^
testgcc.cpp:10:8: warning: unused variable 'uu' [-Wunused-variable]
char* uu = "llll"; //1.let compiler warning.


5. 64位系统下编译32位程序,关键参数-m32. MacOSX可以用-arch i386.(64位-m64或 -arch x84_64)

g++ -g -Wall -m32 -o testgcc testgcc.cpp

6. 编译动态库 -shared,包含头文件目录,关键参数-I

# .的意思是当前目录,可以用路径代替,如果是Windows平台,路径要使用/,比如/E/software/include

g++ -g -Wall -shared -o libtestshare.dll -I. testshare.cpp

6.1 使用动态库

g++ -g -Wall -shared -o libtestshare.dll -I. testshare.cpp #编译动态库

g++ -g -Wall -m32 -o testgcc -I. testgcc.cpp -L. -ltestshare #使用动态库

testshare.h

#ifdef __cplusplus

extern "C"
{

int GetHandler();
int DoProcess(int handler);
}

#endif


testshare.cpp

#include "testshare.h"
#include <iostream>

using namespace std;

int GetHandler()
{
cout << "GetHandler" << endl;
return 10;
}

int DoProcess(int handler)
{
cout << "DoProcess" << endl;
return 0;
}


7. 编译静态库 ar 命令

gcc -c -g -Wall -o testshare.o testshare.cpp

ar -rc libtestshare.a testshare.o

g++ -g -Wall -m32 -o testgcc -I. testgcc.cpp -L. -ltestshare

8. 以-std=c89或c++98方式编译程序,可以直接用-ansi.

-ansi A synonym for -std=c89 (for C) or -std=c++98 (for C++)

gcc -c -ansi -o testgcc.o testgcc.cpp

gcc -c -std=c++98 -o testgcc.o testgcc.cpp

9. 编译文件时输出gcc(g++)的编译预定义宏.

-E Preprocess only; do not compile, assemble or link

-d<letters> Enable dumps from specific passes of the compiler
g++ -E -dM a.cpp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: