【linux】scau期末复习||Linux C||实验

5 篇文章 0 订阅

step by step.

hello.c

#include <stdio.h>
#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 <stdio.h>
#include “say_hello.h”
void say_hello(char *str)
{
   printf(“%s”,str);
}

hello.c

#include <stdio.h>
#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<stdio.h>
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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值