簡體   English   中英

CMake 源文件找不到 header 文件

[英]CMake source files cannot find header files

我有一個項目,我使用 CMake 的結構如下:

CMakeLists.txt
├── build
├── include
│   ├── AudioDevice.h
│   ├── Game.h
│   ├── game_objects
│   │   ├── Ball.h
│   │   ├── Brick.h
│   │   ├── GameObject.h
│   │   ├── IndestructibleBrick.h
│   │   ├── MoveableGameObject.h
│   │   ├── Physics.h
│   │   ├── Platform.h
│   │   └── Wall.h
│   ├── GameParameter.h
│   ├── InputHandler.h
│   ├── Level.h
│   ├── Renderer.h
│   └── SDL_RAII.h
├── src
│   ├── AudioDevice.cpp
│   ├── Game.cpp
│   ├── game_objects
│   │   ├── Ball.cpp
│   │   ├── Brick.cpp
│   │   ├── GameObject.cpp
│   │   ├── MoveableGameObject.cpp
│   │   ├── Physics.cpp
│   │   └── Platform.cpp
│   ├── GameParameter.cpp
│   ├── InputHandler.cpp
│   ├── Level.cpp
│   ├── main.cpp
│   ├── Renderer.cpp
│   └── SDL_RAII.cpp
├── test
│   ├── game_objects
│   │   ├── Ball_test.cpp
│   │   ├── Brick_test.cpp
│   │   ├── GameObject_test.cpp
│   │   ├── MoveableGameObject_test.cpp
│   │   ├── Physics_test.cpp
│   │   └── Platform_test.cpp
│   └── Level_test.cpp

現在我想通過 CMake 改進項目的組織方式。 目前我在根目錄中只有一個CMakeLists.txt 好的做法應該是在每個文件夾中都有它。

我在根目錄中的CMakeLists.txt目前看起來像這樣:

cmake_minimum_required(VERSION 3.7)

add_definitions(-std=c++17)

set(CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS, "${CXX_FLAGS}")

set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,readability-*)

project(bricks)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

set(LIBRARY_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/lib")

#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

find_package(SDL2 REQUIRED)
find_package(SDL2_mixer REQUIRED)

include_directories(
    ${SDL2_INCLUDE_DIRS} 
    ${SDL2_mixer_INCLUDE_DIRS} 
    include 
    include/game_objects
    include/types
    include/utility 
)

add_subdirectory(thirdparty/googletest)

add_executable(bricks 
    src/game_objects/Ball.cpp
    src/game_objects/Brick.cpp
    src/game_objects/GameObject.cpp
    src/game_objects/MoveableGameObject.cpp
    src/game_objects/Physics.cpp
    src/game_objects/Platform.cpp

    src/AudioDevice.cpp
    src/Game.cpp
    src/GameParameter.cpp
    src/InputHandler.cpp
    src/Level.cpp
    src/main.cpp
    src/Renderer.cpp
    src/SDL_RAII.cpp
)

target_link_libraries(
    bricks 
    SDL2::Main
    SDL2::Mixer
)

add_executable(test 
    test/game_objects/Ball_test.cpp
    src/game_objects/Ball.cpp
    test/game_objects/Brick_test.cpp
    src/game_objects/Brick.cpp
    test/game_objects/GameObject_test.cpp
    src/game_objects/GameObject.cpp
    test/game_objects/MoveableGameObject_test.cpp
    src/game_objects/MoveableGameObject.cpp
    test/game_objects/Physics_test.cpp
    src/game_objects/Physics.cpp
    test/game_objects/Platform_test.cpp
    src/game_objects/Platform.cpp

# needs to be added so linker works with level
    src/GameParameter.cpp

    test/Level_test.cpp
    src/Level.cpp
)

target_link_libraries(test 
    gtest_main 
)

現在我認為作為第一步,最好將文件夾src中的內容放在它自己的CMakeLists.txt中。

所以我的根文件變成了這樣:

cmake_minimum_required(VERSION 3.7)

add_definitions(-std=c++17)

set(CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS, "${CXX_FLAGS}")

set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,readability-* -fix)

project(bricks)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

