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

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.

Related

"Undefined reference to dlopen" when statically linking with gcc

After reading up lots of gcc docomentation and similar questions my problem remains.
Im trying to statically link my libluajit.a into my project but no matter what combination of commands I try, one or another error pops up. Ive successfully compiled my project with dynamic linking though.
Right now Im out of ideas so heres what i got right now:
gcc_options = -std=c++11 -static -pthread
src_dir = LuaHost
src_files = $(src_dir)/*.cpp
src_files += $(src_dir)/*.h
src_files += $(src_dir)/LuaSrc/*.h
src_files += $(src_dir)/LuaSrc/*.hpp
lib_cmd = -Wl,--no-as-needed -ldl -L./$(src_dir)/LuaSrc/ -lluajit
#compile everything and output an executeable
all:
g++ $(gcc_options) $(src_files) $(lib_cmd) -o LuaJITSandbox.o
And heres some of the errors:
./LuaHost/LuaSrc/libluajit.a(lj_clib.o): In function `lj_clib_index':
lj_clib.c:(.text+0x1c0): undefined reference to `dlsym'
./LuaHost/LuaSrc/libluajit.a(lj_clib.o): In function `lj_clib_load':
lj_clib.c:(.text+0x2c8): undefined reference to `dlopen'
lj_clib.c:(.text+0x350): undefined reference to `dlerror'
lj_clib.c:(.text+0x424): undefined reference to `dlopen'
The libluajit.a has been compiled on the same machine, a RaspberryPi.
I think -static is not what you are after. -static will build a static application and does not mean link this static library to the application.
There are a few options here. When you link with -lluajit you could remove the the dynamic .so version of the library. gcc will default to dynamic linking, but will fallback to static linking when the dynamic library is not available or not found.
Instead of -lluajit you could just point to the static library file directly - treating it as an object input file: /usr/lib/libluajit.a.
I think the recommend way is to tell the linker how to link you library. Try using -Wl,-Bstatic -lluajit. You can switch between Bstaticand Bdynamic right in front of the library name in case you link to multiple libraries and want to link them differently.

Call C function in C++ class implementation

I have to use https://github.com/google/gumbo-parser library that is written in C.
I have a HtmlParser class which is defined in HtmlParser.h and I implement it's methods in HtmlParser.cpp
I include gumbo.h in HtmlParser.h and call it's functions in implemented by me getLinks(...) function that is in HtmlParser.cpp
When I try to compile it I get undefined reference to 'gumbo_parse'
How can I fix it?
My makefile is
cmake_minimum_required(VERSION 3.3)
project(WebCrawler)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp HtmlParser.cpp HtmlParser.h)
add_executable(WebCrawler ${SOURCE_FILES})
The undefined reference is an error at link time. It means the symbol (function) that you're using and for which the definition was found when compiling the compilation unit cannot be resolved at link time to link against.
If you build in only one command, you probably just need to add a -lgumbo to your command line, eventually with -L<path to directory containing libgumbo.so> if it's not in the default lib path. Typically:
g++ main.cc -lgumbo
or if gumbo lib and headers are in gumbo subdirectories:
g++ main.cc -I/usr/local/include/gumbo/ -L/usr/local/lib/gumbo/ -lgumbo
If you build in multiple command lines (first building objects, then linking them, then you need to add the -l (and eventually -L) options to the link command:
g++ main.cc -o main.o # This is the objects building command
g++ main.o -l gumbo # This is the linking command
Edit: With cmake (that I now see you're using), you must tell that you're using gumbo library. This should be done using find_library:
find_library(gumbo)
If not supported you may need to use link_directories to specify where to find it. Then use target_link_libraries to specify to link with this library for your target.

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.

Clang and undefined symbols when building a library

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

Ffmpeg linking problems using CMake

I'm writing an application containig 2 internal libraries and depends on more 2 external libraries (ffmpeg and opencv). I'm also using CMake to produce UNIX makefiles. And the problem is when i'm trying to build sources, it compiles but don't link with ffmpeg at all and the next output the linker gives:
../../Decoder/libDecoder.a(ConverterAVFrameToRGB.cpp.o): In function `FaceVideo::ConverterAVFrameToRGB::to_rgb_conversion(std::vector<AVFrame*, std::allocator<AVFrame*> >&, int, int, int)':
ConverterAVFrameToRGB.cpp:(.text+0x990): undefined reference to `av_frame_free'
../../Decoder/libDecoder.a(FfmpegDecoder.cpp.o): In function `FaceVideo::FfmpegDecoder::destroy()':
FfmpegDecoder.cpp:(.text+0xa30): undefined reference to `av_frame_free'
../../Decoder/libDecoder.a(FfmpegDecoder.cpp.o): In function `FaceVideo::FfmpegDecoder::decode_next_chunk(int)':
FfmpegDecoder.cpp:(.text+0xb6b): undefined reference to `av_frame_clone'
FfmpegDecoder.cpp:(.text+0xc13): undefined reference to `av_frame_free'
../../Decoder/libDecoder.a(FfmpegEncoder.cpp.o): In function `FaceVideo::FfmpegEncoder::destroy()':
FfmpegEncoder.cpp:(.text+0x132): undefined reference to `avcodec_free_frame'
../../Decoder/libDecoder.a(FfmpegEncoder.cpp.o): In function `FaceVideo::FfmpegEncoder::encode()':
FfmpegEncoder.cpp:(.text+0x4c4): undefined reference to `avcodec_encode_video2'
FfmpegEncoder.cpp:(.text+0x592): undefined reference to `avcodec_encode_video2'
../../Decoder/libDecoder.a(FrameSaver.cpp.o): In function `FaceVideo::FrameSaver::saver(std::vector<AVFrame*, std::allocator<AVFrame*> >&, int, int, int)':
FrameSaver.cpp:(.text+0x869): undefined reference to `av_frame_free'
collect2: ld returned 1 exit status
That's excatly what i don't want to see.
There are three Cmake files: two for internal libraries (use
add_library(Decoder ${SOURCES_DECODER})
and
add_library(Detector ${SOURCES_DETECTOR})
in them) and one for main executable (use
add_executable(Tool ${SOURCES_TOOL})
and
target_link_libraries (Tool Decoder avutil avcodec swscale avformat Detector ${OpenCV_LIBS})
in it).
As far as i understand from CMake manuals and examples, this should make linker link this libraries together, but no effect.
I've been trying lot of things such as:
1) Adding link_directories() with path to libraries (/usr/lib/x86_64-linux-gnu/ for me) wherever it's possile, nothing changed.
2) Linking every library separately, i mean i tried do something like this in my internal libraries CMake files: target_link_libraries (Decoder avutil avcodec swscale avformat). And then link library together into my Tool CMake file: target_link_libraries (Tool Decoder Detector).
3) Editing output makefiles.
4) Compiling simple one-file application just to test whether i can do it or not. I can. g++ -lavcodec -o out mysource.cpp works perfectly.
5) Compling ffmpeg manually and installing it.
The fact is i realy don't know what should i do. I have no idea. And i would very appreciate your every answer.
UPD1:
Output when CMAKE_VERBOSE_MAKEFILE is set
!
/usr/bin/c++ -march=x86-64 -Wall -fPIC -pthread -std=c++0x -D__STDC_CONSTANT_MACROS -march=x86-64 -fPIC CMakeFiles/FaceDetectorTool.dir/home/anton/Programming/facevideo/branches/Stream_Prototype/src/tools/FaceDetectorTool/facedetector.cpp.o -o FaceDetectorTool -rdynamic ../../Detector/libDetector.a ../../Decoder/libDecoder.a -lavutil -lavcodec -lswscale -lavformat /usr/local/lib/libopencv_videostab.so.2.4.7 /usr/local/lib/libopencv_video.so.2.4.7 /usr/local/lib/libopencv_ts.a /usr/local/lib/libopencv_superres.so.2.4.7 /usr/local/lib/libopencv_stitching.so.2.4.7 /usr/local/lib/libopencv_photo.so.2.4.7 /usr/local/lib/libopencv_ocl.so.2.4.7 /usr/local/lib/libopencv_objdetect.so.2.4.7 /usr/local/lib/libopencv_nonfree.so.2.4.7 /usr/local/lib/libopencv_ml.so.2.4.7 /usr/local/lib/libopencv_legacy.so.2.4.7 /usr/local/lib/libopencv_imgproc.so.2.4.7 /usr/local/lib/libopencv_highgui.so.2.4.7 /usr/local/lib/libopencv_gpu.so.2.4.7 /usr/local/lib/libopencv_flann.so.2.4.7 /usr/local/lib/libopencv_features2d.so.2.4.7 /usr/local/lib/libopencv_core.so.2.4.7 /usr/local/lib/libopencv_contrib.so.2.4.7 /usr/local/lib/libopencv_calib3d.so.2.4.7 -ldl -lm -lpthread -lrt /usr/local/lib/libopencv_nonfree.so.2.4.7 /usr/local/lib/libopencv_ocl.so.2.4.7 /usr/local/lib/libopencv_gpu.so.2.4.7 /usr/local/lib/libopencv_photo.so.2.4.7 /usr/local/lib/libopencv_objdetect.so.2.4.7 /usr/local/lib/libopencv_legacy.so.2.4.7 /usr/local/lib/libopencv_video.so.2.4.7 /usr/local/lib/libopencv_ml.so.2.4.7 /usr/local/lib/libopencv_calib3d.so.2.4.7 /usr/local/lib/libopencv_features2d.so.2.4.7 /usr/local/lib/libopencv_highgui.so.2.4.7 /usr/local/lib/libopencv_imgproc.so.2.4.7 /usr/local/lib/libopencv_flann.so.2.4.7 /usr/local/lib/libopencv_core.so.2.4.7 -Wl,-rpath,/usr/local/lib
UPD2
Cmake example looks like that.
cmake_minimum_required (VERSION 2.8)
project (Decoder)
include(../CMakeInit.cmake)
include_directories(${FFMPEG_INCLUDE_DIR})
include_directories(${Stream_Facevideo_SOURCE_DIR}/../src/kernel/decoder/)
include_directories(${Stream_Facevideo_SOURCE_DIR}/../src/kernel/stuff/)
set(SOURCES_VIDEODECODER
*sources*
)
if(WINDOWS)
set(SOURCES_VIDEODECODER ${SOURCES_VIDEODECODER}
*headers*
)
endif(WINDOWS)
set (SOURCES_DECODER
${SOURCES_VIDEODECODER}
)
add_library(Decoder ${SOURCES_DECODER})
link_directories("/usr/lib/x86_64-linux-gnu/")
target_link_libraries(Decoder swscale avformat avcodec avutil)
Seems this solution should work: Findavutil.cmake & CMakeLists.txt
Your problem looks like you forget to add LINK_DIRECTORIES for ffmpeg lib.
If it's ok then try to change order of linking libs in target_link_libraries for executable or add target_link_libraries for your internal libs, that use external ones.