Dan*_*wna 9 c++ windows dll makefile cmake
我正在使用gcc(cygwin),gnu make,windows 7和cmake.
我的cmake testprojekt具有以下结构
rootdir
|-- App
| |-- app.cpp
| +-- CMakeLists.txt
|-- Lib
| |-- lib.cpp
| |-- CMakeLists.txt
|-- MakeFileProject
+ CMakeLists.txt
Run Code Online (Sandbox Code Playgroud)
ROOTDIR /应用/ app.cpp:
#include<string>
void printThemMessageToScreen(std::string input);//prototype
int main(int argc,char **argv){
printThemMessageToScreen("this will be displayed by our lib");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
ROOTDIR /库/ lib.cpp:
#include<iostream>
#include<string>
void printThemMessageToScreen(std::string input){
std::cout<<input;
}
Run Code Online (Sandbox Code Playgroud)
ROOTDIR /的CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project(TestProject)
add_subdirectory(App)
add_subdirectory(Lib)
Run Code Online (Sandbox Code Playgroud)
ROOTDIR/lib中/的CMakeLists.txt:
add_library(Lib SHARED lib.cpp)
Run Code Online (Sandbox Code Playgroud)
ROOTDIR /应用/的CMakeLists.txt:
# Make sure the compiler can find include files from our Lib library.
include_directories (${LIB_SOURCE_DIR}/Lib)
# Make sure the linker can find the Lib library once it is built.
link_directories (${LIB_BINARY_DIR}/Lib)
# Add executable called "TestProjectExecutable" that is built from the source files
add_executable (TestProjectExecutable app.cpp)
# Link the executable to the lib library.
target_link_libraries (TestProjectExecutable Lib)
Run Code Online (Sandbox Code Playgroud)
现在,当我运行cmake和make时,一切都将生成并构建,没有错误,但是当我尝试执行二进制文件时,它将失败,因为找不到生成的库.
但是:当我将lib dll复制到像app exe这样的目录时,它会被执行!
另外:如果我将库配置为静态,它也将执行.
如何告诉运行时链接器在哪里查找我的DLL?
解决方案根据用户Vorren提出的方法:
我打开了注册表编辑器,导航到以下密钥:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
Run Code Online (Sandbox Code Playgroud)
,在这里我创建了一个名为applikation的新密钥:
在这种情况下:TestProjectExecutable.exe
之后,(默认)值设置为TestProjectExecutable.exe的完整路径,包括文件名和扩展名.然后我创建了另一个名为"Path"的字符串值,并将值设置为dll所在的文件夹:

mr.*_*.pd 10
您的问题不在于链接器或编译器,而在于Windows搜索DLL的方式.
操作系统将使用以下算法来定位所需的DLL:
顺便拜访:
因此,如果您不想使用特定于应用程序的dll来混淆操作系统目录,那么您有两个合理的选择:
amu*_*ray 10
一个解决方案,我宁愿没有真正被提到的,是建立自己的共享库到同一目录下的可执行文件.这往往是一个更简单的解决方案.
使用cmake执行此操作的一种方法是
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $ {CMAKE_BINARY_DIR}/bin)
或者您也可以根据构建风格设置输出目录.
我发现(我认为是)处理这个问题的好方法。它遵循将 .dll 添加到与 .exe 相同的目录的方法。你可以在 CMake 中这样做:
if (WIN32)
# copy the .dll file to the same folder as the executable
add_custom_command(
TARGET <app-target> POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
$<TARGET_FILE_DIR:<lib-target>>
$<TARGET_FILE_DIR:<app-target>)
endif()
Run Code Online (Sandbox Code Playgroud)
其中app-target是您正在构建的应用程序或库的名称(通过add_executable或创建add_library),lib-target是使用find_package.
# full example
cmake_minimum_required(VERSION 3.14)
project(my-app-project VERSION 0.0.1 LANGUAGES CXX)
find_package(useful-library REQUIRED)
add_executable(my-application main.cpp)
target_link_libraries(my-application PUBLIC useful-library::useful-library)
if (WIN32)
# copy the .dll file to the same folder as the executable
add_custom_command(
TARGET my-application POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
$<TARGET_FILE_DIR:useful-library::useful-library>
$<TARGET_FILE_DIR:my-application>)
endif()
Run Code Online (Sandbox Code Playgroud)