CMake building a static library with other static library

☆樱花仙子☆ 提交于 2020-02-23 03:44:18

问题


My problem is as follows :

I have OpenSSL as static libraries (libcrypto.a and libssl.a). Compiled on Windows with MSys2/MinGW64. Besides that, I have a small self written library based on OpenSSL.

Now I want to "bundle" the Crypto lib from OpenSSL with My lib to a "big" static library for later statically compiling in other applications on Windows without deploying any library.

What would a CMakeLists.txt file looks like? And are the prerequisites (OpenSSL as static libs) correct?

Actually compiling this to a dynamic DLL works like a charm. But My static lib are only includes the symbols of My own library, not from OpenSSL too.

Actually I have no working CMake file, so I can't include an example. I'm just looking for a direction how to do it.

A short suggestion how to begin would be highly appreciated.


回答1:


If what you really want to do is combine multiple static libraries into one, for the convenience of external consumers, you'll probably need to run a custom command to do this (as it's not something that's frequently done).

Using the GNU toolchain, the commands to do so might look something like this (untested though):

${CMAKE_COMMAND} -E make_directory tempdir
cd tempdir
${CMAKE_AR} x $<yourlib:TARGET_FILE>
${CMAKE_AR} x ${OPENSSL_CRYPTO_LIBRARY}
${CMAKE_AR} x ${OPENSSL_SSL_LIBRARY}
${CMAKE_AR} cf ${CMAKE_BINARY_DIR}/bin/libyourmegalib.a *.o
cd ..
${CMAKE_COMMAND} -E remove_directory tempdir

Then you would use add_custom_command() to create a target to run this script - either put those lines as COMMAND arguments, or create a script template, substitute values at CMake generation time using configure_file(), then have the custom command run that script.

That said, if the external consumers are all using CMake themselves, I would skip this entirely, and just have the installation process for your library generate CMake package configuration files that declare the dependencies on the OpenSSL libraries.




回答2:


target_link_library() works both for add_executable() and add_library(), so you should be able to do:

add_library(yourlib STATIC ...)
target_link_libraries(yourlib lib1 lib2 ...)

(where yourlib is your library, and lib1, ... are the libraries you want to bundle.



来源:https://stackoverflow.com/questions/49945142/cmake-building-a-static-library-with-other-static-library

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