
step by step.
hello.c#include
#include "say_hello.h"
int main()
{
say_hello("hello world!\n");
}
say_hello.h
#ifndef _SAY_HELLO_H
#define _SAY_HELLO_H
void say_hello(char *str);
#endif
say_hello.c
#include
#include “say_hello.h”
void say_hello(char *str)
{
printf(“%s”,str);
}
hello.c
#include
#include “say_hello.h”
int main()
{
say_hello(“hello world!\n”);
}
生成编译文件:
# gcc –E hello.c –o hello.i --生成预处理文件
# gcc –S hello.c –o hello.s --生成汇编文件
# gcc -c hello.c -o hello.o --生成对应目标文件
# gcc hello.o -o hello
--多文件编译
# gcc hello.c say_hello.c -o hello --进一步编译
# gcc -c say_hello.c -o say_hello.o
# gcc -c hello.c -o hello.o
# gcc hello.o say_hello.o -o hello
[root@localhost ~]# gcc -E hello.c -o hello.i
[root@localhost ~]# gcc -S hello.c -o hello.s
[root@localhost ~]# gcc -c hello.c -o hello.o
[root@localhost ~]# gcc hello.o -o hello
hello.o: In function `main':
hello.c:(.text+0xa): undefined reference to `say_hello'
collect2: error: ld returned 1 exit status
[root@localhost ~]# gcc hello.c say_hello.c -o hello
[root@localhost ~]# gcc -c say_hello.c -o say_hello.o
[root@localhost ~]# gcc hello.o say_hello.o -p hello
结果
-rw-r--r--. 1 root root 91 May 12 01:54 say_hello.c
-rw-r--r--. 1 root root 77 May 12 01:53 say_hello.h
-rw-r--r--. 1 root root 1512 May 12 02:08 say_hello.o
-rwxr-xr-x. 1 root root 8464 May 12 02:08 hello
-rw-r--r--. 1 root root 86 May 12 01:55 hello.c
-rw-r--r--. 1 root root 16930 May 12 02:07 hello.i
-rw-r--r--. 1 root root 1496 May 12 02:07 hello.o
-rw-r--r--. 1 root root 441 May 12 02:07 hello.s
实验7
1. sum.c
进入编辑sum.c
[root@localhost ~]# vim sum.c
sum.c:
#include
int sum(int num)
{
int i;
int sum=0;
for(i=1;i<=num;i++)
{
sum+=i;
}
return sum;
}
int main()
{
int num,result;
result=0;
printf("Input a number:");
scanf("%d",&num);
result=sum(num);
printf("the sum of %d is %d\n", num, result);
return 0;
}
生成文件:
[root@localhost ~]# gcc -E sum.c -o sum.i
[root@localhost ~]# gcc -S sum.c -o.sum.s
[root@localhost ~]# gcc -c sum.c -o sum.o
[root@localhost ~]# gcc sum.c -o sum
检查生成情况:
[root@localhost ~]# ll
-rwxr-xr-x. 1 root root 8480 May 12 02:28 sum
-rw-r--r--. 1 root root 275 May 12 02:28 sum.c
-rw-r--r--. 1 root root 17071 May 12 02:28 sum.i
-rw-r--r--. 1 root root 1888 May 12 02:28 sum.o
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)