CMake - add_executable called with incorrect number of arguments

后端 未结 2 1333
忘了有多久
忘了有多久 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: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: //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
    

提交回复
热议问题