c - 如何声明多个 .c 文件(每个文件包含一个函数)和一个 .h 文件以实现链接列表?

标签 c

我编写了一个链表的C代码,并在同一个文件中声明了各种函数,例如插入、删除和遍历。我现在想在单独的 .c 文件中定义这些函数,但我无法找到正确的解决方案。 以下代码来自 LinkedList.h 文件:

    struct node
{
    int data;
    struct node *link;
};
struct node *head; // Global variable

void insert(int x);
void insert2(int x, int n);
void traverse();
void delete(int n);

LinkedList.c 文件:

#include <stdio.h>
#include <stdlib.h>
#inlcude "LinkedList.h"
#inlcude "insert.c"
#inlcude "traverse.c"
#inlcude "insert.c"
#include "insert2.c"

int main(void)

来自 .c 文件之一 `include

#include <stdlib.h>
#include "LinkedList.h"

void insert(int x)
{
    struct node *curr = (struct node *)malloc(sizeof(struct node));
    if(head == NULL)    // for empty list condition
    {

我只包含了 .c 文件代码的初始部分。

编译 LinkedList.c 时出现以下错误

  In file included from delete.c:3:0,
                 from LinkedList.c:14:
LinkedList.h:1:8: error: redefinition of ‘struct node’
 struct node
        ^
In file included from traverse.c:3:0,
                 from LinkedList.c:13:
LinkedList.h:1:8: note: originally defined here
 struct node
        ^
In file included from delete.c:3:0,
                 from LinkedList.c:14:
LinkedList.h:6:14: error: conflicting types for ‘head’
 struct node *head; // Global variable
              ^
In file included from traverse.c:3:0,
                 from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
 struct node *head; // Global variable
              ^
In file included from insert2.c:3:0,
                 from LinkedList.c:15:
LinkedList.h:1:8: error: redefinition of ‘struct node’
 struct node
        ^
In file included from delete.c:3:0,
                 from LinkedList.c:14:
LinkedList.h:1:8: note: originally defined here
 struct node
        ^
In file included from insert2.c:3:0,
                 from LinkedList.c:15:
LinkedList.h:6:14: error: conflicting types for ‘head’
 struct node *head; // Global variable
              ^
In file included from traverse.c:3:0,
                 from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
 struct node *head; // Global variable
              ^
In file included from insert.c:3:0,
                 from LinkedList.c:16:
LinkedList.h:1:8: error: redefinition of ‘struct node’
 struct node
        ^
In file included from insert2.c:3:0,
                 from LinkedList.c:15:
LinkedList.h:1:8: note: originally defined here
 struct node
        ^
In file included from insert.c:3:0,
                 from LinkedList.c:16:
LinkedList.h:6:14: error: conflicting types for ‘head’
 struct node *head; // Global variable
              ^
In file included from traverse.c:3:0,
                 from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
 struct node *head; // Global variable
              ^
In file included from LinkedList.c:17:0:
LinkedList.h:1:8: error: redefinition of ‘struct node’
 struct node
        ^
In file included from insert.c:3:0,
                 from LinkedList.c:16:
LinkedList.h:1:8: note: originally defined here
 struct node
        ^
In file included from LinkedList.c:17:0:
LinkedList.h:6:14: error: conflicting types for ‘head’
 struct node *head; // Global variable
              ^
In file included from traverse.c:3:0,
                 from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
 struct node *head; // Global variable

编译定义了函数的 .c 文件时出现以下错误

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

最佳答案

我相信你应该更好地理解 C 程序是如何构建的:

        #include     compile         link
               +-----+      +-----+
           ,-> | y.c | ---> | y.o |-.
  +-----+ /    +-----+      +-----+  \    +-----+
  | x.h |                             o-> | exe |
  +-----+ \    +-----+      +-----+  /    +-----+
           `-> | w.c | ---> | w.o |-'
               +-----+      +-----+

假设 GCC 编译器:

  • 头文件 (.h) 通过 #include 包含到源文件 (.c) 中。
  • 编译器使用 gcc -c 将源文件 (.c) 编译为目标文件 (.o)
  • 目标文件 (.o) 链接在一起,使用 gcc 将库文件 (.a) 添加到可执行程序中。

您的 LinkedList.c 文件清楚地表明您有不同的想法。

#include 的效果与复制正在包含的文件的内容并将其粘贴到包含它的文件中相同。

如果您再次查看 LinkedList.c 文件,您就会明白错误的原因。在所有 #include 发生之后,您就有了结构和变量的多个定义。

有关缺少 main() 的错误是由于您试图绕过链接步骤并尝试从没有 main() 的源文件创建可执行文件 函数在其中。

尝试重新组织您的构建过程,同时牢记所涉及的不同步骤,您将解决问题。

关于c - 如何声明多个 .c 文件(每个文件包含一个函数)和一个 .h 文件以实现链接列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40819622/

相关文章:

有人能解释一下指针的指针是如何工作的吗?

c++ - c++ 中的 curl_easy_perform 停止用于其他用途的计时器。

c - 使用全局变量同步 p_threads

c - free( ) 时堆损坏

c - 打印标题层次结构

c 在ubuntu中读取文件的最大大小

c++ - 如何在 GCC 中将重复或相乘的字符串文字合并为一个

c - C 中不同文件循环中的 Search_string

c - 通过堆栈溢出重定向指令指针

c++ - 如何检测应用程序是否在 KVM 下运行?