我正在使用gcc(cygwin),gnu make,Windows 7和cmake。
我的cmake测试项目具有以下结构。
rootdir
|-- App
| |-- app.cpp
| +-- CMakeLists.txt
|-- Lib
| |-- lib.cpp
| |-- CMakeLists.txt
|-- MakeFileProject
+ CMakeLists.txt
根目录/App/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;
}
根目录/Lib/lib.cpp:
#include<iostream>
#include<string>
void printThemMessageToScreen(std::string input){
std::cout<<input;
}
根目录/CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project(TestProject)
add_subdirectory(App)
add_subdirectory(Lib)
根目录/Lib/CMakeLists.txt:
add_library(Lib SHARED lib.cpp)
根目录/App/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)
现在,当我运行 cmake 和 make 时,所有东西都会生成并构建,没有错误,但是当我尝试执行可执行文件时,它会失败,因为生成的库找不到。
但是:如果我将 lib dll 复制到与应用程序 exe 相同的目录中,它就会被执行!
另外:如果我将库配置为静态,则也可以执行。
如何告诉运行时链接器在哪里查找我的 dll?
更新:
根据用户 Vorren 提出的方法解决方案:
我打开了注册表编辑器,并导航到以下键:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
我创建了一个名为我的应用程序的新键:
在这种情况下:TestProjectExecutable.exe
之后,(默认)值被设置为包括文件名和扩展名的 TestProjectExecutable.exe 的完整路径。然后,我创建了另一个字符串值称为“Path”,并将值设置为 dll 所在的文件夹: