CMake cannot find source file (add_executable)

前端 未结 2 1852
眼角桃花
眼角桃花 2020-12-06 10:57

I have been trying to follow the vendor\'s tutorial here: CMake-Tutorial, look over the documentation here: Cmake-Documentation, and educate myself as best as possible with

相关标签:
2条回答
  • 2020-12-06 11:29

    In add_executable() call relative paths are always interpreted relative to variables CMAKE_CURRENT_SOURCE_DIR or CMAKE_CURRENT_BINARY_DIR.

    If you have source files in subdirectories of these dirs, you should explicitely specify these subdirectories:

    add_executable(main src/main.cpp)
    

    Also, variables like PROJECT_SOURCE_DIR should be treated as readonly: while changing them isn't detected by CMake as an error, it breaks some things.

    0 讨论(0)
  • 2020-12-06 11:43

    There are some points I'd like to mention.

    1. include_directories helps for finding header files. Source files must always have a complete relative path.

      Assuming that your main.cpp is within src, the correct syntax is

      add_executable(main ${PROJECT_SOURCE_DIR}/main.cpp)
      
    2. Including ${PROJECT_BINARY_DIR} does not make sense.

    3. Overwriting CMake default variables like ${PROJECT_SOURCE_DIR} is no good practice. In future you will always remember that you have done such a thing and for another programmer it is quite unexpected.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题