g++ undefined reference although symbol is present in *.so file(g++ 未定义的引用,尽管 *.so 文件中存在符号)
问题描述
我发现了许多类似的问题(例如 这个,那个 或 this),但它们都没有帮助我解决我的问题.我有一个 *.so 文件(来自 gnss-sdr 的核心),如下所示:
I found a number of similar questions (e.g. this, that or this), but none of them helped me solve my problem. I have a *.so file (from the core of gnss-sdr) that, as indicated by:
$nm libgnss_system_parameters_dyn.so | c++filt |grep Gps_Eph
包含符号Gps_Ephemeris::Gps_Ephemeris()
,应该是构造函数.
contains the symbol Gps_Ephemeris::Gps_Ephemeris()
, which is supposed to be a constructor.
我写了一些最少的代码:
I've written some minimal code:
#include <iostream>
#include <core/system_parameters/gps_ephemeris.h>
int main(int argc,const char* argv[])
{
Gps_Ephemeris ge;
return 0;
}
我用来编译的:
g++ main.cpp -std=c++0x -I some_include_path -L some_lib_path -l gnss_system_parameters_dyn`
然后链接器抱怨:
/tmp/ccHCvldG.o: In function `main':
main.cpp:(.text+0x33): undefined reference to `Gps_Ephemeris::Gps_Ephemeris()'
collect2: error: ld returned 1 exit status
我也试过cmake,但是它生成的那一行是类似的(它只是在链接之前添加了-rdynamic
),它仍然产生了完全相同的链接器错误.
I also tried cmake, but the line it generated was similar to that (it just added -rdynamic
before linking), and it still generated the exact same linker error.
请注意,库和我的最小代码都是使用相同的编译器 (g++-5) 编译的,具有完全相同的标志和相同的 c++0x 标准.
Note that both the library and my minimal code are being compiled with the same compiler (g++-5), with the exact same flags and the same c++0x standard.
Maxim Egorushkin 的回答如下:
Addressing the answer by Maxim Egorushkin, the line:
nm --demangle --defined-only --extern-only libgnss_system_parameters.so |grep Gps_Eph
不输出任何东西.但是,符号是在静态库中定义的(即 *.a 库):
doesn't output anything. However, the symbol is defined in the static library (i.e. the *.a library):
00000000000006b0 T Gps_Ephemeris::Gps_Ephemeris()
00000000000006b0 T Gps_Ephemeris::Gps_Ephemeris()
知道两者都是由cmake生成的,方式如下:
Knowing that both are generated by cmake, in the following way:
add_library(lib_name SHARED ${sources_etc}) #for the *.so
add_library(lib_name_2 ${sources_etc}) #for the *.a
这些库中包含/定义的符号应该没有区别,对吧?我没有注意到 cmake 关于 add_library
的文档中的任何内容.我错过了什么明显的东西吗?
there should be no difference in symbols contained/defined in those libraries, right? I didn't notice anything in cmake's documentation on add_library
. Am I missing something obvious?
推荐答案
检查 .so
是否导出符号的迂腐正确方法是 nm --demangle --dynamic --仅定义 --extern-only <lib.so>|grep <符号>
.
The pedantically correct way to check that a .so
exports a symbol is nm --demangle --dynamic --defined-only --extern-only <lib.so> | grep <symbol>
.
如果没有 --defined-only
,您的命令也会显示 undefined 符号.
Without --defined-only
your command also shows undefined symbols.
如果没有 --extern-only
,它还会显示带有 internal links 的符号,这些符号无法链接.
Without --extern-only
it also shows symbols with internal linkage which are unavailable for linking.
您似乎需要链接另一个库,因为链接 libgnss_system_parameters_dyn.so
无法解析 Gps_Ephemeris::Gps_Ephermeris()
.一个好的开始方式是该库的文档和示例.
It looks like you need to link another library because Gps_Ephemeris::Gps_Ephermeris()
is not resolved by linking libgnss_system_parameters_dyn.so
. A good way to start is that library's documentation and examples.
这篇关于g++ 未定义的引用,尽管 *.so 文件中存在符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:g++ 未定义的引用,尽管 *.so 文件中存在符号
基础教程推荐
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17