g++和gcc的一些编译参数说明

时间:2022-03-28 09:13:21

for(int var=0; var<5; var++){
    ...
}
var++; //使用for循环中定义的变量
cout << var << endl;
-fno-for-scope后可以通过编译:g++ main.cpp -fno-for-scope

 

int typeof; //与关键字重名
typeof = 1;
cout << typeof << endl;
-fno-gnu-keywords后可以通过编译:g++ main.cpp -fno-gnu-keywords

 

int and, xor; //使用了and, xor等操作符来作为变量名
and = 1;
xor = 2;
cout << "and = " << and << " xor = " << xor << endl;
-fno-operator-names后可以通过编译:g++ main.cpp -fno-operator-names

 

/usr/bin/cc -> /etc/alternatives/cc -> /usr/bin/gcc -> gcc-4.3cc与gcc是同一个程序

 

cc main.c -S编译成汇编文件main.s

 

cc main.s -o main可以直接编译汇编文件为可执行文件

 

cc main.c -o main -v加-v可以查看头文件及库文件的搜索路径及具体的编译参数

 

cc main.c -o main -L. -lfunc
cc main.c -o main ./libfunc.so
可以以二种形式使用动态库

 

g++ main.cpp -o main
gcc main.cpp -o main -lstdc++
使用g++及gcc来编译

以下为对参数--no-whole-archive及--whole-archive的尝试,先构造三个C文件: //a.c
void afunc() { printf("inside a afunc()/n"); }
void samefunc() { printf("in samefunc of a.c/n"); }
  //b.c
void bfunc() { printf("inside a bfunc()/n"); }
void samefunc() { printf("in samefunc of b.c/n"); }   //test.c
extern void afunc();
extern void bfunc();
void testfunc()
{
    afunc();
    bfunc();
}

 

先用以下的命令来生成test.o, a.sa, b.sa文件:
gcc a.c -c -o a.o
ar -q a.o a.sa
gcc b.c -c -o b.o
ar -q b.o b.sa
gcc test.c -c -o test.o

 

gcc -shared -Wl,--no-whole-archive a.sa b.sa -Wl,--no-whole-archive -o m.so上面的命令运行通过, 但生成的文件中是不包含afunc, bfunc, samefunc的,不可用

 

gcc -shared test.o -Wl,--no-whole-archive a.sa b.sa -Wl,--no-whole-archive -o m.so上面的命令运行失败, 错误如下:
b.sa(b.o): In function `samefunc':
b.c:(.text+0x14): multiple definition of `samefunc'
a.sa(a.o):a.c:(.text+0x14): first defined here
collect2: ld returned 1 exit status

 

gcc -shared -Wl,--no-whole-archive a.sa b.sa -Wl,--no-whole-archive test.o -o m.so上面的命令运行通过,与再上面的命令相比,只是调换了test.o在命令行中的位置
但生成的文件中是不包含afunc, bfunc, samefunc的,不可用

 

如果以以下方式修改test.c
//test.c
#include <stdio.h>
#include <stdlib.h>
extern void afunc();
extern void bfunc();
void testfunc()
{
    afunc();
    //bfunc();   <---------------just comment following line
}


gcc -shared test.o -Wl,--no-whole-archive a.sa b.sa -Wl,--no-whole-archive -o m.so
链接成功,生成的m.so也是可用的,原因是: 当test.c中不使用bfunc时,也就不再加载b.sa(虽然b.sa是在命令行中的), 也就不会出现与a.sa中存在二个同名的samefunc函数了

 

当改成使用-Wl,--whole-archive后,只有当a.sa与b.sa中没有同名的函数时以下命令才可通过:
gcc -shared test.o -Wl,--whole-archive a.sa b.sa -Wl,--no-whole-archive -o m.so此m.so可以使用

 

总结:
1)当使用--whole-archive时,其会把所有的--whole-archive之后的.sa中的所有函数全部加入到生成的文件中来,这样的情况下,如果有同名函数,则链接就不会通过;
2)当使用--no-whole-archieve时,则此后的所有文件中的所有函数都不会加到生成的文件中,但下面的第3点情况例外;
3)如果命令行中有.o文件,如test.o, 并且.o文件后面有.sa文件,则会把.o文件中用到的函数的.sa文件加入到生成的结果文件中来(即使.sa前有--no-whole-archieve);所以这和.o及.sa的顺序有关系。

 

注:
--whole-archive, --no-whole-archive是ld的命令,gcc并不认识,所以要加-Wl,换成如下命令也是一样的:
ld -shared test.o --whole-archive a.sa b.sa --no-whole-archive -o m.so

 

ld的其它一些用法:
把多个.o加成.a:
ar -q result.a s1.o s2.o s3.o

在一个.a中去掉一个.o
ar -d result.a s1.o

居然可以在一个.a中加入多个相同的.o
ar -q result.a s1.o s1.o s1.o

 

g++ -v的输出为:
Using built-in specs.
Target: i486-linux-gnu
Configured with:
   ../src/configure -v --with-pkgversion='Ubuntu 4.3.3-5ubuntu4'
   --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
   --enable-languages=c,c++,fortran,objc,obj-c++
   --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib
   --without-included-gettext --enable-threads=posix --enable-nls
   --with-gxx-include-dir=/usr/include/c++/4.3
   --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug
   --enable-objc-gc --enable-mpfr --enable-targets=all
   --with-tune=generic --enable-checking=release --build=i486-linux-gnu
   --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4) 

 

当gcc加-ansi参数编译时,有以下限制:
1)文件中不能以//来加注解