gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:10:59 26 4
gpt4 key购买 nike

我编写了一个链表的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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com