简体   繁体   中英

g++ undefined reference although symbol is present in *.so file

I found a number of similar questions (eg 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 ( ie 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> 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.

我在过去发现这种类型的错误是由于包含文件中缺少适当的extern "C" { ... }包围而引起的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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