Barth
Barth

Reputation: 15735

How to produce a "self-contained" static library?

I develop a framework under the form of a shared library (in Linux). A user asked for a static version of the library. I use cmake and therefore just switched BUILD_SHARED_LIBS to OFF. I ended up with a static library as expected.

However, the user complained that he has now to link against boost and hdf5 that are dependencies of my library.

Do you think that I have to take action to avoid this situation ? Or is it normal ? Is it ever possible to provide a library that has no dependencies ?

EDIT: Should I do something like extracting the object files from the boost and hdf5 static libraries and add them when building my own ?

Upvotes: 1

Views: 1634

Answers (1)

Naszta
Naszta

Reputation: 7744

The boost' solution is:

set(Boost_USE_STATIC_LIBS   ON)
set(Boost_USE_STATIC_RUNTIME ON) # it may help
find_package(Boost REQUIRED ...)

For hdf5 you could try something like this.

  1. Find the HDF static library with FIND_LIBRARY
  2. Copy the static library into a directory in your build tree with

    EXEC_PROGRAM( ${CMAKE_COMMAND} -E copy_if_different ${HDF_LIB} ${PROJECT_BINARY_DIR}/HDFStaticLib)

  3. Add the link directory for HDFStaticLib first with

    LINK_DIRECTORIES(${PROJECT_BINARY_DIR}/HDFStaticLib)

  4. Add the library like this:

    TARGET_LINK_LIBRARIES(foo ${PROJECT_BINARY_DIR}/HDFStaticLib/HDF)

One more thing: you should have renamed API.a to libAPI.a, if the file name does not start by lib.

Upvotes: 3

Related Questions