CMake: add static library to shared lib - cmake

I would like to create shared library with cmake, but also I need to link it to third party static libraries.
For example if it should include my own file1.o, file2.o, then statically linked libfoo.la and then be written down to disk as .so file which dynamically linked to libbar.so
Is it even possible?

It is possible to link static library to shared library. On most Unixes, you will need
to add -fPIC flag or its equivalent for producing position-independent code when you build static library. On Windows, there is no PIC, i.e linking static to shared just works out of the box.

Related

FindHDF5 picks static library while shared one is requested

I have compiled hdf5-1.8.21 to have both static and shared libraries. In the project I use
set (HDF5_USE_STATIC_LIBRARIES FALSE)
find_package(HDF5 REQUIRED)
to just select the shared library version but it does not work. It contradicts with the documentation in FindHDF5.cmake. I do not know the reason for it.
Make sure do target_link_libraries for you binary/library with static HDF5 library target HDF5_LIB_TARGET and not HDF5_LIBSH_TARGET

Best way to build a cmake static library from a parent cmake dynamic/static library

I would like to add this cmake-based static library
https://github.com/google/highway
to my static or dynamic library
https://github.com/GrokImageCompression/grok
So, I would like to build static library first, then build the dynamic library
and link to the static library.
I have added the static library as a subfolder, and the .a file is being generated in my
cmake binary folder. But, I am not sure how to link to this library
target_link_libraries(${PARENT_LIBRARY_NAME} ${PARENT_BIN_DIR}/hwy)
doesn't work.
You need to use the name of the target in the call to target_link_libraries, i.e. target_link_libraries(${PARENT_LIBRARY_NAME} PUBLIC hwy) or target_link_libraries(${PARENT_LIBRARY_NAME} PRIVATE hwy) depending on whether you make use of hwys headers in public headers of the parent library (choose PUBLIC) or only in the implementation of the parent library (choose PRIVATE).

can Cmake pkg-config module say if the library is static or not

I have a program, built with cmake, that (optionally) depends on a library (openwsman), which itself depends on some libraries (libcurl, libssl, libxml-2.0 ...)
pkg_check_modules(winrm openwsman++ openwsman)
IF (${winrm_FOUND})
include_directories(${winrm_INCLUDE_DIRS})
add_definitions(-DHAVE_WINRM=openwsman)
target_sources(launch PRIVATE src/exec_impl_winrm_openwsman.cpp)
target_link_libraries(Tests PRIVATE ${winrm_LIBRARIES})
ENDIF()
If the openwsman library provides shared objects, the link runs fine, and the binary depends on libs the libwsman*.so depends on
But if only static libraries is provided, the linker must have access to libs used by the said static libraries (libcurl, libssl, libcrypto, libxml-2.0)
Is there a way, in cmake, to know the libraries provided by ${winrm_LIBRARIES} are the static ones ?
(BTW: openwsman does NOT provide "static" link options in its .pc file, like libcurl does)

Using static library in a shared library created with CMake

I'm using a compiled library that I made, libexp.a. (Was compiled with PIC so its good for this use case).
I want to use it statically in my shared library and I can do that via Mixing static libraries and shared libraries.
So now I'm trying to get that working with CMake, my libexp.a is in the root directory and I do:
find_library(EXP NAMES exp PATHS ${PROJECT_SOURCE_DIR} NO_DEFAULT_PATH)
Then using it in target_link_libraries via ${EXP}, but during link time I still get a linking error of couldn't find -lexp. What is the right way to get this done? Using over 3.6

cmake target_link_libraries - unwanted target

I have a problem with cmake target_link_libraries.
I have 3 libs. The first is static compiled, the second one (shared lib) link to it and the third one is an executable which use the second lib.
My problem is that my first lib is automatically added to the third lib and leads into a "object already defined" problem.
Is it possible to hide away the first lib from the third one?
I use cmake 3.4.x
Compiler: msvc 2010 x64
Thanks in advance
Tonka
Your third "lib" isn't a library, but an application. You need to add this using add_executable, not add_library.
If your shared library links in a static library, and then you want to link an application to both the static library and that shared library, you get two copies of the static library. Never link static libraries you plan to use elsewhere into a shared library, for this reason. Either make the first shared as well (the name implies that's what you want, as it is exactly what you are describing), or a workaround for this design problem could be to not explicitly link the application to the static library.
I've solved it. I can link to a library private, so f.e.
target_link_libraries(MyLib2 PRIVATE MyLib1)
will hide MyLib1 from everybody linking to MyLib2