如何将分号(;)附加到cmake字符串

How to append semicolon (;) to cmake string

在 cmake 3.19.2 中,我尝试通过以下方式将分号添加到字符串中:

cmake_minimum_required(VERSION 3.12)
project(some_project)

#--------------------------------------------------------------------
# Set CPP standard
#--------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#--------------------------------------------------------------------
# Create source and headers sets
#--------------------------------------------------------------------
set(SOURCE_FILES)
set(HEADER_FILES)

file( GLOB_RECURSE SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
file( GLOB_RECURSE HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

add_library(${CMAKE_PROJECT_NAME} STATIC  ${SOURCE_FILES})

#--------------------------------------------------------------------
# Generate include list
#--------------------------------------------------------------------
set(INSTALLED_INCLUDES)
foreach(header_path ${HEADER_FILES})
# HERE IM TRYING TO ADD A SEMICOLON BUT IT ALWAYS IGNORED
    string(CONCAT path ${INSTALLED_INCLUDES}${header_path}${\;})
    set(INSTALLED_INCLUDES ${path})
endforeach()
message(-------------->>>>>${INSTALLED_INCLUDES})

set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
      LIBRARY_OUTPUT_DIRECTORY ${CMAKE_INSTALL_LIBDIR}
      PUBLIC_HEADER ${INSTALLED_INCLUDES}
)

install(TARGETS ${CMAKE_PROJECT_NAME}
        PUBLIC_HEADER
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

target_link_libraries(${CMAKE_PROJECT_NAME}
     -lboost_regex
     -lboost_thread
     -lcrypto
     -lssl
     -pthread
     )

但总是忽略分号。

以下代码也不起作用:

string(CONCAT path ${INSTALLED_INCLUDES}${header_path}\;)

string(CONCAT path ${INSTALLED_INCLUDES}${header_path};)

set(SEM ";")
set(INSTALLED_INCLUDES)
foreach(header_path ${HEADER_FILES})
    string(CONCAT path ${INSTALLED_INCLUDES}${header_path}${SEM})
    set(INSTALLED_INCLUDES ${path})
endforeach()

Cmake 文档说,什么: “将所有参数连接在一起并将结果存储在指定的 中。”

我做错了什么?

您应该使用引号来确保分号不会被解释为列表项分隔符。

# file: test.cmake
set(my_string "Foo bar baz")
message(STATUS "${my_string}")

# Add a semicolon to the end of my_string. (Without quotes around ;, this does nothing)
string(APPEND my_string ";")

message(STATUS "${my_string}")

这会打印:

$ cmake -P test.cmake
-- Foo bar baz
-- Foo bar baz;

引用。请记住,不要完全参与其中 - 引用完整的单词。

string(CONCAT path "${INSTALLED_INCLUDES}${header_path}${SEM}")

这很奇怪,但它有效:

#--------------------------------------------------------------------
# Generate include list
#--------------------------------------------------------------------
set(INSTALLED_INCLUDES )
foreach(header_path ${HEADER_FILES})
    string(CONCAT INSTALLED_INCLUDES "${INSTALLED_INCLUDES}${header_path}\;")
endforeach()

#--------------------------------------------------------------------
# Install includes
#--------------------------------------------------------------------
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
      LIBRARY_OUTPUT_DIRECTORY ${CMAKE_INSTALL_LIBDIR}
      PUBLIC_HEADER ${INSTALLED_INCLUDES}
)