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

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 x86_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peter(阿斯拉达)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值