[GCC]_[初级]_[gcc(g++)相关使用参数技巧]
2014-11-06 13:54
351 查看
1. 最快的编译单个文件:
g++ -o testgcc testgcc.cpptestgcc.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.cppg++ -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.cpp6. 编译动态库 -shared,包含头文件目录,关键参数-I
# .的意思是当前目录,可以用路径代替,如果是Windows平台,路径要使用/,比如/E/software/includeg++ -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.cppar -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
相关文章推荐
- [GCC]_[初级]_[gcc(g++)相关使用参数技巧]
- 调试技巧之 gcc/g++ -instrument-functions 参数
- 调试技巧之 gcc/g++ -instrument-functions 参数
- 调试技巧之 gcc/g++ -instrument-functions 参数【转载】
- 调试技巧之 gcc/g++ -instrument-functions 参数
- libsvm相关变量总结以及libsvm 参数粗调、微调技巧 和PCA主成分分析princomp函数的使用
- Linux系统中使用GCC 对CPU参数优化代码编译
- gcc和g++的基本使用
- gcc g++ 参数
- C# Enum设计和使用的相关技巧
- gcc使用参数详解
- 写批处理必备的一些命令参数使用技巧
- C# Enum设计和使用的相关技巧
- C# Enum设计和使用的相关技巧
- C# Enum设计和使用的相关技巧
- GCC编译器相关参数
- gcc和g++参数
- C# Enum设计和使用的相关技巧
- C# Enum设计和使用的相关技巧
- 使用OpenGL开发时Blend混合的参数选择技巧