繁体   English   中英

不知道为什么我出现链接错误

[英]Don't know why I am getting a linking error

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

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

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

驱动程序

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

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

target.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;
}
......

目标

#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;

如果first不需要外部target.c ,可以从中移除target.h共(可能由statictarget.c如果你想避免将它放在全局命名空间不必要的)。

具有外部链接的对象仅在整个程序中定义一次。

struct target_node * first在头文件中定义了struct target_node * first ,然后将其包含在两个不同的实现文件中。 现在,您在程序中有first两个定义。 first是具有外部链接的对象。 因此,错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM