Clang and undefined symbols when building a library - c++

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 complains that some symbols aren't solved when I build the library. They shouldn't be: these libraries will be linked with the final binary, it shouldn't happen on an intermediary.
Then, there's also a few methods and variables that are supposed to be implemented in the "client" binary... with GCC, no problems, but Clang also complains that these symbols can't be solved during compilation.
How come ? What should I do ?
Here's my CMakeLists.txt in case that can 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 the two kinds of error I get:
Undefined symbols for architecture x86_64:
"_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

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));

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()

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

Related

Getting undefined reference error but nm shows symbol present

I am building a large application using libkml. I am using the cmake port of libkml from here: https://github.com/rashadkm/libkml
I am getting a stranged undefined reference to symbol error even thought the symbol appears to be referenced and defined.
This is the make command:
/usr/bin/c++ -fPIC -Werror=return-type -Werror=return-type -Wall
-Werror=parentheses -Werror=uninitialized -Werror=missing-braces
-fPIC -O0 -Wall -fPIC -fvisibility=hidden -fno-strict-aliasing
-Wno-long-long -m64 -g -D_DEBUG --coverage -Wl,-Bsymbolic -Wl,--
no-undefined -shared -o GPS2KML.plb CMakeFiles/GPS2KML.dir
/gps.cpp.o CMakeFiles/GPS2KML.dir/kml.cpp.o CMakeFiles/GPS2KML.dir
/stdafx.cpp.o /trunk/src/filter/GPS2KML/external/libkml/lib/cmake
/libkml/../../libkmlconvenience.so.1.3.1 /trunk/src/filter/GPS2KML
/external/libkml/lib/cmake/libkml/../../libkmlengine.so.1.3.1
/trunk/src/filter/GPS2KML/external/libkml/lib/cmake/libkml/../..
/libkmldom.so.1.3.1 /trunk/src/filter/GPS2KML/external/libkml
/lib/cmake/libkml/../../libkmlbase.so.1.3.1 -lminizip -luriparser
-lexpat
The make output:
CMakeFiles/GPS2KML.dir/kml.cpp.o: In function `cKML::~cKML()':
/trunk/src/filter/GPS2KML/src/kml.cpp:55: undefined reference to `*kmldom::SerializePretty(boost::intrusive_ptr<kmldom::Element> const&)*'
collect2: error: ld returned 1 exit status
Now if i do this:
daniyal#daniyal-Inspiron-5521:/$ nm --demangle --extern-only --defined-only ../trunk/src/filter/GPS2KML/external/libkml/lib/libkmldom.so | grep SerializePretty
It clearly shows:
000000000013c9aa T kmldom::SerializePretty[abi:cxx11](boost::intrusive_ptr<kmldom::Element> const&)
Now I fail to understand what is the problem. I have check the existing questions on stackoverflow regarding this, I found four solutions in the existing questions:
In some cases the demangled symbol name did not match with the symbol that was causing error. This is clearly not my case.
Use -llibrary at the end of the command after putting the name of the .o file. So that when the linker encounters the library it has an undefined symbol present in that library. This is clearly not a solution for me as I have the libraries present at the end of the command.
In some cases the symbol is present in the shared library but does not have external linkage or is not defined. This can be confirmed by using nm with --extern-only and --defined-only. And hence this is also not a solution for me.
Edit:
Additional Info:
This is the cmake file I am using:
find_package(LibKML REQUIRED)
include_directories(${LIBKML_INCLUDE_DIRS})
add_filter(${PROJECT}
gps.h
gps.cpp
kml.h
kml.cpp
#can2gps.h
#can2gps.cpp
stdafx.h
stdafx.cpp )
target_link_libraries (${PROJECT} ${LIBKML_LIBRARIES})
add_filter roughly translates to this macro:
add_library(${NAME} MODULE ${ARGN} ${${NAME}_MOC} ${${NAME}_UI} ${${NAME}_QRC})
target_link_libraries(${NAME} ${BUILD_LIBS} ${QT_LIBRARIES} ${ADTF_OPENGL_LIBRARY} ${ADTF_ADDITIONAL_UTILS_LIBS})
set_target_properties(${NAME}
PROPERTIES
SUFFIX ".plb"
)
if(UNIX)
set_target_properties(${NAME}
PROPERTIES
PREFIX ""
)
It looks like you have an ABI mismatch issue. ABI is "Application Binary Interface", basically the specification for exactly how arguments make it onto the stack (or are put in registers) and various other things like that.
Try making sure your code is compiled with the -std=c++11 (or -std=gnu++11 if you use any GNU extensions) flag. It looks like that's how libkml was compiled. C++11 has a bunch of new features that require an ABI compatibility break with pre-C++11. C++14 and C++1z are less drastic changes, but they may also break ABI compatibility, I'm not sure. In this case though, the demangled symbol is clear, libkml wants at least C++11.

