g ++未定义的引用,尽管符号存在于* .so文件中 [英] g++ undefined reference although symbol is present in *.so file

查看:506
本文介绍了g ++未定义的引用,尽管符号存在于* .so文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些类似的问题(例如 gnss-sdr 核心),如下所示:

  $ nm libgnss_system_parameters_dyn.so | c ++ filt | grep Gps_Eph 

包含符号 Gps_Ephemeris :: Gps_Ephemeris ),应该是一个构造函数。



我写了一些最小的代码:

  #include< iostream> 
#include< core / system_parameters / gps_ephemeris.h>

int main(int argc,const char * argv [])
{
Gps_Ephemeris ge;
返回0;
}

我编译的内容:

  g ++ main.cpp -std = c ++ 0x -I some_include_path -L some_lib_path -l gnss_system_parameters_dyn` 

链接器会抱怨:

  /tmp/ccHCvldG.o:In函数`main':
main.cpp :(。text + 0x33):未定义引用到`Gps_Ephemeris :: Gps_Ephemeris()'
collect2:错误:ld返回1退出状态

我也尝试了 cmake ,但它生成的行类似于(它只是添加了 -rdynamic 在链接之前),并且它仍然生成完全相同的链接器错误。



请注意,库和我的最小代码正在使用相同的编译器(g ++ - 5)进行编译,具有完全相同的标志和相同的c ++ 0x标准。




解决Maxim Egorushkin的答案:

  nm --demangle --defined-only --extern - 仅限libgnss_sys tem_parameters.so | grep Gps_Eph 

不输出任何内容。但是,符号是在静态库( * .a库)中定义的:

  00000000000006b0 T Gps_Ephemeris :: Gps_Ephemeris()
00000000000006b0 T Gps_Ephemeris :: Gps_Ephemeris()

知道它们都是由 cmake 生成的,如下所示:

  add_library(lib_name SHARED $ {sources_etc })#为* .so 
add_library(lib_name_2 $ {sources_etc})#为* .a

这些库中包含/定义的符号应该没有区别,对吧?我在 add_library cmake 文档中没有注意到任何内容。我错过了什么明显的东西?

解决方案

c $ c>出口符号为 nm --demangle --dynamic --defined-only --extern-only< lib.so> | grep< symbol>



没有 - 仅定义您的命令也会显示 undefined p>

如果没有 - extern-only ,它还会显示内部链接符号链接。



看起来你需要链接另一个库,因为 Gps_Ephemeris :: Gps_Ephermeris()链接 libgnss_system_parameters_dyn.so 。开始一个好的方法就是图书馆的文件和例子。

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

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; 
}

which I compile with:

g++  main.cpp -std=c++0x -I some_include_path -L some_lib_path -l gnss_system_parameters_dyn`

The linker then complains:

/tmp/ccHCvldG.o: In function `main':
main.cpp:(.text+0x33): undefined reference to `Gps_Ephemeris::Gps_Ephemeris()'
collect2: error: ld returned 1 exit status

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.

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.


Addressing the answer by Maxim Egorushkin, the line:

nm --demangle --defined-only --extern-only libgnss_system_parameters.so  |grep Gps_Eph

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()

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

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?

解决方案

The pedantically correct way to check that a .so exports a symbol is nm --demangle --dynamic --defined-only --extern-only <lib.so> | grep <symbol>.

Without --defined-only your command also shows undefined symbols.

Without --extern-only it also shows symbols with internal linkage which are unavailable for linking.

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文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