2013-02-10 565 views
3

所以我不得不创建一些代码来制作一个列表并为它做各种事情。具体打印它,对其进行分类,并查看是否有值。没有,运行良好。现在,我必须将这些函数分成不同的文件,然后使用gcc -c(我不确定我是否正确使用)来获取.o文件,再加上测试程序的.o 。然后我必须使用gcc将我的.o文件链接到一个可执行文件中。提示表示它会识别.o的并意识到如何链接它们。使用gcc -c生成.o文件

所以,这里是我的问题: 为什么下面的代码返回错误(将在下面定义的方式)? 和 究竟是什么我应该写入命令行来链接这些家伙?

因此,代码如下: (第一h文件,则主.c文件)

node.h

typedef struct Node{ 
    int data; 
    struct Node *next; 
    struct Node *prev; 
}node; 

print.h

#include<stdio.h> 
#include"node.h" 
void print(node *pointer){ 
    if (pointer == NULL){ 
     return; 
    } 

    printf("%d ",pointer->data); 
    print(pointer->next); 
} 

init.h

#include<stdio.h> 
#include"node.h" 
int init(node *pointer,int find){ 
    pointer = pointer->next; 

    while (pointer != NULL){ 
     if (pointer->data == find)//found find 
     { 
      printf("The data is in the list."); 
      return 1; 
     } 
     pointer = pointer->next;// Search in the next node. 
    } 

    //find is not found 
    printf("The data is not in the list."); 
    return 0; 
} 

sort.h

#include<stdio.h> 
#include"node.h" 

void swap (node *x, node *y){ 
    int temp = x->data; 
    x->data = y->data; 
    y->data = temp; 
} 

void sort(node*pointer){ 
    int i; 

    while (pointer->next != NULL){ 
     if (pointer->data>pointer->next->data){ 
      swap(pointer,pointer->next); 
     } 

     pointer = pointer->next; 
     sort(pointer); 
    } 
} 

list.c

#include<stdio.h> 
#include<stdlib.h> 
#include"node.h" 
#lnclude"print.h" 
#include"sort.h" 
#include"init.h" 
int i; 
node *p; 
node *n; 

void insert(node *pointer, int data){ 
    //go through list till ya find the last node 
    while (pointer->next != NULL){ 
     pointer = pointer->next; 
    } 

    //allocate memory for new node and put data in it 
    pointer->next = (node *)malloc(sizeof(node)); 
    (pointer->next)->prev = pointer; 
    pointer = pointer->next; 
    pointer->data = data; 
    pointer->next = NULL; 
} 

int main(){ 
    //start is used to point to the first node 
    //temp is for the last node 
    node *start, *temp; 
    int z; 
    start = (node *)malloc(sizeof(node)); 
    temp = new; 
    temp->next = NULL; 
    temp->prev = NULL; 

    for (z = 0; z < 10; z++){ 
     insert(start,(3*10) - z); 
    } 

    init(start,12); 
    init(start,3); 
    init(start,27); 
    init(start,7); 
    print(start); 
    sort(start); 
    print(start); 

}  

现在的代码都跑了完美的罚款当一切都在一起,node.h的异常(即始终是一个单独的文件)。 .h文件将完美编译,但是当我尝试编译.c文件时,它会返回错误,声明我正试图在每个.h文件中重新定义节点。这可能是因为我将它包含在每个.h文件中?

我也遇到错误,我试图将不适当的参数传递给初始化函数,这似乎并不是这种情况。但我可能只是在看东西。

非常感谢您的帮助。

编辑:在从主新改变可变键入“GCC list.c”

In file included from init.h:2:0, 
       from list.c:4: 
node.h:1:16 error: redefinition of'struct Node' 
node.h:1:16 note: originally defined here 
node.h:5:2 error: conflicting types for 'node' 
node.h:5:2 note: previous declaration of 'node' was here 

In file included from sort.h:2:0; 
       from list.c:5: 
node.h:1:16 error: redefinition of'struct Node' 
node.h:1:16 note: originally defined here 
node.h:5:2 error: conflicting types for 'node' 
node.h:5:2 note: previous declaration of 'node' was here 

list.c: In function 'main': 
list.c:41:1: warning: passing argument 1 of 'init' from incompatible pointer type[enabled by default] 
init.h:4:5: note: expected 'struct node *' but argument is of type 'struct node *' 

(再有每个在主单独初始化呼叫的错误)

何时开始 错误
list.c:45:1: warning:passing argument 1 of 'print' from incompatible pointer type [enabled by default] 
print.h:3:6: note expected 'struct node *' but argument is of type 'struct node *' 

(再有另一个用于第二打印功能)

+2

问:我可以使用“gcc -c”创建.o文件(并在稍后链接它们)吗?答:是的。问:*你得到什么特定的错误*? PS:不要使用变量名称“new” - 它很容易与C++关键字混淆。 – paulsm4 2013-02-10 23:12:43

+0

所以我在威廉莫里斯的帮助下解决了我的错误。但我不完全确定要写什么来创建.o文件或将它们链接到可执行文件中。你能详细说明一下吗? – drpogue 2013-02-10 23:33:46

+1

威廉莫里斯以“[卫兵](http://en.wikipedia.org/wiki/Include_guard)”为你提供帮助。这个约定允许你多次使用头文件,而不会出现“重复定义”的错误。你的下一个挑战是熟悉“[make](http://en.wikipedia.org/wiki/Makefile)”。这是一个很好的教程(你可以在网上找到很多教程):http://www.codeproject.com/Articles/31488/Makefiles-in-Linux-An-Overview – paulsm4 2013-02-11 19:07:27

回答

6

在.h文件包含认沽障碍。例如,对于sort.h

#ifndef INCLUDE_SORT_H 
#define INCLUDE_SORT_H 

// contents of file 

#endif 


编辑

其实我只是看着你的代码更加紧密。您已经在.h文件中定义了函数,这不是要做的事情。真的,我没有理由把它分成单独的文件。

所以他们都合并到一个单一的文件,并编译:

gcc -o list -Wall list.c 

如果你真的想单独的文件,然后把功能集成到C文件的结构和原型为.h文件(你包括到每个C文件中)。然后编译并使用类似链接:

gcc -o list -Wall list.c node.c main.c 
+0

我试图做到这一点,但是当我应该her der一下时就会被篡改。我认为它现在工作... – drpogue 2013-02-10 23:27:33