Source-built gcc linking error

I have Debian Wheezy and I need C++11 features to build my project. I've compiled gcc(c and c++ only) from source and put the libraries under my home folder using this question. I am also using the wrapper script supplied by the guy who answered that question which is
$HOME/gcc/gcc-4.8.4/bin/g++ -Wl,-rpath,$HOME/gcc/gcc-4.8.4/lib32 "$#"
Now, I have a game engine that I use in my project for GUI and graphic operations. It compiles in 32 bit just fine. I pass -m32 switch for all of the external libraries it uses and the engine itself. Also, in order for cmake to find my wrapper, I give following options while running cmake
cmake .. -DCMAKE_CXX_COMPILER=path/to/my/32-bit-wrapper
After compilation, it gives following linking erros
undefined reference to `XOpenDisplay'
undefined reference to `glBlendFunc'
undefined reference to `alGenBuffers'
At first, I thought I may be missing the 32-bit development libraries, so I installed following packages.
libgl1-mesa-dev:i386
libopenal-dev:i386
libx11-dev:i386
but I am getting errors, regardless. So, How can I solve this problem? I can supply additional information such as cmake files etc if needed. Thank you in advance.
EDIT
Cmake file in case if error stems from it
cmake_minimum_required(VERSION 2.8.3)
project(uwmf)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m32 -DLINUX")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32 -DLINUX")
#set(CMAKE_CXX_COMPILER "${HOME_PATH}/devel/g++-4.8.4-32")
#set(CMAKE_C_COMPILER "${HOME_PATH}/devel/gcc-4.8.4-32")
message("${CMAKE_CXX_FLAGS}")
message("${CMAKE_C_FLAGS}")
message("${CMAKE_CXX_COMPILER}")
message("${CMAKE_C_COMPILER}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(GGE ${HOME_PATH}/devel/gorgon-3.x)
set(GRAPHICS ${HOME_PATH}/devel/graphics)
set(SOURCES
src/source.cpp
src/algorithms.h
src/cloud-gen.h
src/latex.h
src/macros.h
src/matrix.h
src/utils.h
)
include_directories(${GGE})
include_directories(${GRAPHICS})
add_executable(uwmf ${SOURCES})
target_link_libraries(uwmf ${GGE}/build/libGGE.a)
UPDATE
ereOn's answer did the trick. I also had to install libalut-dev:i386 and link (-lalut) to successfully compile. I get many warning messages like the following (probably due to introducing additional linkage of same library)
/usr/bin/ld: Warning: type of symbol `glDrawBuffers' changed from 2 to 1 in ../devel/gorgon-3.x/build/libGGE.a(OpenGL.cpp.o)
/usr/bin/ld: Warning: type of symbol `glGetAttribLocation' changed from 2 to 1 in ../devel/gorgon-3.x/build/libGGE.a(OpenGL.cpp.o)
but these are not part of this question. Thank you for your help.
It could be that the symbols that are reported missing are not used by your game engine library and were thus "optimized out" to make for a smaller binary.
Try linking your target executable explicitely with -lX11 and -lGL to see if that works.
As #ereOn said, the linker could have optimized out some symbols. Try linking with -Wl,--no-as-needed.

Need some help figuring out compile-time error: 'Undefined symbols for architecture x86_64: "boost::system::system_category()"' [duplicate]

I'm trying to compile a program on Ubuntu 11.10 that uses the Boost libraries. I have the 1.46-dev Boost libraries from the Ubuntu Repository installed, but I get an error when compiling the program.
undefined reference to boost::system::system_category()
What is it that I do wrong?
The boost library you are using depends on the boost_system library. (Not all of them do.)
Assuming you use gcc, try adding -lboost_system to your compiler command line in order to link against that library.
Linking with a library that defines the missing symbol (-lboost_system) is the obvious solution, but in the particular case of Boost.System, a misfeature in the original design makes it use boost::system::generic_category() and boost::system::system_category() needlessly. Compiling with the flag -DBOOST_SYSTEM_NO_DEPRECATED disables that code and lets a number of programs compile without requiring -lboost_system (that link is of course still needed if you explicitly use some of the library's features).
Starting from Boost 1.66 and this commit, this behavior is now the default, so hopefully fewer and fewer users should need this answer.
As noticed by #AndrewMarshall, an alternative is to define BOOST_ERROR_CODE_HEADER_ONLY which enables a header-only version of the code. This was discouraged by Boost as it can break some functionality. However, since 1.69, header-only seems to have become the default, supposedly making this question obsolete.
Another workaround for those who don't need the entire shebang: use the switch
-DBOOST_ERROR_CODE_HEADER_ONLY.
If you use CMake, it's add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY).
The above error is a linker error... the linker a program that takes one or more objects generated by a compiler and combines them into a single executable program.
You must add -lboost_system to you linker flags which indicates to the linker that it must look for symbols like boost::system::system_category() in the library libboost_system.so.
If you have main.cpp, either:
g++ main.cpp -o main -lboost_system
OR
g++ -c -o main.o main.cpp
g++ main.o -lboost_system
When using CMAKE and find_package, make sure it is :
find_package(Boost COMPONENTS system ...)
and not
find_package(boost COMPONENTS system ...)
Some people may have lost hours for that ...
I got the same Problem:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-1_47 -lboost_filesystem-mgw45-mt-1_47
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj:main_filesystem.cpp:(.text+0x54):
undefined reference to `boost::system::generic_category()
Solution was to use the debug-version of the system-lib:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-d-1_47 -lboost_filesystem-mgw45-mt-1_47
But why?
When I had this, problem, the cause was the ordering of the libraries. To fix it, I put libboost_system last:
g++ mingw/timer1.o -o mingw/timer1.exe -L/usr/local/boost_1_61_0/stage/lib \
-lboost_timer-mgw53-mt-1_61 \
-lboost_chrono-mgw53-mt-1_61 \
-lboost_system-mgw53-mt-1_61
This was on mingw with gcc 5.3 and boost 1.61.0 with a simple timer example.
in my case, adding -lboost_system was not enough, it still could not find it in my custom build environment. I had to use the advice at Get rid of "gcc - /usr/bin/ld: warning lib not found" and change my ./configure command to:
./configure CXXFLAGS="-I$HOME/include" LDFLAGS="-L$HOME/lib -Wl,-rpath-link,$HOME/lib" --with-boost-libdir=$HOME/lib --prefix=$HOME
for more details see Boost 1.51 : "error: could not link against boost_thread !"
...and in case you wanted to link your main statically, in your Jamfile add the following to requirements:
<link>static
<library>/boost/system//boost_system
and perhaps also:
<linkflags>-static-libgcc
<linkflags>-static-libstdc++

