简体   繁体   English

链接器未在目标文件中找到现有引用

[英]Linker not finding existing reference in object file

I am getting trouble compiling a project, basically linking the objects. 我在编译项目时遇到了麻烦,基本上是在链接对象。 The linker complains: 链接器抱怨:

$ g++ -o bin/program obj/linux64/Debug/src/main.o  [... more object files ...] ../../../addons/obj/linux64/Debug/ofxLua/libs/lua/lua/lapi.o [...many other object files and libraries...]
../../../addons/obj/linux64/Debug/ofxLua/libs/luabind/src/scope.o: In function `~lua_pop_stack':
../../../addons/ofxLua/libs/luabind/src/scope.cpp:122: undefined reference to `lua_settop(lua_State*, int)'
[...many more undefined reference errors...]

But if I inspect the object file I find the symbol: 但是,如果我检查目标文件,我会找到符号:

$ nm ../../../addons/obj/linux64/Debug/ofxLua/libs/lua/lua/lapi.o | grep lua_settop
000000000000045c T lua_settop

I checked that g++ arguments are correctly ordered . 我检查了g ++参数是否正确排序

What can be the cause of this behavior? 造成这种现象的原因是什么? The only explanation I can think of is that somehow the headers used from different source files are altered somehow by previous includes or defines. 我能想到的唯一解释是,不同源文件中使用的标头通过先前的include或define有所更改。 Is there any way I can check the resulting complete symbol so I can be sure that's the case? 有什么方法可以检查生成的完整符号,以便可以确定吗?

If you are including the header for code written in C you will need to enclose the #include statement within extern "C" { ... } so that the C++ compiler knows to use C-linkage for those symbols, rather than C++-linkage (which uses name-mangling to allow method overloading). 如果您包含用C编写的代码的标头,则需要将#include语句括在extern "C" { ... }以便C ++编译器知道对这些符号使用C-linkage,而不是C ++-linkage (它使用名称处理来允许方法重载)。

For example: 例如:

extern "C" {
#include <file.h>
}

Really the header file itself should do this, in order to avoid you needing to, and I would consider it an error not do so: 确实,头文件本身应该执行此操作,以避免您需要这样做,我认为这样做不是错误:

//
// file.h
//
#ifdef __cplusplus
extern "C" {
#endif

...

#ifdef __cplusplus
}    // extern "C"
#endif

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

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