user1601331
user1601331

Reputation: 21

Adding dependencies to static library using CMake in Visual studio 2017 does not work

I'm trying to convert a big project (with executable, dynamic libraries and static libraries) to use the CMake build system. I have issues adding dependencies to static libraries.

I have a root CMake which will call a list of sub directories

cmake_minimum_required(VERSION 3.8.0)
project(CC)
add_subdirectory(SmartCardUtility)
add_subdirectory(CertificateUtil)
..

The CMakeLists.txt file under the CertificateUtil looks like below.

project(CertificateUtil CXX)
source grouping....
include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../../Output/_INC_DEV")
link_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../OpenSSL")
add_library(${PROJECT_NAME} STATIC ${ALL_FILES})
target_link_libraries(${PROJECT_NAME} libcrypto)

In the above piece of code, if the add_library is replaced by add_executable or add_library with SHARED, then the dependencies are resolved correctly and show up in the Additional_Dependencies but for the static lib, the Additional_Dependencies in the property page is always empty.

After surfing through the net around this point, I understand target_link_libraries command doesn't add anything in the Librarian. Then the question is, what is the right way to add dependencies to static libraries so they show up in the Additional_Dependencies under Librarian?

I tried to add the dependencies as imported objects like below but still it doesn't work.

add_library(libcrypto OBJECT IMPORTED)
set_property(TARGET libcrypto PROPERTY  IMPORTED_OBJECTS "${CMAKE_CURRENT_SOURCE_DIR}/../../OpenSSL/libcrypto.lib")
add_library(${PROJECT_NAME} STATIC ${ALL_FILES} $<TARGET_OBJECTS:libcrypto>)

Upvotes: 2

Views: 763

Answers (1)

vdrizzle
vdrizzle

Reputation: 64

I saw a similar question asked on CMake's gitlab https://gitlab.kitware.com/cmake/cmake/-/issues/16931.

A suggestion was made to use:

set ( CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} somelib.lib" )

This worked for me and it I was able to replicate Visual Studio's Librarian Addition Dependencies. I think you can fix this by adding:

set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} libcrypto")

You can list multiple paths to different libs and they will be included in your generated .lib file

Upvotes: 0

Related Questions