c - 使用 gcc -c 生成 .o 文件

标签 c gcc linked-list

所以我不得不创建一些代码来制作一个列表并对其执行各种操作。专门打印它,对其进行排序,然后查看其中是否有值。这样做,运行良好。现在我必须使用这些函数并将它们拆分成单独的文件,然后使用 gcc -c(我不太确定我使用的是否正确)来获取 .o 文件,以及用于测试程序的 .o .然后我必须使用 gcc 将我的 .o 文件链接到一个可执行文件中。提示说它将识别 .o 并了解如何链接它们。

所以这是我的问题: 为什么下面的代码返回错误(以下面定义的方式)? 和 我到底应该在命令行中写入什么来链接这些人?

所以代码如下: (首先是 .h 文件,然后是主 .c 文件)

节点.h

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

打印.h

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

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

初始化.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;
}

排序.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);
    }
}

列表.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 文件中吗?

我还遇到错误,我试图将不适当的参数传递给 init 函数,但事实并非如此。但我可能只是看多了。

提前感谢您的帮助。

编辑:将 main 中的变量从 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 *'

(然后 main 中的每个单独的 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 *' 

(然后还有一个用于第二个打印功能)

最佳答案

在 .h 文件中放置包含障碍。例如,对于 sort.h

#ifndef INCLUDE_SORT_H
#define INCLUDE_SORT_H

// contents of file

#endif


编辑

实际上,我只是更仔细地查看了您的代码。您已经在 .h 文件中定义了函数,这不是应该做的事情。真的,我认为根本没有理由将它分成单独的文件。

所以将它们全部合并到一个文件中并编译:

gcc -o list -Wall list.c

如果您确实需要单独的文件,则将函数放入 C 文件,将结构和原型(prototype)放入 .h 文件(您将其包含在每个 C 文件中)。然后使用类似的东西编译和链接:

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

关于c - 使用 gcc -c 生成 .o 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14803717/

相关文章:

c - 为什么我不能在 printf 中没有 '\n' 的情况下使用 gcc 编译器?

c++ - 如何禁止Qt Creator自动使用特定的包含路径?

c - C语言中在链表末尾插入节点

c++ - MSVC : "use of class template requires template argument list" inside STL containers

c - 为什么对 sendto() 的调用失败了?

c - open(O_CREATE) 权限的八进制字符串到整数

android - Android 上的 TCP FAST OPEN

java - 列表文档中的混淆点

java - 使用链表进行循环。输入不能使用多个进程

c - 赋值使指针来自整数而不进行强制转换