How can I force cmake to statically link a specific library even when BUILD_SHARED_LIBS is set to ON

200 views Asked by At

How can I force a library to link statically even if I want other libraries to link dynamically. I want to link most of my libraries dynamically, but one of my libraries requires static linking. Is there a way that I can force any link to that library to be static while allowing the other libraries to link dynamically?

cmake_minimum_required(VERSION 3.15) # required

set(BUILD_SHARED_LIBS ON) # BUILD_SHARED_LIBS is set to ON

add_library("Top_Level_Library" "${HEADER_FILES}" "${SOURCE_FILES}") # Top Library

add_library("Lower_level_library_1" "${HEADER_FILES}" "${SOURCE_FILES}") # Dependency 1

add_library("Lower_level_library_2" "${HEADER_FILES}" "${SOURCE_FILES}") # Dependency 2

target_link_library("Top_Level_Library" PUBLIC "Lower_level_library_1") # I want this library to link statically, even if BUILD_SHARED_LIBS is set to true
target_link_library("Top_Level_Library" PUBLIC "Lower_level_library_2") # I want this library to link depending on BUILD_SHARED_LIBS

Please note that the above is pseudocode, so fixing a 'bug' in the code won't solve my problem. I just want to know if there is a way to force a library to always link statically, even if other libraries in CMake are linking dynamically.

0

There are 0 answers