Linux 对函数的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6423340/
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
Undefined Reference to a function
提问by Rayne
I'm using Linux and I have the following files:
我正在使用 Linux,我有以下文件:
main.c, main.h
fileA.c, fileA.h
fileB.cpp, fileB.h
The function F1()
is declared in fileB.h
and defined in fileB.cpp
. I need to use the function in fileA.c
, and so I declared the function as
该函数F1()
在 中声明fileB.h
和定义fileB.cpp
。我需要在 中使用该函数fileA.c
,因此我将该函数声明为
extern void F1();
in fileA.c
.
在fileA.c
。
However, during compilation, I got the error
但是,在编译过程中,我收到了错误
fileA.c: (.text+0x2b7): undefined reference to `F1'
What is wrong?
怎么了?
Thank you.
谢谢你。
ETA: Thanks to the answers I've received, I now have the following:
ETA:感谢我收到的答案,我现在有以下几点:
In fileA.h, I have
在fileA.h中,我有
#include fileB.h
#include main.h
#ifdef __cplusplus
extern "C"
#endif
void F1();
In fileA.c, I have
在fileA.c中,我有
#include fileA.h
In fileB.h, I have
在fileB.h中,我有
extern "C" void F1();
In fileB.cpp, I have
在 fileB.cpp 中,我有
#include "fileB.h"
extern "C" void F1()
{ }
However, I now have the error
但是,我现在有错误
fileB.h: error: expected identifier or '(' before string constant
采纳答案by Lightness Races in Orbit
If you're really compiling fileA.c
as C, not C++, then you need to make sure that the function has the proper, C-compatible linkage.
如果您真的编译fileA.c
为 C,而不是 C++,那么您需要确保该函数具有正确的、与 C 兼容的链接。
You can do this with a special case of the extern
keyword. Both at declaration and definition:
您可以使用extern
关键字的特殊情况来执行此操作。在声明和定义中:
extern "C" void F1();
extern "C" void F1() {}
Otherwise the C linker will be looking for a function that only really exists with some mangled C++ name, and an unsupported calling convention. :)
否则,C 链接器将寻找一个真正存在的函数,该函数仅具有一些错误的 C++ 名称和不受支持的调用约定。:)
Unfortunately, whilst this is what you have to do in C++, the syntax isn't valid in C. You must make the extern
visible only to the C++ code.
不幸的是,虽然这是您在 C++ 中必须做的事情,但语法在 C 中无效。您必须使extern
仅对 C++ 代码可见。
So, with some preprocessor magic:
因此,使用一些预处理器魔法:
#ifdef __cplusplus
extern "C"
#endif
void F1();
Not entirely pretty, but it's the price you pay for sharing a header between code of two languages.
不完全漂亮,但这是您在两种语言的代码之间共享标头所付出的代价。
回答by Xedecimal
fileA.c needs to also include fileA.h I believe.
我相信 fileA.c 还需要包含 fileA.h 。
回答by mattn
perhaps, use
也许,使用
extern "C" void F1();
回答by Alok Save
To be able to call a c++ function from c source code you neeed to give the appropriate linkage specification
.
为了能够从 c 源代码调用 c++ 函数,您需要提供适当的linkage specification
.
The format for specifying linkage specification is
指定链接规范的格式为
extern "type_of_Linkage" <function_name>
So in your case, you should be using:
所以在你的情况下,你应该使用:
extern "C" void F1();
回答by dmh2000
fileA.c can't include fileB.h (via fileA.h) because the C compiler doesn't know what extern "C" means, so it complains that it sees an identifier before a string. don't try to include fileB.h in fileA.c or fileA.h. its not needed
fileA.c 不能包含 fileB.h(通过 fileA.h),因为 C 编译器不知道 extern "C" 是什么意思,所以它抱怨它在字符串之前看到一个标识符。不要试图在 fileA.c 或 fileA.h 中包含 fileB.h。它不需要