linking a shared library with statics using CMake

一世执手 提交于 2019-12-12 10:59:25

问题


cmake 2.8
gcc (GCC) 4.8.1

Edit ----------

Wrapping the static libraries in whole-archive works for every library except the pjmedia-videodev The problem now is that when I try and build I get the following error.

cbar_factory_init': colorbar_dev.c:(.text+0x2a0): undefined reference to pjmedia_format_init_video'

Hello,

I have created a shared library and I need to link that library with about 10 static libraries. I then link my executable with the shared library.

My question is that when I run make it fails to link as it wants the static libraries as well. The purpose of is to create a wrapper for the static libraries. So the executable only has to link with 1 single shared library. As I am linking the shared library with the statics, then the statics will automatically become part of the source code of the shared library.

Only code sippnets to make it short. In my CMakeLists.txt that creates the shared library and links the static libraries:

add_library(app_module_sip SHARED app_module_sip_init.c)

 set(PJSIP_LIBRARIES
  g7221codec
  gsmcodec
  ilbccodec
  milenage
  pj
  pjlib-util
  pjmedia
  pjmedia-codec
  pjmedia-audiodev
  pjmedia-videodev
  pjnath
  pjsip
  pjsip-simple
  pjsip-ua
  pjsua
  portaudio
  resample
  speex
  srtp
)

target_link_libraries(app_module_sip pthread m uuid nsl rt asound crypto ssl ${PJSIP_LIBRARIES})

Now my CMakeLists.txt that makes the executable

add_executable(app sip_test.c)

target_link_libraries(app app_module_sip)

Is this correct what I am doing here. I don't want to link the executable with the static libraries. Just the single shared library as that is my wrapper what I will be calling the functions in.

It does link ok, if I link all the statics libraries when making the executable, but that is not the result I want.

Many thanks for any suggestions,


回答1:


I tried to test my solution but your CMakeLists.txt worked for me without any changes. Still, looking at this question: Include static lib in dynamic lib, it appears that you should try

target_link_libraries(app_module_sip ... ssl -Wl,-whole-archive ${PJSIP_LIBRARIES} -Wl,-no_whole-archive)

(scroll to the end, it's a long line)




回答2:


It's not that simple.

You can look at using '-Wl,--whole-archive' or '-Wl,--export-all-symbols' depending on your platform, but there's no good cross platform way of doing this. Everything does it differently, and windows plays a completely different game using lib.exe.

You probably want to do something like this:

http://www.mail-archive.com/cmake@cmake.org/msg01890.html

...and add support specifically for the platforms you want to support, one at a time.




回答3:


# Location for shared library
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs)

# Create shared library
add_library(app_module_sip SHARED app_module_sip_init.c)

# compile and link for 32 bit mode
set_target_properties(app_module_sip PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

# PJSIP static libraries
set(PJSIP_LIBRARIES
  pjsua
  pjsip-ua
  pjsip-simple
  pjsip
  pjmedia-codec
  pjmedia-videodev
  pjmedia
  pjmedia-audiodev
  pjnath
  pjlib-util
  resample
  milenage
  srtp
  gsmcodec
  speex
  ilbccodec
  g7221codec
  portaudio  
  pj
)

# Wrap the static libraries in to the shared library
target_link_libraries(app_module_sip -Wl,--start-group ${PJSIP_LIBRARIES} -Wl,--end-group 
  m uuid nsl rt pthread asound crypto ssl)

Need to wrap the pjsip libraries will the following linker command -Wl,--start-group *.a -Wl,--end-group.

That solved my problem.



来源:https://stackoverflow.com/questions/19141383/linking-a-shared-library-with-statics-using-cmake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!