关于重复符号的奇怪ld错误

时间:2022-07-16 09:44:10

I have a project consisting of two files, main.c and logoff.c. When I try to compile them I get this error:

我有一个由两个文件main.c和logoff.c组成的项目。当我尝试编译它们时,我收到此错误:

gcc -c -g -Wall main.c
gcc -c -g -Wall logoff.c
gcc -o main -g -Wall main.o logoff.o
ld: duplicate symbol _logoff in logoff.o and main.o

I have a function named logoff in logoff.c, but I have searched main.c for the text "logoff" and have found nothing (the function is not even called yet!).

我在logoff.c中有一个名为logoff的函数,但是我在main.c中搜索了文本“logoff”并且什么都没找到(函数甚至还没有调用!)。

If I change the name of the function to log_off it works. There is a second function in the file which then causes the same error unless its name is changed as well.

如果我将函数的名称更改为log_off则可以。文件中有第二个函数,除非其名称也被更改,否则会导致相同的错误。

Is there any reason why this might occur? I have this problem on two different systems. One thing that might be relevant is that I used the same logoff.c file in another project but it is not connected to this one.

有什么理由可能会发生这种情况吗?我在两个不同的系统上遇到这个问题。可能相关的一件事是我在另一个项目中使用了相同的logoff.c文件,但它没有连接到这个文件。

2 个解决方案

#1


2  

Create a logoff.h file with only the function declaration of logoff, like

创建一个logoff.h文件,只包含logoff的函数声明,如

void logoff(void);

Then, in main.c include it with #include "logoff.h". Don't include logoff.c, as the compiler will compile the function two times, and the linker will see two functions of that name then.

然后,在main.c中包含#include“logoff.h”。不包括logoff.c,因为编译器将编译该函数两次,然后链接器将看到该名称的两个函数。

It appears to work if you change it to log_off, and then only recompile one of them. The other object-file will still have the old logoff function compiled in. Thus the linker sees one log_off and one logoff. That's the reason it appeared to work for you with that name.

如果您将其更改为log_off,然后只重新编译其中一个,它似乎有效。另一个目标文件仍将编译旧的注销函数。因此链接器会看到一个log_off和一个注销。这就是它似乎以这个名字为你工作的原因。

#2


2  

Do you #include or #import logoff.c in main.c?

你在main.c中#include或#import logoff.c吗?

You did - well there's your problem. logoff.c is being included in main.c, so main defines _logoff and _main. Now you also compile logoff.c, which defines _logoff. Then you try and link the two, which means the resulting binary includes the symbols _main, _logoff and _logoff, which is exactly what the linker is telling you about.

你做了 - 那就是你的问题。 logoff.c包含在main.c中,因此main定义了_logoff和_main。现在你还编译了logoff.c,它定义了_logoff。然后你尝试链接这两个,这意味着生成的二进制文件包含符号_main,_logoff和_logoff,这正是链接器告诉你的。

#1


2  

Create a logoff.h file with only the function declaration of logoff, like

创建一个logoff.h文件,只包含logoff的函数声明,如

void logoff(void);

Then, in main.c include it with #include "logoff.h". Don't include logoff.c, as the compiler will compile the function two times, and the linker will see two functions of that name then.

然后,在main.c中包含#include“logoff.h”。不包括logoff.c,因为编译器将编译该函数两次,然后链接器将看到该名称的两个函数。

It appears to work if you change it to log_off, and then only recompile one of them. The other object-file will still have the old logoff function compiled in. Thus the linker sees one log_off and one logoff. That's the reason it appeared to work for you with that name.

如果您将其更改为log_off,然后只重新编译其中一个,它似乎有效。另一个目标文件仍将编译旧的注销函数。因此链接器会看到一个log_off和一个注销。这就是它似乎以这个名字为你工作的原因。

#2


2  

Do you #include or #import logoff.c in main.c?

你在main.c中#include或#import logoff.c吗?

You did - well there's your problem. logoff.c is being included in main.c, so main defines _logoff and _main. Now you also compile logoff.c, which defines _logoff. Then you try and link the two, which means the resulting binary includes the symbols _main, _logoff and _logoff, which is exactly what the linker is telling you about.

你做了 - 那就是你的问题。 logoff.c包含在main.c中,因此main定义了_logoff和_main。现在你还编译了logoff.c,它定义了_logoff。然后你尝试链接这两个,这意味着生成的二进制文件包含符号_main,_logoff和_logoff,这正是链接器告诉你的。