GCC 编译多个文件

今天写数据结构的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
",P->Element);
 16             P=P->Next;
 17         }
 18         printf("Not Empty!
");
 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文件在同一文件夹下,否则要指定路径。

原文地址:https://www.cnblogs.com/haxianhe/p/9271202.html