使用gcc -c生成.o文件

4
所以我必须编写一些代码,用于创建一个列表并对其执行各种操作。具体地,打印它,排序它,并查看其中是否存在某个值。已完成,并能正常运行。现在我必须将这些函数拆分成单独的文件,然后使用gcc -c(我不确定自己是否使用正确)获取.o文件,以及测试程序的.o文件。然后,我必须使用gcc将我的.o文件链接成可执行文件。提示会识别.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文件中重新定义node。这可能是因为我在每个.h文件中都包含它吗?
此外,我还遇到了错误,声称我正在尝试将不适当的参数传递给init函数,但事实并非如此。但我可能只是忽略了一些东西。
提前感谢您的任何帮助。
编辑:在主函数中将变量从new更改为start。 在输入“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 *'

接着,主函数中的每个 init 调用都会出现错误。

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
所以在William Morris的帮助下,我已经修复了我的错误。但我不太确定该写什么来创建.o文件或将它们链接为可执行文件。您可以详细说明一下吗? - drpogue
1
William Morris帮助您了解了“守卫” 。这个约定允许您多次使用头文件,而不会出现“重复定义”的错误。您接下来的挑战是熟悉“make” 。这里有一个很好的教程(您可以在网络上找到许多类似的教程):http://www.codeproject.com/Articles/31488/Makefiles-in-Linux-An-Overview - paulsm4
@paulsm4:我不同意你关于避免使用new的建议。C和C++是不同的语言,阅读或编写C代码不应该需要了解C++。我个人感觉没有必要避免使用new(一个C++关键字)或者subtract(一个Cobol关键字)来编写C代码。此外,在C代码中使用new作为标识符可以避免将其错误地编译为C++的错误。(当然,如果你确实有编写既可作为C又可作为C++编译的实际需求,那就是另一回事了。) - Keith Thompson
1个回答

7
在.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

我试着去做,但在应该做某事的时候出了错。我认为现在它正在工作... - drpogue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接