gcc简单用法:
在同一个夹子新建3个文件:main.c hello_fn.c hello_fn.h
/**
* hello_fn.h
*/
void hello(const char *msg);
/**
* hello_fn.c
*/
#include <stdio.h>
void hello(const char *msg)
{
printf("Hello %s!\n", msg);
}
/**
* main.c
*/
#include <stdio.h>
#include <math.h>
#include "hello_fn.h"
int
main ()
{
hello("HuangHao");
printf("sqrt(2.0) = %f\n", sqrt(2.0));
return 0;
}
1.编译简单文件
gcc main.c -o main
2.将hello_fn.c编译为动态库
$ gcc -shared hello_fn.c -o libhello.so
使用:$ gcc main.c -o main ./libhello.so
3.将hello_fn.c编译为静态库
$ gcc -W -Wall hello_fn.c -c
$ ar r libhello.a hello_fn.o
使用:
$ gcc main.c -o main2 ./libhello.a
删除libhello.a, 运行main2.exe, 一切正常。
参考资料:
参考资料1:
http://blog.csdn.net/cheungmine/article/details/7684800
xxx@xxxx/usr/bin $ gcc -v
可以显示默认的gcc版本,其实上述的命令执行的是/usr/bin/gcc这个文件。
一般该文件是一个符号链接文件,链接到你默认的 gcc版本中。
xxx@xxxx/usr/bin $ ls -l gcc*
可以显示默认的版本,
一般为:
lrwxrwxrwx 1 root root 7 11月 27 2012 gcc -> gcc-4.6
-rwxr-xr-x 1 root root 306200 4月 16 2012 gcc-4.6
表示gcc链接到gcc-4.6
使用
xxx@xxxx/usr/bin $ sudo apt-get install gcc-4.4
安装4.4版本的gcc
xxx@xxxx/usr/bin $ sudo rm gcc
删除之前的gcc这个符号链接
xxx@xxxx/usr/bin $ sudo ln -s gcc-4.4 gcc
建立新的符号链接,即gcc -> gcc-4.4
xxx@xxxx/usr/bin $ ls -l gcc*
最后检查是否设置成功
对于g++同样如此。。