CMake安装项目不会将我的可执行文件复制到我指定的文件夹

Sim*_*orn 8 c++ cmake visual-studio-2010

我刚刚开始使用CMake.我成功地建立了最小的Hello,World!C++应用程序可能适用于Windows 7上的Visual Studio 2012,但我有一个最后一个唠叨的事情,这是不太正确,我无法弄清楚为什么:(

我的文件夹结构是:

[cmakeTest]
- [build]
- [source]
  - [helloWorld]
    - main.cpp
    - CMakeLists.txt
  - CMakeLists.txt
Run Code Online (Sandbox Code Playgroud)

我的main.cpp文件只是:

#include <iostream>

int main()
{
    std::cout << "Hello World!";
}
Run Code Online (Sandbox Code Playgroud)

source/CMakeLists.txt 是:

cmake_minimum_required (VERSION 3.0.1)

# Specifies project name for Visual Studio solution.
# Visual Studio projects will be made for each CMake target specified

project(cmakeTesting)

# Set the install directory
set(CMAKE_INSTALL_PREFIX ${cmakeTesting_BINARY_DIR}/bin)

# Generate organiser projects
# Creates "CMakePredefinedTargets" folder with INSTALL and ZERO_CHECK

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Queue up CMakeLists from subdirectories

add_subdirectory(helloWorld)
Run Code Online (Sandbox Code Playgroud)

source/helloWorld/CMakeLists.txt 是:

# Set Properties->General->Configuration Type to Application (.exe)
# Creates helloWorld.exe with the listed sources (main.cxx)
# Adds sources to the Solution Explorer

add_executable (helloWorld main.cpp)

# Creates a folder called "executables" and adds target
# project (helloWorld.vcproj) under it

set_property(TARGET helloWorld PROPERTY FOLDER "executables")

# Adds logic to INSTALL.vcproj to copy helloWorld.exe to dest dir

install (TARGETS helloWorld RUNTIME DESTINATION ${PROJECT_BINARY_BIN}/bin)
Run Code Online (Sandbox Code Playgroud)

什么工作:

  • 它在构建目录中创建Visual Studio解决方案/项目内容

  • 该项目在调试和发布模式下构建和运行

  • 它创建EXE文件/build/helloWorld/Debug//build/helloWorld/Release(工作)

什么行不通:

  • Visual Studio说它已将EXE文件复制到/bin/helloWorld.exe,但它没有> :-(

.

1> ------ Build build:项目:ZERO_CHECK,配置:发布Win32 ------

2> ------ Build build:项目:ALL_BUILD,配置:发布Win32 ------

2>构建所有项目

3> ------ Build build:项目:INSTALL,配置:发布Win32 ------

3> - 安装配置:"发布"

3> - 最新:/bin/helloWorld.exe

==========构建:3成功,0失败,1最新,0跳过==========

.

我知道它看起来很挑剔,但我正在努力确保在了解更复杂的东西(PS我使用的是CMake客户端,而不是命令行)之前,我已经理解了所有正在发生的事情.

Fra*_*ser 12

这可能只是归结为一个错字.在source/helloWorld/CMakeLists.txt的最后一行,我猜你是指PROJECT_BINARY_DIR而不是PROJECT_BINARY_BIN

这里发生的是${PROJECT_BINARY_BIN}/bin解析为/bin(在CMake中取消引用未定义的字符串,但遗憾的是不产生警告)并且/bin是绝对路径.如果您的项目位于C:驱动器中,我希望您会发现C:\ bin\helloWorld.exe 确实存在:Visual Studio一直没有骗你:-)

另外,通常在install命令中指定相对路径以允许用户选择安装根.同样,硬编码并不是真正的用户友好CMAKE_INSTALL_PREFIX(至少没有警告).

在这种情况下,我将install命令更改为:

install (TARGETS helloWorld RUNTIME DESTINATION bin)
Run Code Online (Sandbox Code Playgroud)

set(CMAKE_INSTALL_PREFIX ...)从源/ CMakeLists.txt中删除.

假设您的项目的根目录是C:\ myProject,然后从Visual Studio命令提示符,您可以执行以下操作:

cd C:\myProject\build
cmake -DCMAKE_INSTALL_PREFIX="C:\myProject\build" ..\source
cmake --build . --config Release --target INSTALL
bin\helloWorld.exe
Run Code Online (Sandbox Code Playgroud)