【发布时间】:2014-06-12 23:24:47
【问题描述】:
我正在使用 gcc(cygwin)、gnu make、windows 7 和 cmake。
我的 cmake testprojekt 具有以下结构
rootdir
|-- App
| |-- app.cpp
| +-- CMakeLists.txt
|-- Lib
| |-- lib.cpp
| |-- CMakeLists.txt
|-- MakeFileProject
+ CMakeLists.txt
rootdir/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;
}
rootdir/Lib/lib.cpp:
#include<iostream>
#include<string>
void printThemMessageToScreen(std::string input){
std::cout<<input;
}
rootdir/CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project(TestProject)
add_subdirectory(App)
add_subdirectory(Lib)
rootdir/Lib/CMakeLists.txt:
add_library(Lib SHARED lib.cpp)
rootdir/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 所在的文件夹:
【问题讨论】:
-
dll 是和可执行文件在同一个文件夹中还是在你的路径中?
-
不,我希望 dll 位于不同的目录中,但是如果我将 dll 复制到与 exe 相同的目录中,程序将执行得很好。
-
那么您很可能必须将包含 dll 的文件夹添加到您的 PATH 变量中。这确实不是 cmake 或 cygwin 特定的问题,而是 Windows 问题。请参阅此处了解 windows 如何查找 dll:msdn.microsoft.com/en-us/library/7d83bc18.aspx
-
最简单的方法是将dll和exe放在同一个文件夹中。我给你的链接显示了 5 种可能的方法来做到这一点,微软认为所有这些方法都很好。尽管使用 UAC 触摸系统文件夹将需要提升权限和权限。
-
顺便说一句,使用 cmake 您可以轻松设置输出文件夹,以便将 .dll 与 exe 放在同一文件夹中:stackoverflow.com/questions/6594796/… 我在所有基于 cmake 的项目中都使用此方法。跨度>
标签: c++ windows dll makefile cmake