CMake - add_executable called with incorrect number of arguments

后端 未结 2 1316
忘了有多久
忘了有多久 2021-01-13 19:46

I am trying to organize a C++ project which starts to have a lot of files. I would like to create two executables which share some source file using Cmake. I have found an i

相关标签:
2条回答
  • 2021-01-13 20:16

    You have to quote the variables.

    add_executable(test_mss "${Common_sources}" "${Mss_sources}")
    

    Otherwise for an empty variable, CMake replaces the variable by nothing and the number of arguments seems to be wrong.

    Similar problem: https://stackoverflow.com/a/39733128/2799037

    0 讨论(0)
  • 2021-01-13 20:31

    The error you get is because source list, passed to add_executable, is actually empty.

    Correct way for collect sources in Common/ subdirectory is:

    file(GLOB Common_sources "Common/*.cpp")
    

    In command file(GLOB) RELATIVE option doesn't specify search directory. Instead, it just tells CMake to generate relative paths instead of absolute:

    If RELATIVE flag is specified, the results will be returned as relative paths to the given path.

    Assuming

    file(GLOB Common_sources "Common/*.cpp")
    # gets: /<path-to-source>/Common/my_source.cpp
    

    then (also note to absolute path in RELATIVE option)

    file(GLOB Common_sources RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/Common" "Common/*.cpp")
    # gets: my_source.cpp
    

    and (when files are not under RELATIVE directory)

    file(GLOB Common_sources RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/Mps" "Common/*.cpp")
    # gets: ../Common/my_source.cpp
    
    0 讨论(0)
提交回复
热议问题