0

So I try to link an executable to my library, which is a shared library. But CMake (target_link_libraries) tries to link to a static library.

My folder structure looks like this:

sage/
|-CMakeLists.txt
|-src/
|-include/
|-tests/
  |-CMakeLists.txt
  |-sandbox/
    |-CMakeLists.txt
    |-src/

The library is called "sage". I have a "tests" directory for projects to test the library. This is the CMakeLists.txt for the library:

cmake_minimum_required(VERSION 3.13)

project(sage VERSION 0.1.0)

set(SAGE_SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/")
file(GLOB_RECURSE SAGE_SOURCE_FILES "${SAGE_SOURCE_DIRECTORY}/*.cpp")

add_library(sage SHARED ${SAGE_SOURCE_FILES})

target_include_directories(sage PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")

add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/tests")

This is the CMakeLists.txt in test/

add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/sandbox")

And this is the one in test/sandbox/

set(SANDBOX_SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/")
file(GLOB_RECURSE SANDBOX_SOURCE_FILES "${SANDBOX_SOURCE_DIRECTORY}/*.cpp")

add_executable(sandbox ${SANDBOX_SOURCE_FILES})

target_link_libraries(sandbox PRIVATE sage)

When I use "cmake --build .", I get the error that "sandbox" can not be linked to "sage.lib", because it is not found.

LINK : fatal error LNK1181: Input file "..\..\Release\sage.lib" can not be opened. [C:\Users\Daniel\Documents\Entwicklung\Cpp\sage\build\tests\sandbox\sandbox 
.vcxproj]

But I defined the "sage" target to be a shared library, thus it should link to "sage.dll". I am on Windows unsing the Visual Studio 2019 compiler.

maniel34
  • 177
  • 9
  • The lib mentioned is the import library for the DLL. The import library will not be created if there are no symbols exported from the DLL. – vre Jan 10 '20 at 14:15
  • Please provide the **full** error log, describing the error is not very helpful, in this case. – Kevin Jan 10 '20 at 14:18
  • @vre I'm sorry, I'm quite new to DLLs. Can you maybe explain a little bit what you mean with "symbols exported from the DLL". I did it this way on Linux and it worked totally fine. – maniel34 Jan 10 '20 at 14:29
  • @ago I'm sorry, I added it. I mean it can not be linked because **the .lib file can not be found**. – maniel34 Jan 10 '20 at 14:30
  • When you export the symbols (producing the DLL) you need to prefix them with a `__declspec(__dllexport)` in the header, When importing (consuming the DLL) you need to prefix the with `__declspec(__dllimport)`. See `GenerateExportHeader` documentation from CMake how to do that. https://cmake.org/cmake/help/v3.0/module/GenerateExportHeader.html – vre Jan 10 '20 at 14:39
  • @vre Ah, I know now. Thank you very much! – maniel34 Jan 10 '20 at 14:51

0 Answers0