set(LIBRARY_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/lib")

#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

find_package(SDL2 REQUIRED)
find_package(SDL2_mixer REQUIRED)

add_subdirectory(src)

add_subdirectory(thirdparty/googletest)

add_executable(test 
    test/game_objects/Ball_test.cpp
    src/game_objects/Ball.cpp
    test/game_objects/Brick_test.cpp
    src/game_objects/Brick.cpp
    test/game_objects/GameObject_test.cpp
    src/game_objects/GameObject.cpp
    test/game_objects/MoveableGameObject_test.cpp
    src/game_objects/MoveableGameObject.cpp
    test/game_objects/Physics_test.cpp
    src/game_objects/Physics.cpp
    test/game_objects/Platform_test.cpp
    src/game_objects/Platform.cpp

    src/GameParameter.cpp

    test/Level_test.cpp
    src/Level.cpp
)

target_link_libraries(test 
    gtest_main 
)

我在src創建了這個文件:

include_directories(
    ${SDL2_INCLUDE_DIRS} 
    ${SDL2_mixer_INCLUDE_DIRS} 
    ../include 
    ../include/game_objects
    ../include/types
    ../include/utility 
)

add_executable(bricks 
    game_objects/Ball.cpp
    game_objects/Brick.cpp
    game_objects/GameObject.cpp
    game_objects/MoveableGameObject.cpp
    game_objects/Physics.cpp
    game_objects/Platform.cpp


    AudioDevice.cpp
    Game.cpp
    GameParameter.cpp
    InputHandler.cpp
    Level.cpp
    main.cpp
    Renderer.cpp
    SDL_RAII.cpp
)

target_link_libraries(
    bricks 
    SDL2::Main
    SDL2::Mixer
)

現在,當我運行 CMake 時,它不起作用。 我在Ball.cpp和其他文件中抱怨找不到 header 文件。 我以為我用include_directories添加了它們。

我真的不知道這里發生了什么。 我已經用谷歌搜索了這個問題,但找不到解決方案。

如果有人能給我提示如何很好地重組,我將不勝感激。

include_directories()命令將列出的包含目錄應用於當前 CMake 文件中的目標以及子目錄中的目標。 因此, include_directories()命令不會將包含目錄應用到頂層 CMake 文件中的test目標。 因此,為您的test可執行文件列出的源將不知道您列出的包含目錄。 一個簡單的解決方法是簡單地將include_directories()調用移動到您的頂級 CMake 文件:

... 

include_directories(
    ${SDL2_INCLUDE_DIRS} 
    ${SDL2_mixer_INCLUDE_DIRS} 
    ${CMAKE_SOURCE_DIR}/include 
    ${CMAKE_SOURCE_DIR}/include/game_objects
    ${CMAKE_SOURCE_DIR}/include/types
    ${CMAKE_SOURCE_DIR}/include/utility 
)

add_subdirectory(src)

add_subdirectory(thirdparty/googletest)

add_executable(test 
    test/game_objects/Ball_test.cpp
    src/game_objects/Ball.cpp
    test/game_objects/Brick_test.cpp
    src/game_objects/Brick.cpp
    test/game_objects/GameObject_test.cpp
    src/game_objects/GameObject.cpp
    test/game_objects/MoveableGameObject_test.cpp
    src/game_objects/MoveableGameObject.cpp
    test/game_objects/Physics_test.cpp
    src/game_objects/Physics.cpp
    test/game_objects/Platform_test.cpp
    src/game_objects/Platform.cpp
    src/GameParameter.cpp
    test/Level_test.cpp
    src/Level.cpp
)

target_link_libraries(test 
    gtest_main 
)

您還應該使用變量CMAKE_SOURCE_DIR來獲取頂級 CMake 文件的完整路徑,以避免使用相對路徑。


FWIW,我不知道有任何指南聲稱您應該在項目的每個子目錄中都有一個CMakeLists.txt文件。 在項目層次結構中構建 CMakeLists.txt 文件的方式顯然取決於文件在文件系統中的布局方式、每個 CMakeLists.txt 文件的長度以及其他設計決策。 對於大型項目,我發現每個 target有一個 CMakeLists.txt 文件是很常見的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM