使用 cmake 构建可执行和共享库,runtimelinker 找不到 dll

Building of executable and shared library with cmake, runtimelinker does not find dll(使用 cmake 构建可执行和共享库,runtimelinker 找不到 dll)

本文介绍了使用 cmake 构建可执行和共享库,runtimelinker 找不到 dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 gcc(cygwin)、gnu make、windows 7 和 cmake.

I am working with gcc(cygwin), gnu make, windows 7 and cmake.

我的 cmake testprojekt 结构如下

my cmake testprojekt has the following structure

rootdir
|-- App
|   |-- app.cpp
|   +-- CMakeLists.txt
|-- Lib
|   |-- lib.cpp
|   |-- CMakeLists.txt
|-- MakeFileProject
+ CMakeLists.txt

rootdir/App/app.cpp:

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:

rootdir/Lib/lib.cpp:

#include<iostream>
#include<string>

void printThemMessageToScreen(std::string input){
 std::cout<<input;
}

rootdir/CMakeLists.txt:

rootdir/CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)
project(TestProject)

add_subdirectory(App)
add_subdirectory(Lib)

rootdir/Lib/CMakeLists.txt:

rootdir/Lib/CMakeLists.txt:

add_library(Lib SHARED lib.cpp)

rootdir/App/CMakeLists.txt:

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 时,一切都会生成&构建时没有错误,但是当我尝试执行二进制文件时,它会失败,因为找不到生成的库.

Now, when i run cmake and make, everything will get generated & built with no errors, but when i try to execute the binary, it will fail because the library which was generated could not be found.

但是:当我将 lib dll 复制到与 app exe 相同的目录中时,它将被执行!

BUT: when i copy the lib dll into the same directory like the app exe, it will get executed!

还有:如果我将库配置为静态,它也会执行.

also: if i configure the library to be static, it will also execute.

如何告诉运行时链接器在哪里查找我的 dll?

how to tell the runtime linker where to look for my dll?

根据用户 Vorren 提出的方法解决:

Solution according to the Method proposed by User Vorren:

我打开了注册表编辑器,并导航到以下键:

I opened up the registry editor, and navigated to the following Key:

HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Paths

,这里我用我的应用程序的名称创建了一个新密钥:

, here i created a new key with the name of my Applikation:

在这种情况下:TestProjectExecutable.exe

in this case : TestProjectExecutable.exe

之后,(默认)值设置为 TestProjectExecutable.exe 的完整路径,包括文件名和扩展名.然后我创建了另一个名为Path"的字符串值并将值设置为 dll 所在的文件夹:

after that, the (default) value was set to the full path of TestProjectExecutable.exe including the filename and extension. Then i created another String Value called "Path" and set the value to the folder where the dll was located:

推荐答案

您的问题不在于链接器或编译器,而在于 Windows 搜索 DLL 的方式.

Your problem lies not with linker or compiler, but with the way Windows searches for DLL's.

操作系统将使用以下算法来定位所需的 DLL:

The OS will use the following algorithm to locate the required DLL's:

查看:

  1. 在特定于应用程序的路径注册表项中列出的目录;
  2. 当前进程的可执行模块所在的目录;
  3. 当前目录;
  4. Windows 系统目录;
  5. Windows 目录;
  6. PATH 环境变量中列出的目录;

因此,如果您不想用特定于应用程序的 dll 弄乱操作系统目录,您有两个合理的选择:

Thus you have two reasonable options if you don't want to clutter the OS directories with your app-specific dll:

  1. 创建特定于应用程序的路径注册表项(我会选择此选项)
  2. 将您的 DLL 与您的 EXE 放在同一文件夹中;
  3. 修改 PATH 变量(但为什么要这样做,如果您可以选择选项 1?);

这篇关于使用 cmake 构建可执行和共享库,runtimelinker 找不到 dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 cmake 构建可执行和共享库,runtimelinker 找不到 dll

基础教程推荐