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

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.

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.

Thank you Petr