1

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.

Eurobertics
  • 78
  • 1
  • 8
  • In short, CMake [doesn't help](https://cmake.org/pipermail/cmake/2010-April/036669.html) you in combining several static libraries into single one. You may use `add_custom_command`/`add_custom_target` for specify utilities invocation, which does that job. CMake-**unrelated** ways see in that question: https://stackoverflow.com/questions/3821916/how-to-merge-two-ar-static-libraries-into-one. – Tsyvarev Apr 20 '18 at 17:27

2 Answers2

2

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.

Daniel Schepler
  • 3,043
  • 14
  • 20
-1

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.

Marco Pantaleoni
  • 2,529
  • 15
  • 14
  • 2
    `target_link_libraries` doesn't **bundle** other libs into your one. So, when create an application which uses `yourlib`, it still requires `lib1`, `lib2` libraries to be existed. – Tsyvarev Apr 20 '18 at 17:31