GCC 编译

http://blog.csdn.net/ruglcc/article/details/7814546/

可以参考这个PPT:
http://wenku.baidu.com/link?url=zRKmb2VRhOV6d4DmaAB2zc4-dRwjcbCQlPb2i-ilNiadCI0UQ8UiJZcHoWmKZj2RSL8mwarZprkNdCa8tNDOAOI4aDG500FvuT7vaLyzj0q

可以参考鸟哥的Linux私房菜 第668页

单一程序,打印 Hello World!

直接编译

编译程序源码,即源码

vim hello.c

内容如下:

#include <stdio.h>
void main()
{
    printf("Hello World!");
}
开始编译与测试执行

gcc hello.c

查看当前目录下的文件:

ls

此时会产生如下两个文件

a.out hello.c

在默认状态下,如果直接以gcc编译源码,并且没有加上任何参数,这执行文件的文件名会被自动设置为a.out这个文件名。

./a.out #

整体过程如下所示:

hdu@ubuntu:~/XJFile/make$ ls
hello.c
hdu@ubuntu:~/XJFile/make$ gcc hello.c
hdu@ubuntu:~/XJFile/make$ ls
a.out  hello.c
hdu@ubuntu:~/XJFile/make$ ./a.out
Hello World!
hdu@ubuntu:~/XJFile/make$

分步编译

第一步:

gcc -c hello.c

第二步:

gcc -o hello hello.o

hdu@ubuntu:~/XJFile/make$ gcc -c hello.c
hdu@ubuntu:~/XJFile/make$ ls
hello.c  hello.o
hdu@ubuntu:~/XJFile/make$ gcc -o hello hello.o
hdu@ubuntu:~/XJFile/make$ ls
hello  hello.c  hello.o
hdu@ubuntu:~/XJFile/make$ ./hello
Hello World!
hdu@ubuntu:~/XJFile/make$

主程序、子程序链接:子程序编译

编写所需要的主程序、子程序

主程序

#include <stdio.h>
int main()
{
    printf("I am father!\n");
    Son();
}

子程序

#include <stdio.h>
int Son()
{
    printf("I am son!\n");
}

Demo

touch a.h b.h c.h

main.c

#include <stdlib.h>
#include "a.h"

extern void function_two();
extern void function_tree();

int main()
{
    function_two();
    function_three();
}

2.c

#include "a.h"
#include "b.h"

void function_two(){
}

3.c

#include "b.h"
#include "c.h"

void function_three(){
}
myapp: main.o 2.o 3.o
    gcc -o myapp main.o 2.o 3.o
main.o: main.c a.h
    gcc -c main.c
2.o: 2.c a.h b.h
    gcc -c 2.c
3.o: 3.c b.h c.h
    gcc -c 3.c
clean: 
    rm -f main.o 2.o 3.o

编译

gcc -c father.c son.c

链接

gcc -o out father.o son.o

执行

./out

调用外部函数库:加入链接的函数库

gcc sin.c -lm -L/lib -L/usr/bin

-l: 是加入某个函数库(library)的意思;
m:则是libm.so这个函数库。

-L后面接路径,表示需要的libm.so请到/lib或到/usr/lib里面搜索!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值