C语言 C 中的“对 varName 的未定义引用”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5892056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:33:59  来源:igfitidea点击:

What does the "undefined reference to varName" in C mean?

creferenceundefined

提问by James Raitsev

I have 2 files: a.cand b.c

我有 2 个文件:a.cb.c

In a.cI am sending a signal to a function located in b.c

a.c我向位于的函数发送信号b.c

signal(SIGUSR1,doSomething);

On top of the a.c file, I have:

在 ac 文件之上,我有:

extern void doSomething (int    sig);

When I compile, however, I get an error:

但是,当我编译时,出现错误:

/tmp/ccCw9Yun.o: In function main':
a.c:(.text+0xba): undefined reference to
doSomething'
collect2: ld returned 1 exit status

/tmp/ccCw9Yun.o: 在函数doSomething' collect2: ld 返回 1 退出状态main':
a.c:(.text+0xba): undefined reference to

The following headers are included:

包括以下标头:

#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

How do I fix this?

我该如何解决?

回答by Jonathan Leffler

You need to link both a.oand b.o:

您需要同时链接a.ob.o

gcc -o program a.c b.c


If you have a main()in each file, you cannot link them together.

如果main()每个文件中都有一个,则无法将它们链接在一起。

However, your a.cfile contains a reference to doSomething()and expects to be linked with a source file that defines doSomething()and does not define any function that is defined in a.c(such as main()).

但是,您的a.c文件包含对doSomething()源文件的引用并希望与该源文件链接,该源文件定义doSomething()和未定义在a.c(例如main())中定义的任何函数。

You cannot call a function in Process B from Process A. You cannot send a signal to a function; you send signals to processes, using the kill()system call.

不能从进程 A 调用进程 B 中的函数。不能向函数发送信号;您使用kill()系统调用向进程发送信号。

The signal()function specifies which function in your current process (program) is going to handle the signal when your process receives the signal.

signal()函数指定当前进程(程序)中的哪个函数将在您的进程收到信号时处理该信号。

You have some serious work to do understanding how this is going to work - how ProgramA is going to know which process ID to send the signal to. The code in b.cis going to need to call signal()with dosomethingas the signal handler. The code in a.cis simply going to send the signal to the other process.

您有一些认真的工作要做,以了解这是如何工作的 - ProgramA 如何知道将信号发送到哪个进程 ID。中的代码b.c将会需要调用signal()dosomething作为信号处理程序。中的代码a.c只是将信号发送到另一个进程。

回答by mattnz

It is very bad style to define external interfaces in .c files. .

在 .c 文件中定义外部接口是一种非常糟糕的风格。.

You should do this

你应该做这个

a.h

    extern void doSomething (int    sig);

a.c

交流电

    void doSomething (int    sig)
    {
       ... do stuff 
    }

b.c

公元前

#include "a.h"
.....
signal(SIGNAL, doSomething); 

.

.

回答by Roadrunner-EX

An initial reaction to this would be to ask and ensure that the two object files are being linked together. This is done at the compile stage by compiling both files at the same time:

对此的最初反应是询问并确保两个目标文件链接在一起。这是在编译阶段通过同时编译两个文件来完成的:

gcc -o programName a.c b.c

Or if you want to compile separately, it would be:

或者如果你想单独编译,那就是:

gcc -c a.c
gcc -c b.c
gcc -o programName a.o b.o

回答by Michael Mrozek

You're getting a linker error, so your extern is working (the compiler compiled a.cwithout a problem), but when it went to link the object files together at the end it couldn't resolve your extern -- void doSomething(int);wasn't actually found anywhere. Did you mess up the extern? Make sure there's actually a doSomethingdefined in b.cthat takes an intand returns void, and make sure you remembered to include b.cin your file list (i.e. you're doing something like gcc a.c b.c, not just gcc a.c)

您收到链接器错误,因此您的 extern 正在工作(编译器编译a.c没有问题),但是当它最终将目标文件链接在一起时,它无法解析您的 extern -void doSomething(int);实际上在任何地方都找不到. 你搞砸了外部吗?确保实际上有一个doSomething定义,b.c它接受 anint并返回void,并确保您记得将其包含b.c在您的文件列表中(即您正在做类似的事情gcc a.c b.c,而不仅仅是gcc a.c

回答by perreal

You need to compile and then link the object files like this:

您需要编译,然后像这样链接目标文件:

gcc -c a.c  
gcc -c b.c  
gcc a.o b.o -o prog  

回答by Ram

make sure your doSomething function is not static.

确保你的 doSomething 函数不是静态的。