今天写数据结构的example,定义了3个文件:lish.h list.c main.c

list.h是list.c的头文件,mian.c中对list.h进行了引用。代码如下:

list.h

  1 #ifndef _List_H
2 #define _List_H
3 typedef int ElementType;
4
5 struct Node;
6 typedef struct Node *PtrToNode;
7 typedef PtrToNode List;
8 typedef PtrToNode Position;
9
10 List MakeEmpty();
11 int IsEmpty( List L );
12 int IsLast( Position P, List L );
13 Position Find( ElementType X,List L );
14 void Delete( ElementType X,List L);
15 Position FindPrevious( ElementType X, List L );
16 void Insert( ElementType X, List L, Position P );
17 void DeleteList( List L );
18 //Position Header( List L);
19 //Position First( List L );
20 //Position Advice( Position P);
21 //ElementType Retrieve( Position P);
22
23 #endif /*_List_H*/
24
25
26 /*Place in the Implementation file */
27 struct Node
28 {
29 ElementType Element;
30 Position Next;
31 };

list.c

    #include "list.h"
2 #include <stdlib.h>
3 /*make a empty list,Head pointer to it's head*/
4 List MakeEmpty()
5 {
6 PtrToNode L;
7 L=(struct Node*)malloc(sizeof(struct Node));
8 L->Next = NULL;
9 return L;
10 }
11 /*Return true if L is empty*/
12 int IsEmpty( List L )
13 {
14 return L->Next==NULL;
15 }
16 /*Return true if P is the last position in list L*/
17 int IsLast( Position P, List L )
18 {
19 return P->Next==NULL;
20 }
21 /*Return Position of X in L;NULL if not found*/
22 Position Find( ElementType X,List L )
23 {
24 Position P;
25 P=L->Next;
26 while(P!=NULL&&P->Element!=X)
27 P=P->Next;
28 return P;
29 }
30 /*Delete first occurrence of X from a list*/
31 /*Assume use of a header node*/
32 void Delete( ElementType X, List L )
33 {
34 Position P, TmpCell;
35
36 P = FindPrevious( X, L );
37
38 if( !IsLast( P, L ))
39 {
40 TmpCell = P->Next;
41 P->Next = TmpCell->Next;
42 free( TmpCell );
}
44 }
45 /* If X is not found, then Next field of returned */
46 /* Position id NULL*/
47 /*Assumes a header */
48 Position FindPrevious( ElementType X, List L )
49 {
50 Position P;
51
52 P=L;
53 while( P->Next != NULL && P->Next->Element != X )
54 P = P->Next;
55 return P;
56 }
57 /* Insert (after legal position P) */
58 /* Header implementtation assumed */
59 /* Parameter L is unused in this implementation */
60 void Insert( ElementType X, List L, Position P )
61 {
62 Position TmpCell;
63
64 TmpCell = malloc( sizeof ( struct Node ) );
65 if( TmpCell == NULL)
66 {
67 //printf( "Out of space!!!" );
68 }
69 TmpCell->Element = X;
70 TmpCell->Next = P->Next;
71 P->Next = TmpCell;
72 }
73 /* Correct DeleteList algorithm*/
74 void DeleteList( List L )
75 {
76 Position P, Tmp;
77
78 P = L->Next;
79 L->Next = NULL;
80 while( P != NULL)
81 {
82 Tmp = P->Next;
83 free(P);
84 P=Tmp;
85 }
86 }

main.c

 1 #include <stdio.h>
2 #include "list.h"
3
4 void main()
5 {
6 List L;
7 L=MakeEmpty();
8 //Insert( 10, L, L);
9 Position P;
10 if( !IsEmpty( L ) )
11 {
12 P = L->Next;
13 while( P != NULL )
14 {
15 printf( "%d\n",P->Element);
16 P=P->Next;
17 }
18 printf("Not Empty!\n");
19 }
20 else
21 {
22 printf("Empty!");
23 }
24 }
25

最后,即是利用gcc来编译这几个文件:

gcc -c list.c

gcc -c main.c

gcc main.o list.o -o main

编译后的目标文件即为:main

然后执行:./main 即可
注:被引用的list.h list.c文件要和main.c文件在同一文件夹下,否则要指定路径。