How to set CMAKE static linking ( undefined reference to `dlopen' )?

I need to set static linking for my project.
Current state is :
target_link_libraries(armd
${SQLITE3_LIBRARY}
${CMAKE_THREAD_LIBS_INIT}
rt)
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-O0 -Wall -fmessage-length=0")
doesn't work on device, I need to link statically (add -static for device) but when I do
set(CMAKE_CXX_FLAGS "-O0 -Wall -fmessage-length=0 -static")
I'm getting:
/arm-buildroot-linux-uclibcgnueabi/sysroot/usr/lib/libsqlite3.a(sqlite3.o): In function `unixDlOpen':
sqlite3.c:(.text+0x3e5d4): undefined reference to `dlopen'
How do I set static linking for sqlite without getting this error? Or maybe my root system is missing something?
When statically linking some archives, you need to specify its dependencies (in this case libdl) yourself. Be aware of that the order in which you specify the archive files on the linker command line is more important than for linking shared objects.

undefined reference to boost::system::system_category() when compiling

I'm trying to compile a program on Ubuntu 11.10 that uses the Boost libraries. I have the 1.46-dev Boost libraries from the Ubuntu Repository installed, but I get an error when compiling the program.
undefined reference to boost::system::system_category()
What is it that I do wrong?
The boost library you are using depends on the boost_system library. (Not all of them do.)
Assuming you use gcc, try adding -lboost_system to your compiler command line in order to link against that library.
Linking with a library that defines the missing symbol (-lboost_system) is the obvious solution, but in the particular case of Boost.System, a misfeature in the original design makes it use boost::system::generic_category() and boost::system::system_category() needlessly. Compiling with the flag -DBOOST_SYSTEM_NO_DEPRECATED disables that code and lets a number of programs compile without requiring -lboost_system (that link is of course still needed if you explicitly use some of the library's features).
Starting from Boost 1.66 and this commit, this behavior is now the default, so hopefully fewer and fewer users should need this answer.
As noticed by #AndrewMarshall, an alternative is to define BOOST_ERROR_CODE_HEADER_ONLY which enables a header-only version of the code. This was discouraged by Boost as it can break some functionality. However, since 1.69, header-only seems to have become the default, supposedly making this question obsolete.
Another workaround for those who don't need the entire shebang: use the switch
-DBOOST_ERROR_CODE_HEADER_ONLY.
If you use CMake, it's add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY).
The above error is a linker error... the linker a program that takes one or more objects generated by a compiler and combines them into a single executable program.
You must add -lboost_system to you linker flags which indicates to the linker that it must look for symbols like boost::system::system_category() in the library libboost_system.so.
If you have main.cpp, either:
g++ main.cpp -o main -lboost_system
OR
g++ -c -o main.o main.cpp
g++ main.o -lboost_system
When using CMAKE and find_package, make sure it is :
find_package(Boost COMPONENTS system ...)
and not
find_package(boost COMPONENTS system ...)
Some people may have lost hours for that ...
I got the same Problem:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-1_47 -lboost_filesystem-mgw45-mt-1_47
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj:main_filesystem.cpp:(.text+0x54):
undefined reference to `boost::system::generic_category()
Solution was to use the debug-version of the system-lib:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-d-1_47 -lboost_filesystem-mgw45-mt-1_47
But why?
When I had this, problem, the cause was the ordering of the libraries. To fix it, I put libboost_system last:
g++ mingw/timer1.o -o mingw/timer1.exe -L/usr/local/boost_1_61_0/stage/lib \
-lboost_timer-mgw53-mt-1_61 \
-lboost_chrono-mgw53-mt-1_61 \
-lboost_system-mgw53-mt-1_61
This was on mingw with gcc 5.3 and boost 1.61.0 with a simple timer example.
in my case, adding -lboost_system was not enough, it still could not find it in my custom build environment. I had to use the advice at Get rid of "gcc - /usr/bin/ld: warning lib not found" and change my ./configure command to:
./configure CXXFLAGS="-I$HOME/include" LDFLAGS="-L$HOME/lib -Wl,-rpath-link,$HOME/lib" --with-boost-libdir=$HOME/lib --prefix=$HOME
for more details see Boost 1.51 : "error: could not link against boost_thread !"
...and in case you wanted to link your main statically, in your Jamfile add the following to requirements:
<link>static
<library>/boost/system//boost_system
and perhaps also:
<linkflags>-static-libgcc
<linkflags>-static-libstdc++