在cmake中命名可执行文件

时间:2019-07-03 17:30:38

标签: cmake

我正在研究一个项目,并决定将其移植到CMake。在配置时,我使用了占位符版本名称build;没有遇到任何问题。但是在将名称更改为S&W之后,CMake不允许我构建。它产生了错误信息:

[cmake] CMake Error at CMakeLists.txt:28 (add_executable):
[cmake]   The target name "S&W" is reserved or not valid for certain CMake features,
[cmake]   such as generator expressions, and may result in undefined behavior.

我很确定CMake不保留名称S&W

那为什么会这样呢?我可以做些什么来纠正此错误吗?

cmake_minimum_required(VERSION 3.14)
project(S&W)

include_directories("source")
include_directories("dependencies/include")

file(GLOB_RECURSE SRC "source/*.cpp")
LINK_DIRECTORIES("dependencies/lib/x86")

if ( MINGW )
set(CMAKE_EXE_LINKER_FLAGS  "-static-libgcc -static-libstdc++")
LINK_LIBRARIES("mingw32")
endif ( MINGW )

LINK_LIBRARIES("libSDL2main.a")
LINK_LIBRARIES("libSDL2.dll.a")
LINK_LIBRARIES("libSDL2_mixer.dll.a")

add_executable(${PROJECT_NAME} ${SRC}) 

2 个答案:

答案 0 :(得分:1)

该错误表明您无法声明包含某些特殊符号(在您的情况下为&)的 target

但是,这并不意味着您不能生成具有给定名称的文件。只需创建“普通”可执行目标,然后为其分配OUTPUT_NAME属性:

add_executable(SW ${SRC})
set_target_properties(SW PROPERTIES OUTPUT_NAME "S&W")

答案 1 :(得分:1)

您已在目标名称中禁止使用字符,即&

看看有关cmake target naming的文档,尤其是本段:

Target names may contain upper and lower case letters, numbers, the underscore
character (_), dot(.), plus(+) and minus(-). As a special case, ALIAS targets
and IMPORTED targets may contain two consequtive colons.