使用 gcc 编译的静态库中未解析的符号
我正在尝试使用 gcc 4.2 在 Mac OS X 上编译一些遗留的 C/C++ 代码。最初是使用 Visual Studio 在 Windows 上编写的。我正在编译一个由其他代码链接的静态库,但我遇到了一些难以理解的链接器错误。
当我尝试针对静态库编译一些源代码时,链接器给出以下错误:
Undefined symbols for architecture i386:
"_read_float", referenced from:
_sub_token_values in libcompengine.a(alparce.o)
方法 read_float 在库中,并且正在编译。但是当我使用 nm 转储库中的符号时,我看到在 libcompengine.a(alparce.o) 下,方法 _read_float 显示为未定义:
U _read_float
并且在应该定义 read_float 的文件中,它出现了一个损坏的name:
00000686 t __ZL10read_floatPKj
任何人都可以给我任何提示,说明为什么此方法没有被正确解析吗?我花了半天时间尝试各种 gcc 编译器和源代码标志,但不幸的是,我还没有找到一个获胜的组合。
I am trying to compile some legacy C/C++ code on Mac OS X with gcc 4.2. that was originally written on Windows using Visual Studio. I am compiling a static library that is being linked against by other code, but I've been encountering some hard to understand linker errors.
When I try to compile some source code against the static library, the linker gives me the following errors:
Undefined symbols for architecture i386:
"_read_float", referenced from:
_sub_token_values in libcompengine.a(alparce.o)
The method read_float is in the library, and it is being compiled. But when I dump the symbols in the library using nm, I see that under libcompengine.a(alparce.o) the method _read_float appears as undefined:
U _read_float
And further down in the file where read_float is supposed to be defined, it appears with a mangled name:
00000686 t __ZL10read_floatPKj
Can anyone give me any hints as to why this method isn't being resolved correctly? I spent half a day trying various gcc compiler and source flags, but unfortunately, I haven't hit on a winning combination yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您计划使用 C++ 库与 C 进行交互,那么正确标记 C 程序将调用的函数非常重要。
我已经完成了以下操作(尽管还有其他方法可以实现),
它的作用是告诉编译器 extern "C" {} 行之间的所有外部函数定义都应该使用 C 链接。它仅在使用 C++ 编译时添加额外的定义。另一种选择是执行类似下面的操作,它基本上允许您逐个函数指定哪些应该使用 C 链接,哪些不应该使用。
If you're planning on using a C++ library to interface with C, it's important that you mark the functions that the C program will be calling are marked appropriately.
I've done the following (though there are other ways of doing it)
What this does is it tells the compiler that all of the external function definitions between the extern "C" {} lines should use C linkage. It only adds the extra definition when compiled with C++. Another option is to do something like the below, which basically lets you specify function-by-function which should use C linkage, and which don't.