gcc用法以及在ubuntu安装gcc的方法

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++同样如此。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值