C语言 C编译-“未定义的引用”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19393700/
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
C compiling - "undefined reference to"?
提问by Katie Jurek
I am making a reliable data transfer protocol and have the function prototype
我正在制作一个可靠的数据传输协议并具有功能原型
void tolayer5(int, char data[]);
With the structs
随着 structs
struct msg {
char data[20];
};
struct pkt {
int seqnum;
int acknum;
int checksum;
char payload[20];
};
And when I call the function in this format:
当我以这种格式调用函数时:
tolayer5(A, packet.payload);
Where Ais an intand packet.payloadis a struct pkt, I get the error "undefined reference to 'tolayer5(int, char*)'. Can you help me see what I'm missing here?
Aanint和packet.payloadis a在哪里struct pkt,我收到错误“对 的未定义引用'tolayer5(int, char*)'。你能帮我看看我在这里遗漏了什么吗?
void tolayer5(int AorB, char data[])
{
int i;
if (TRACE>2)
{
printf("TOLAYER5: data received:");
for (i=0; i<20; i++)
printf("%c",data[i]);
printf("\n");
}
}
Thank you all for helping with the original issue! :) When trying to fix that one, however, I ran into an infinite loop that I think has something to do with me addressing characters in an array incorrectly (it's been awhile since I've done Clike this. Can you help me to find where I'm creating an infinite loop?
感谢大家帮助解决原始问题!:) 但是,在尝试修复该问题时,我遇到了一个无限循环,我认为这与我错误地处理数组中的字符有关(我已经有一段时间没有这样做了C。你能帮我找到我在哪里创建一个无限循环?
I have updated the above code to what I'm now working with. Notice the main changes have been to my function:
我已将上述代码更新为我现在正在使用的代码。请注意,我的功能发生了主要变化:
void tolayer5(int AorB, char data[])
And this line inside the function: printf("%c",msgReceived.data[i]);since now it's just:
函数内部的这一行:printf("%c",msgReceived.data[i]);因为现在它只是:
printf("%c",data[i]);
回答by tristan
seems you need to link with the obj file that implements tolayer5()
似乎您需要链接实现 tolayer5() 的 obj 文件
Update: your function declaration doesn't match the implementation:
更新:您的函数声明与实现不匹配:
void tolayer5(int AorB, struct msg msgReceived)
void tolayer5(int, char data[])
So compiler would treat them as two different functions (you are using c++). and it cannot find the implementation for the one you called in main().
因此编译器会将它们视为两个不同的函数(您使用的是 C++)。它找不到您在 main() 中调用的实现的实现。
回答by Phillip Ngan
Make sure your declare the tolayer5 function as a prototype, or define the full function definition, earlier in the file where you use it.
确保将 tolayer5 函数声明为原型,或在使用它的文件的前面定义完整的函数定义。
回答by dsell002
As stated by a few others, this is a linking error. The section of code where this function is being called doesn't know what this function is. It either needs to be declared in a header file an defined in its own source file, or defined or declared in the same source file, above where it's being called.
正如其他一些人所说,这是一个链接错误。调用此函数的代码部分不知道此函数是什么。它要么需要在头文件中声明并在其自己的源文件中定义,要么在同一个源文件中定义或声明,在调用它的上方。
Edit: In older versions of C, C89/C90, function declarations weren't actually required. So, you could just add the definition anywhere in the file in which you're using the function, even after the call and the compiler would infer the declaration. For example,
编辑:在旧版本的 C、C89/C90 中,实际上不需要函数声明。因此,您可以在使用该函数的文件中的任何位置添加定义,即使在调用之后,编译器也会推断出声明。例如,
int main()
{
int a = func();
}
int func()
{
return 1;
}
However, this isn't good practice today and most languages, C++ for example, won't allow it. One way to get away with defining the function in the same source file in which you're using it, is to declare it at the beginning of the file. So, the previous example would look like this instead.
然而,这在今天并不是一个好习惯,大多数语言,例如 C++,都不允许这样做。在使用它的同一源文件中定义函数的一种方法是在文件的开头声明它。所以,前面的例子看起来像这样。
int func();
int main()
{
int a = func();
}
int func()
{
return 1;
}

