栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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 %dn", 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

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/881228.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号