Klang and undefined symbols when building a library

I am working on a C ++ framework, and there are several problems when I compile it on OSX with Clang.

At first I use some other libraries, such as openssl, and clang complains that some characters are not resolved when creating the library. They should not be: these libraries will be associated with the final binary code, this should not happen at the intermediary.

Then there are also several methods and variables that should be implemented in the β€œclient” binary ... with GCC, no problem, but Clang also complains that these characters cannot be resolved at compile time.

How did it happen? What should I do?

Here is my CMakeLists.txt in case this might be useful:

cmake_minimum_required(VERSION 2.8) project(crails_project) set(CMAKE_CXX_FLAGS "-std=c++0x -Wall -Wno-deprecated-declarations -pedantic -DASYNC_SERVER -DSERVER_DEBUG -DUSE_MONGODB_SESSION_STORE") find_package(cppnetlib REQUIRED) include_directories(include /usr/local/include ${CPPNETLIB_INCLUDE_DIRS} .) file(GLOB crails_core src/*.cpp) file(GLOB crails_sql src/sql/*.cpp) file(GLOB crails_mongodb src/mongodb/*.cpp) add_library(crails-core SHARED ${crails_core}) add_library(crails-sql SHARED ${crails_sql}) add_library(crails-mongodb SHARED ${crails_mongodb}) 

This is the command that crashes:

 /usr/bin/c++ -std=c++0x -Wall -Wno-deprecated-declarations -pedantic -DASYNC_SERVER -DSERVER_DEBUG -DUSE_MONGODB_SESSION_STORE -dynamiclib -Wl,-headerpad_max_install_names -o libcrails-core.dylib -install_name /Users/michael/Personal/crails/build/libcrails-core.dylib CMakeFiles/crails-core.dir/src/assets.cpp.o CMakeFiles/crails-core.dir/src/cgi2params.cpp.o CMakeFiles/crails-core.dir/src/cipher.cpp.o [...] 

And here are two types of errors that I get:

Undefined characters for x86_64 architecture:

  "_BIO_ctrl", referenced from: Cipher::encode_base64(unsigned char*, unsigned int) const in cipher.cpp.o 

And the second one:

  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition. "vtable for boost::detail::thread_data_base", referenced from: boost::detail::thread_data_base::thread_data_base() in server.cpp.o 
+6
source share
3 answers

I decided! Clang should get the -undefined dynamic_lookup option to ignore missing characters when compiling the library.

Add this to CMakeFile.txt to get 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() 
+8
source

I do not recommend enabling global dynamic search:

-undefined dynamic_lookup which marks all undefined characters as searchable at runtime.

A safer way to fix it for certain characters:

-Wl,-U,symbol_name , which does this only for this symbol (note: you must add an underscore to the symbol name)

You can also use weak dynamic binding:

 extern int SayHello() __attribute__((weak)); 
+9
source

According to one of the commentators, you should use -lcrypto to prevent the first error.

The second error seems to be related to the incompatibility of ABI clang and gcc. Rebuild boost with clang ++ and libC ++. See SO Messages. Is clang ++ ABI the same as g ++? , Why can not I contact libC ++ in C ++ 0x link this boost :: program_options example? and How to compile / link Boost with clang ++ / lib ++? .

If you run into linker problems with other libraries, you should also try rebuilding them with clang ++.

Edit:

To tell the OS X linker to allow unresolved characters, you must add -undefined dynamic_lookup to the linker options. See also SO post. Error creating dynamic library with .o

+1
source

Source: https://habr.com/ru/post/974144/


All Articles