1

在 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 文档说,什么:“将所有参数连接在一起并将结果存储在命名的 <output_variable> 中。”

我做错了什么?

4

3 回答 3

0

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

# 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;
于 2021-02-28T20:31:17.310 回答
0

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

string(CONCAT path "${INSTALLED_INCLUDES}${header_path}${SEM}")
于 2021-02-28T20:47:32.393 回答
0

这很奇怪,但它有效:

#--------------------------------------------------------------------
# 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}
)


于 2021-03-01T16:23:27.127 回答