# CMake: Own Shared libray in Windows: error while loading shared libraries

**URL:** https://discourse.cmake.org/t/cmake-own-shared-libray-in-windows-error-while-loading-shared-libraries/2949
**Category:** Code
**Created:** [March 12, 2021, 3:11pm UTC](https://discourse.cmake.org/t/cmake-own-shared-libray-in-windows-error-while-loading-shared-libraries/2949 "2021-03-12T15:11:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)
#### Post date: [March 12, 2021, 3:11pm UTC](https://discourse.cmake.org/t/cmake-own-shared-libray-in-windows-error-while-loading-shared-libraries/2949/1 "2021-03-12T15:11:38Z")

</div>

Hi people,

I have a **CMake project with the next structure:**  
├── CMakeLists.txt  
├── app  
│ ├── CMakeLists.txt  
│ └── main.cpp  
│  
└── theLib  
├── CMakeLists.txt  
├── internal  
│ ├── internal.cpp  
│ └── internal.h  
├── myprint.cpp  
└── myprint.h

And **the output tree is** :  
├── app  
│ └── app.exe  
├── theLib  
│ └── libtheLib.dll

If I compile the program **under linux, all works perfect** , but when I do it **under Windows, compile fine, but app.exe doesn’t execute;**  
I get the next error:

`app.exe: error while loading shared libraries: libtheLib.dll: cannot open shared object file: No such file or directory.`

**I suspect that don’t links the internal.cpp, because, when I move its procedures to inline into internal.h, then works fine.**

Any help please?

Here are the files:  
**root::CMakeLists.txt**

```
cmake_minimum_required(VERSION 3.13)

set(CMAKE_PROJECT_NAME "testProject")
project(${CMAKE_PROJECT_NAME})

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(BIN_PATH "binAPP")
set(TMP_BUILD "tmpBuild")
set(LIB_PATH "theLib")
set(APP_PATH "app")

set(OUT_PATH ${CMAKE_SOURCE_DIR}/../${BIN_PATH})
set(BUILD_TEMP_PATH ${OUT_PATH}/${TMP_BUILD})

add_subdirectory(${LIB_PATH} ${BUILD_TEMP_PATH}/${LIB_PATH})
include_directories(${LIB_PATH})

add_subdirectory(${APP_PATH} "${BUILD_TEMP_PATH}/${APP_PATH}")
include_directories(${APP_PATH})

```

**app::CMakeLists.txt**  
cmake\_minimum\_required(VERSION 3.13)

```
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUT_PATH}/${APP_PATH}) #/${APP_PATH}

include_directories(        

)

set(SOURCES
    main.cpp
)

add_executable(${APP_PATH} ${SOURCES})
add_dependencies(${APP_PATH} ${LIB_PATH}) 	
target_link_libraries(${APP_PATH} PUBLIC ${LIB_PATH} )

```

**theLib::CMakeLists.txt**  
cmake\_minimum\_required(VERSION 3.13)

```
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUT_PATH}/${LIB_PATH}) 
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUT_PATH}/${LIB_PATH})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUT_PATH}/${LIB_PATH})

include_directories(        
	#./internal
)

add_library(${LIB_PATH} SHARED 
			myprint.h
			myprint.cpp
			./internal/internal.h
			./internal/internal.cpp
)
set(LINK_DIRS
	"${CMAKE_CURRENT_SOURCE_DIR}"  
	"${CMAKE_CURRENT_SOURCE_DIR}/internal"
)
target_include_directories(${LIB_PATH} PUBLIC ${LINK_DIRS} )
target_link_libraries(${LIB_PATH} PUBLIC )

```

**Thanks in 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: [March 12, 2021, 3:15pm UTC](https://discourse.cmake.org/t/cmake-own-shared-libray-in-windows-error-while-loading-shared-libraries/2949/2 "2021-03-12T15:15:06Z")

</div>

Linux has RPATH, so the shared library is found using that mechanism. There is no equivalent under Windows, so you must make sure the exe has access to the DLL when starting:

- either place the DLL into the same directory as the exe,
- or modify `PATH` when launching exe so that the directory with the DLL is in the `PATH`

This is not directly related to CMake, this applies to DLLs on Windows in general.

---

<div class="post-metadata">

### Author: ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)
#### Post date: [March 12, 2021, 3:22pm UTC](https://discourse.cmake.org/t/cmake-own-shared-libray-in-windows-error-while-loading-shared-libraries/2949/3 "2021-03-12T15:22:36Z")

</div>

Thank you Petr
