CMake:在 Windows 中拥有自己的共享库:加载共享库时出错

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

我有一个 具有下一个结构的 CMake 项目:

├── CMakeLists.txt
├── app
│   ├── CMakeLists.txt
│   └── main.cpp
│
└── theLib
    ├── CMakeLists.txt
    ├── internal
    │   ├── internal.cpp
    │   └── internal.h
    ├── myprint.cpp
    └── myprint.h

输出树是:

├── app
│   └── app.exe
├── theLib
│   └── libtheLib.dll

如果我在 linux 下编译程序 ,一切正常,但是当我在 Windows 下编译程序时,编译正常, 但 app.exe 不执行; 我收到下一个错误:

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

我怀疑不链接 internal.cpp,因为,当我将其程序内联到 internal.h 时,工作正常。

有什么帮助吗?

文件如下: 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 )

提前致谢。

您需要确保应用程序可以找到您的dll。为此,您必须:

  1. 将exe和dlll放到同一个文件夹
  2. 将可以找到 dll 的文件夹添加到您的 PATH 环境变量中。

在这里您可以了解有关 dll 加载的更多信息How dll get loaded