visual studio looking for .lib

Hello,

I’m building my own librairy. I try to build a static and a share version (I’m using visual studio community)

my structure:

root
|
|-algos
|   |-src
|   |  |*.cpp
|   |
|   |-include
|   |   |*.h
|   | 
|   | CMakeLists.txt
| CMakeLists.txt

the root cmake is

cmake_minimum_required (VERSION 3.8)

# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
  cmake_policy(SET CMP0141 NEW)
  set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()

project ("algos")


add_subdirectory ("aglos")

and the algos/cmake

add_library(algos src/exceptions.cpp
				   src/localResearch.cpp
				   src/point.cpp
				   src/utils.cpp)


add_library(algos_shared SHARED src/exceptions.cpp
								src/localResearch.cpp
								src/point.cpp
								src/utils.cpp)

add_executable(exemple1 exemple/function_optimisation.cpp)

target_link_libraries(exemple1 PRIVATE algos_shared)

The problem is i get an lnk1104: impossible to open algos_shared.lib.
why, if i give to target_link_library a shared library, cmake is looking for .lib?

If i change the last ligne by:

target_link_libraries(exemple1 PRIVATE algos)

visual studio say to me that an executable is missing for the target and “the value cant be null parameter name: key”

if i delete the section

add_library(algos_shared SHARED src/exceptions.cpp
								src/localResearch.cpp
								src/point.cpp
								src/utils.cpp)

the exemple work perfectly.

Can someone explain me what happen here and how to build and link also shared library?

Thank you advance

On Windows, DLLs themselves are not used for linking, only for runtime loading. At link-time, an “import library” is used. For MyLibrary.dll, there is a corresponding MyLibrary.lib import library which is used for linking. The import library contains “stub” functions for all functions exported from the DLL, which are used to satisfy linker dependencies.

CMake ensures the import library is created automatically, when the DLL exports any symbols. You haven’t shown your C++ code, but I guess that is where the issue lies. Are you actually exporting symbols from your DLL? Look into __declspec(dllexport) and related functionality if you’re not familiar with this.

thank you for you reply.

Indeed i didn’t add __declspec to my c++ code. i’m not so familiar with it and i don’t really know when i should use it as i’m creating one dll file in cmake which should regroup everything. Ok so i will have a look on this __declspec and maybe that will indeed solve my problem.

But what about my second point? when i change

target_link_libraries(exemple1 PRIVATE algos_shared)

by

target_link_libraries(exemple1 PRIVATE algos)

In this case, algos library is static. Why visual studio dont find executable and why if i delete the add_library(algos_shared) everything work perfectly?

Hello again,

i did some research on declspec. and it’s look like i don’t have to use it with cmake: Create dlls on Windows without declspec() using new CMake export all feature (kitware.com)

but i still have to make some modifications