gpt4 book ai didi

c - 不知道为什么我会收到链接错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:34:30 25 4
gpt4 key购买 nike

我不知道为什么会出现此链接错误,我很确定一切都已正确链接

gcc -Wall -Wextra -o test driver.c target.c 
driver.c:8: warning: unused parameter ‘argc’
ld: duplicate symbol _first in /var/folders/yx/31ddgzsj4k97jzvwhfx7tkz00000gn/T//ccw2n48G.o and /var/folders/yx/31ddgzsj4k97jzvwhfx7tkz00000gn/T//ccKZdUlG.o for architecture x86_64
collect2: ld returned 1 exit status

我有下面的代码,这是一个简单的链表,但我不知道为什么编译不通

驱动.c

 #include "target.h"
#include <stdio.h>
#include <stdlib.h>

char * prog;
int main(int argc, char * argv[]){
prog = argv[0];
print_target_list(1);
.....

目标.c

#include "target.h"
/* This function returns true if there is a target with name in the target linked list */
bool is_target(char * name){
struct target_node * ptr = first;
while(ptr != NULL){
if(strcmp(ptr->name,name) == 0){
return true;
}
ptr = ptr->next;
}
return false;
}
......

目标.h

#ifndef TARGET_H
#define TARGET_H

//#include "source.h"
#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

/*-----------------------------------------*/

extern char * prog;

/*-----------------------------------------*/

struct source_node{
char * name;
};

struct target_node{
char * name;
struct target_node * next;
struct source_node * src_node;
};

struct target_node * first = NULL;

/*-----------------------------------------------------*/


/* return 1 if name is in the target linked list 0 otherwise */
bool is_target(char * name);
/* returns a new target_node */
struct target_node * new_target_node(char * name);
/* insert a new target_node into the list */
void insert_target_node(char * name);
/* remove a target_node from the list */
void remove_target_node(char * name);
/* print the current linked list */
void print_target_list(int opts);


#endif

我们会提供任何帮助

最佳答案

target.h 中使用:

extern struct target_node * first;

并将以下内容放入适当的 target.c 文件中:

struct target_node * first = NULL;

如果 target.c 之外不需要 first,则可以将其从 target.h 中完全删除(并且可能制作 static in target.c 如果你想避免将它不必要地放在全局命名空间中)。

关于c - 不知道为什么我会收到链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10201475/

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