我刚开始使用CMake。我已经成功地在Windows 7上为Visual Studio 2012设置了最小的 Hello,World! C ++应用程序,但我有一个最后一个不太正确的事情,我无法想象出于何种原因:(
我的文件夹结构是:
[cmakeTest]
- [build]
- [source]
- [helloWorld]
- main.cpp
- CMakeLists.txt
- CMakeLists.txt
我的main.cpp
文件只是:
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
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)
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)
工作原理:
它在构建目录
项目以调试和发布模式构建和运行
它会在/build/helloWorld/Debug/
和/build/helloWorld/Release
创建EXE文件(工作正常)
什么行不通:
/bin/helloWorld.exe
,但它没有&gt;: - (
1&gt; ------ Build build:Project:ZERO_CHECK,配置:发布Win32 ------
2&gt; ------ Build build:项目:ALL_BUILD,配置:发布Win32 ------
2 - ;建立所有项目
3&gt; ------ Build build:Project:INSTALL,Configuration:Release Win32 ------
3&GT; - 安装配置:“发布”
3&GT; - 最新:/bin/helloWorld.exe
==========构建:3成功,0失败,1最新,0跳过==========
我知道它看起来很挑剔,但我正在努力确保我能够理解所有正在发生的事情,然后才能进入更复杂的东西(P.S。我正在使用CMake客户端,而不是命令行)。
答案 0 :(得分:11)
这可能只是归结为一个错字。在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)
并从source / CMakeLists.txt中删除set(CMAKE_INSTALL_PREFIX ...)
。
假设您的项目的根目录是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