libpthread.so.0: error adding symbols: DSO missing from command line

前端 未结 14 987
长情又很酷
长情又很酷 2020-11-22 07:00

When I\'m compiling openvswitch-1.5.0, I\'ve encountered the following compile error:

 gcc -Wstrict-prototypes -Wall -Wno-sign-compare -Wpointer-arith
     -         


        
相关标签:
14条回答
  • 2020-11-22 07:02

    What I have found is that sometimes the library that the linker complains about is not the one causing the problem. Possibly there is a clever way to work out where the problem is but this is what I do:

    • Comment out all the linked libraries in the link command.
    • Clean out all .o's, .so's etc (Usually make clean is enough, but you may want to run a recursive find + rm, or something similar).
    • Uncomment the libraries in the link command one at a time and re-arrange the order as necessary.

    @peter karasev: I have come across the same problem with a gcc 4.8.2 cmake project on CentOS7. The order of the libraries in "target_link_libraries" section is important. I guess cmake just passes the list on to the linker as-is, i.e. it doesn't try and work out the correct order. This is reasonable - when you think about it cmake can't know what the correct order is until the linking is successfully completed.

    0 讨论(0)
  • 2020-11-22 07:06

    I also encountered same problem. I do not know why, i just add -lpthread option to compiler and everything ok.

    Old:

    $ g++ -rdynamic -m64 -fPIE -pie  -o /tmp/node/out/Release/mksnapshot ...*.o *.a -ldl -lrt
    

    got following error. If i append -lpthread option to above command then OK.

    /usr/bin/ld: /tmp/node/out/Release/obj.host/v8_libbase/deps/v8/src/base/platform/condition-variable.o: undefined reference to symbol 'pthread_condattr_setclock@@GLIBC_2.3.3'
    //lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
    collect2: error: ld returned 1 exit status
    
    0 讨论(0)
  • 2020-11-22 07:12

    The error message depends on distribution / compiler version:

    Ubuntu Saucy:

    /usr/bin/ld: /mnt/root/ffmpeg-2.1.1//libavformat/libavformat.a(http.o): undefined reference to symbol 'inflateInit2_'
    /lib/x86_64-linux-gnu/libz.so.1: error adding symbols: DSO missing from command line
    

    Ubuntu Raring: (more informative)

    /usr/bin/ld: note: 'uncompress' is defined in DSO /lib/x86_64-linux-gnu/libz.so.1 so try adding it to the linker command line
    

    Solution: You may be missing a library in your compilation steps, during the linking stage. In my case, I added '-lz' to makefile / GCC flags.

    Background: DSO is a dynamic shared object or a shared library.

    0 讨论(0)
  • 2020-11-22 07:14

    I found another case and therefore I thing you are all wrong.

    This is what I had:

    /usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: eggtrayicon.o: undefined reference to symbol 'XFlush'
    /usr/lib64/libX11.so.6: error adding symbols: DSO missing from command line
    

    The problem is that the command line DID NOT contain -lX11 - although the libX11.so should be added as a dependency because there were also GTK and GNOME libraries in the arguments.

    So, the only explanation for me is that this message might have been intended to help you, but it didn't do it properly. This was probably simple: the library that provides the symbol was not added to the command line.

    Please note three important rules concerning linkage in POSIX:

    • Dynamic libraries have defined dependencies, so only libraries from the top-dependency should be supplied in whatever order (although after the static libraries)
    • Static libraries have just undefined symbols - it's up to you to know their dependencies and supply all of them in the command line
    • The order in static libraries is always: requester first, provider follows. Otherwise you'll get undefined symbol message, just like when you forgot to add the library to the command line
    • When you specify the library with -l<name>, you never know whether it will take lib<name>.so or lib<name>.a. The dynamic library is preferred, if found, and static libraries only can be enforced by compiler option - that's all. And whether you have any problems as above, it depends on whether you had static or dynamic libraries
    • Well, sometimes the dependencies may be lacking in dynamic libraries :D
    0 讨论(0)
  • 2020-11-22 07:17

    I found I had the same error. I was compiling a code with both lapack and blas. When I switched the order that the two libraries were called the error went away.

    "LAPACK_LIB = -llapack -lblas" worked where "LAPACK_LIB = -lblas -llapack" gave the error described above.

    0 讨论(0)
  • 2020-11-22 07:17

    If you are using CMake, there are some ways that you could solve it:

    Solution 1: The most elegant one

    add_executable(...)
    target_include_directories(...)
    target_link_libraries(target_name pthread)
    

    Solution 2: using CMake find_package

    find_package(Threads REQUIRED) # this will generate the flag for CMAKE_THREAD_LIBS_INIT
    
    add_executable(...)
    target_include_directories(...)
    target_link_libraries(target_name ${CMAKE_THREAD_LIBS_INIT})
    

    Solution 3: Change CMake flags

    # e.g. with C++ 17, change to other version if you need
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread")
    
    0 讨论(0)
提交回复
热议问题