Reputation: 474
I downloaded a framework and decided to bundle it as a .so to include in my other projects. The framework does depend on some static libs though. I saw in multiple posts that you cannot link static libs into a .so because not all linkers support this.
So I then tried to link the static libs to the exe and also link the .so. I get errors saying the references are undefined (reference to stuff from the static libs).
For example:
add_library(framework3 SHARED ${MY_SOURCES}) #produces libframework3.so
Then in exe project:
add_executable(renderer.exe ${MY_SOURCES})
target_link_libraries(tsx_renderer.exe framework3)
target_link_libraries (renderer.exe ${PROJECT_SOURCE_DIR}/Framework/Libs/libjpeg.lib)
target_link_libraries (renderer.exe ${PROJECT_SOURCE_DIR}/Framework/Libs/libpng.lib)
The error message I get is:
/home/joe/ClionProjects/proj/build/libframework3.so: undefined reference to `jpeg_destroy_compress'
/home/joe/ClionProjects/proj/build/libframework3.so: undefined reference to `png_set_write_fn'
Upvotes: 0
Views: 1302
Reputation: 54
Seems that static library are not correctly linked with the exe when using target_link_libraries. I used add_library with the IMPORTED property instead:
add_library(TheStaticLibrary STATIC IMPORTED)
set_property(TARGET TheStaticLibrary PROPERTY IMPORTED_LOCATION "THE_LIBRARY_PATH")
Hope it helps...
Upvotes: 1
Reputation: 19863
Linking shared objects to static libs is fine and should not have any problems as long as you build everything yourself. Importing static libraries from somewhere may bring you some pain.
Usually static libraries are self contained and have all the relevant code to execute. This means that some low level standard library stuff is usually included in the .a. If you have multiple .a coming from different platforms/compilers, then you might have symbols clash/mismatch during linking.
What you are trying to do does not work because libframework3.so will try to link on its own, and not use renderer.exe code to resolve symbols. Then it will not find your .lib and fail
Another solution could be to recompile the static libraries as shared object and use these instead.
Upvotes: 1