如何最好地告诉CMake在哪里找到dll [英] How to best tell CMake where to find dll

查看:840
本文介绍了如何最好地告诉CMake在哪里找到dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自惊人教程的简单项目结构

I have a simple project structure derived from the amazing tutorial

https://rix0r.nl/blog/2015/08/13/cmake-guide/

它看起来如下:

- src
  - CMakeLists.txt
  - mylib
    - include/mylib/mylibclass.h
    - src/mylibclass.cpp
    - CMakeLists.txt
  - myapp
    - src/myapp.cpp
    - CMakeLists.txt

顶级CMakeLists.txt包含:

The top level CMakeLists.txt contains:

cmake_minimum_required( VERSION 3.6 )
project( sample_project VERSION 0.1 LANGUAGES CXX )

set( BUILD_SHARED_LIBS ON CACHE BOOL "" )

add_subdirectory( mylib )
add_subdirectory( myapp )

mylib文件夹中的CMakeLists.txt包含:

The CMakeLists.txt in the mylib folder contains:

add_library( mylib src/mylibclass.cpp include/mylib/mylibclass.h )
set_target_properties( mylib PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
target_include_directories( mylib
    PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> PRIVATE src )  

myapp文件夹中的一个包含:

And the one in the myapp folder contains:

add_executable( myapp src/myapp.cpp )
target_link_libraries( myapp mylib )

我想使用此结构来开发mylib(作为由BUILD_SHARED_LIBS确定的共享库或静态库)和myapp.为此,我想将myapp设置为Visual Studio中的启动项目,并在MSVC调试器中编译并运行.对于没有额外的CMake代码的共享库,这是不可能的,因为myapp.exe不知道在哪里可以找到mylib.dll.

I want to use this structure to develop both mylib (as a shared or static library as determined by BUILD_SHARED_LIBS) and myapp. For this, I want to set myapp as my startup project in Visual Studio and compile and run in the MSVC debugger. This is not possible for the shared library case without extra CMake code, as the myapp.exe doesn't know where to find the mylib.dll.

什么是告诉程序在哪里可以找到dll的最佳CMake最佳实践?

What is best CMake practice to tell the program where to find the dll?

根据@Andre的建议,我将以下几行添加到了顶层CMakeLists.txt:

Based on the suggestions by @Andre, I've added the following lines to the top level CMakeLists.txt:

set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/out CACHE STRING "" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/out CACHE STRING "" )

推荐答案

发生问题,因为Visual Studio时mylib.dll与myapp.exe不在同一文件夹中,也不在%PATH%环境变量中尝试启动您的myapp.exe

The problem occurs, because your mylib.dll is not in the same folder as your myapp.exe nor is it in the %PATH% environment variable when Visual Studio tries to start your myapp.exe

显而易见的解决方案是确保dll和exe都位于同一文件夹中.有几种方法可以做到这一点:

The obvious solution is to make sure both dll and exe are in the same folder. There are several ways to do this:

  1. 通过设置 RUNTIME_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY 属性在您的目标上:

  1. Put both exe and dll into a single "output" directory by setting the RUNTIME_OUTPUT_DIRECTORY and the LIBRARY_OUTPUT_DIRECTORY properties on your targets:

 set_target_properties( myapp PROPERTIES RUNTIME_OUTPUT_DIRECTORY 
    ${sample_project_BINARY_DIR}/build_results/bin )
 set_target_properties( mylib PROPERTIES LIBRARY_OUTPUT_DIRECTORY
    ${sample_project_BINARY_DIR}/build_results/bin )

这会将myapp.exe和mylib.dll生成到顶级构建文件夹中的单个build_results/bin文件夹中.

This will produce the myapp.exe and mylib.dll into a single build_results/bin folder in your top-level build folder.

或通过设置全局 CMAKE_RUNTIME_OUTPUT_DIRECTORY CMAKE_LIBRARY_OUTPUT_DIRECTORY 变量,对您sample_project中的所有目标执行此操作.

Or by setting the the global CMAKE_RUNTIME_OUTPUT_DIRECTORY and CMAKE_LIBRARY_OUTPUT_DIRECTORY variables which will do this for all targets in your sample_project.

构建后将dll复制到exe位置,例如

Copy the dll to the exe location after building, e.g. with

add_custom_command(TARGET mylib POST_BUILD 命令$ {CMAKE_COMMAND} -E复制mylib.dll $ {myapp_BINARY_DIR}/. )

add_custom_command(TARGET mylib POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy mylib.dll ${myapp_BINARY_DIR}/. )

这篇关于如何最好地告诉CMake在哪里找到dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