GCC 编译多个文件的更多相关文章

  1. gcc编译时头文件和库文件搜索路径

    特殊情况:用户自定义的头文件使用#include"mylib"时,gcc编译器会从当前目录查找头文件 一.头文件 gcc 在编译时寻找所需要的头文件 :    ※搜寻会从-I开始( ...

  2. linux gcc 编译时头文件和库文件搜索路径

    一.头文件    gcc 在编译时寻找所需要的头文件 :    ※搜寻会从-I开始    ※然后找gcc的环境变量 C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC_INC ...

  3. 使用当前平台的 gcc 编译内核头文件

    [arm@localhost tchain3.4.4]#cd ${KERNEL} [arm@localhost kernel]#tar xvfz linux­2.6.14.1.tar.gz [arm@ ...

  4. Linux c codeblock的使用(二):在工程中编译多个文件

    (一)前言 我们刚开始学习linux c的时候,一般都是在一个c文件里面写完所有程序,然后用gcc编译这个c文件就好了,十分简单. 但是你有没有想过,如果我们希望将不同模块的代码放到不同的c文件,然后 ...

  5. GCC编译过程与动态链接库和静态链接库

    1. 库的介绍 库是写好的现有的,成熟的,可以复用的代码.现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常. 本质上来说库是一种可执行代码的二进制形式,可 ...

  6. gcc 编译 + 选项【转】

    转自:http://blog.csdn.net/princess9/article/details/6567678 一般来说要现有项目中的编译选项,设置新的project的编译选项 编译器 就是将“高 ...

  7. Linux下多个.c文件的编译和Makefile文件

    在编程的时候,我们可以把一个完整程序的每个函数分离出来,写成.c文件,最后再一起编译和链接.这样有利于程序功能模块化,也方便检查代码错误. .h文件:里面编辑该程序需要引用的头文件. #ifndef  ...

  8. gcc编译问题

    gcc avl.o hash.o list.o rb.o example.o -o 123.exe 多个.o输出 exe -c和-o都是gcc编译器的可选参数.-c表示只编译(compile)源文件但 ...

  9. gcc 编译 c++ 程序(转载)

    单个源文件生成可执行程序 下面是一个保存在文件 helloworld.cpp 中一个简单的 C++ 程序的代码: /* helloworld.cpp */ #include <iostream& ...

随机推荐

  1. qt 心跳设计

    网络通信中的心跳设计是为了判断客户端和服务器通信是socket是否处于连接状态,服务端每隔一个固定的时间间隔给客户端放消息,客户端设计一个心跳类,类中有一个定时器,当socket接收到信息时,心跳类记 ...

  2. Windows10中的IIS10安装php manager

    Windows10中自带的IIS:Microsoft-IIS/10.0. 然后这个10却让原本支持组件无法安装了,php manager组件安装时提示“必须安装IIS7以上才可以安装”. 那是不是真的 ...

  3. VS让人纠结的Release和网站一键发布

    这篇文章不是讲什么知识点,而是开发过程中遇到的问题,一:希望博友看到后知道的给解释一下:二:自己记录一下,下次有时间好好研究一下. 说实话这个问题已经反反复复好几次了,每次都解决不了,都是已另一种方式 ...

  4. 3022Java_运算

    运算 1.运算符分类 算术运算符 二元运算符 +,-,*,/,% 一元运算符 ++,-- 赋值运算符   = 扩展运算符 +=,-=,*=,/= 关系运算符 >,<,>=,<= ...

  5. SpringCloud Sleuth入门介绍

    案例代码:https://github.com/q279583842q/springcloud-e-book 一.Sleuth介绍   为什么要使用微服务跟踪?它解决了什么问题? 1.微服务的现状? ...

  6. Java 泛型学习总结

    前言 Java 5 添加了泛型,提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质是参数化类型,可以为以前处理通用对象的类和方法,指定具体的对象类型.听起来有点抽象, ...

  7. 系统学习 Java IO (十五)----字符读写 Reader/Writer 其他子类

    目录:系统学习 Java IO---- 目录,概览 跟踪行号的缓冲字符输入流 LineNumberReader LineNumberReader 类是一个 BufferedReader ,用于跟踪读取 ...

  8. git的基本指令

    更多详情请看廖雪峰官方网站 http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 1.删 ...

  9. python之内置函数,匿名函数,递归函数

    一. 内置函函数 什么是内置函数?就是Python给你提供的,拿来直接用的函数,比如print,input等等.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就 ...

  10. PHP中var_dump、&&和GLOBALS的爱恨纠缠

    var_dump函数:用来打印显示一个变量的内容与结构: &&:定义一个可变变量.php中,在定义变量时,需要在前面加上一个“&”符号,当加上两个“&&”符号时 ...