Clang and undefined symbols when building a library

后端 未结 3 1584
一整个雨季
一整个雨季 2020-12-08 17:17

I\'m working on a C++ framework, and there\'s a few issues when I compile it on OSX with Clang.

First of, I\'m using some other libraries, such as openssl, and clang

相关标签:
3条回答
  • 2020-12-08 17:21

    I don't recommend to enable global dynamic lookup:

    -undefined dynamic_lookup which will mark all undefined symbols as having to be looked up at runtime.

    Much more safe way to resolve it for specific symbols:

    -Wl,-U,symbol_name, which only does so for the given symbol (note: you have to prepend an underscore to the symbol name)

    You could also use weak dynamic linking:

    extern int SayHello() __attribute__((weak));
    
    0 讨论(0)
  • 2020-12-08 17:27

    According to one of the commenters you have to use -lcrypto to prevent the first error.

    The second error seems to be due to an ABI incompatibility of clang and gcc. Rebuild boost with clang++ and libc++. See SO posts Is clang++ ABI same as g++?, Why can't clang with libc++ in c++0x mode link this boost::program_options example? and How to compile/link Boost with clang++/libc++?.

    In case you run into linker troubles with other libraries you should also try to rebuild those with clang++.

    Edit:

    In order to instruct the OS X linker to allow unresolved symbols you should add -undefined dynamic_lookup to the linker options. See also SO post Error when making dynamic lib from .o

    0 讨论(0)
  • 2020-12-08 17:28

    Solved it ! Clang needs to receive the option -undefined dynamic_lookup to ignore missing symbols when compiling a library.

    Add this to the CMakeFile.txt to produce the expected effect:

    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
      set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS} -undefined dynamic_lookup")
    endif()
    
    0 讨论(0)
提交回复
热议问题