# visual studio looking for .lib

**URL:** https://discourse.cmake.org/t/visual-studio-looking-for-lib/9427
**Category:** Code
**Tags:** os:windows, comp:msvc, gen:ninja
**Created:** [November 14, 2023, 6:01pm UTC](https://discourse.cmake.org/t/visual-studio-looking-for-lib/9427 "2023-11-14T18:01:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![superdu](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@superdu](https://discourse.cmake.org/u/superdu)
#### Post date: [November 14, 2023, 6:01pm UTC](https://discourse.cmake.org/t/visual-studio-looking-for-lib/9427/1 "2023-11-14T18:01:30Z")

</div>

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:

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

```

the root cmake is

```auto
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

```auto
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:

```auto
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

```auto
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

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [November 14, 2023, 7:05pm UTC](https://discourse.cmake.org/t/visual-studio-looking-for-lib/9427/2 "2023-11-14T19:05:43Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![superdu](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@superdu](https://discourse.cmake.org/u/superdu)
#### Post date: [November 14, 2023, 7:20pm UTC](https://discourse.cmake.org/t/visual-studio-looking-for-lib/9427/3 "2023-11-14T19:20:37Z")

</div>

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

```auto
target_link_libraries(exemple1 PRIVATE algos_shared)

```

by

```auto
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?

---

<div class="post-metadata">

### Author: ![superdu](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@superdu](https://discourse.cmake.org/u/superdu)
#### Post date: [November 14, 2023, 9:51pm UTC](https://discourse.cmake.org/t/visual-studio-looking-for-lib/9427/4 "2023-11-14T21:51:01Z")

</div>

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)](https://www.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/#:~:text=On%20Windows%2C%20you%20must%20either%20use%20compiler%20directives,export%20and%20pass%20that%20file%20to%20the%20linker.)

but i still have to make some modifications
