1

In cmake 3.19.2 i try to add semicolon to string by the following way:

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
     )

but semicolon always ignored.

The following code doesn't work either:

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

or

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

or

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

Cmake documentation says, what : "Concatenate all the arguments together and store the result in the named <output_variable>."

What i'm doing wrong?

  • 1
    It looks like you're trying to create a list of header files by using the fact that lists in CMake are represented as semicolon-separated strings. But instead of using that representation directly you could use the list manipulation functions provided by CMake instead: `list(APPEND INSTALLED_INCLUDES "${header_path}")` as the only function call inside the `foreach` loop should do exactly what you want. – Corristo Feb 28 '21 at 20:33

3 Answers3

0

You should use quotes to ensure that semicolons aren't interpreted as list-item separators.

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

This prints:

$ cmake -P test.cmake
-- Foo bar baz
-- Foo bar baz;
Alex Reinking
  • 6,342
  • 2
  • 28
  • 47
0

Quote. Remember, don't quite part of it - quote full words.

string(CONCAT path "${INSTALLED_INCLUDES}${header_path}${SEM}")
KamilCuk
  • 69,546
  • 5
  • 27
  • 60
0

It's strange, but it works:

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