Opening symbol from a C++ dynamic library

I do not have much experience with building C++ libraries. This may be a silly question.

The library is built as follows:

shell> g++ -O3 -march=native -fPIC -shared -fopenmp ECL-GC_12-lib.cpp -o ecl-gc.so

I can see that there is a symbol I want to load (make_graph):

shell> nm -D ecl-gc.so
                 U GOMP_parallel@GOMP_4.0
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000002120 T _Z12freeECLgraphR8ECLgraph
0000000000001c30 T _Z12readECLgraphPKc
0000000000001f00 T _Z13writeECLgraph8ECLgraphPKc
0000000000002170 T _Z8runLargePKiS0_PiPViS3_S0_ii
0000000000002200 T _Z8runSmalliPKiS0_PViPii
                 U _Znam@GLIBCXX_3.4
                 w __cxa_finalize@GLIBC_2.2.5
                 U __cxa_throw_bad_array_new_length@CXXABI_1.3.8
                 U __fprintf_chk@GLIBC_2.3.4
                 U __fread_chk@GLIBC_2.7
                 w __gmon_start__
                 U __printf_chk@GLIBC_2.3.4
                 U __stack_chk_fail@GLIBC_2.4
                 U exit@GLIBC_2.2.5
                 U fclose@GLIBC_2.2.5
                 U fopen@GLIBC_2.2.5
                 U fread@GLIBC_2.2.5
                 U free@GLIBC_2.2.5
                 U fwrite@GLIBC_2.2.5
                 U gettimeofday@GLIBC_2.2.5
00000000000027a0 T make_graph
                 U malloc@GLIBC_2.2.5
                 U memset@GLIBC_2.2.5
                 U omp_get_num_threads@OMP_1.0
                 U omp_get_thread_num@OMP_1.0
                 U puts@GLIBC_2.2.5
0000000000002270 T run
                 U stderr@GLIBC_2.2.5

Yet, the symbol is not found:

julia> using Libdl

julia> _LIBDIR = joinpath(@__DIR__, ".", )
"/mnt/c/Users/XXX/Documents/00WIP/ECLGraphColor/."

julia> _LIB = dlopen(joinpath(_LIBDIR, "ecl-gc") * ".so")
Ptr{Nothing} @0x0000000009122940

julia> dlsym(_LIB, :make_graph)
ERROR: could not load symbol "make_graph":
/mnt/c/Users/XXX/Documents/00WIP/ECLGraphColor/./ecl-gc.so: undefined symbol: make_graph
Stacktrace:
 [1] dlsym(hnd::Ptr{Nothing}, s::Symbol; throw_error::Bool)
   @ Base.Libc.Libdl ./libdl.jl:59
 [2] dlsym(hnd::Ptr{Nothing}, s::Symbol)
   @ Base.Libc.Libdl ./libdl.jl:56
 [3] top-level scope
   @ REPL[43]:1

Why is this symbol not found?

By the way, I wrap the functions I want to call with extern "C" {}.

Do you have an MWE that reproduces this? I’ve done pretty much the same thing a week ago and it worked then :thinking:

I deleted whatever I could. Copied everything into a new folder. Now the symbol is found. Go figure.

Thanks.

Edit: My mistake. I neglected to close the library. While it was open, I added the extern "C" to demangle the name, and recompiled. The change was not seen, because the lib was still open.

2 Likes