CMake入门者求助:如何配置指定的文件目录结构?
Hey there! Let's walk through this clearly since you're new to CMake—those commands can feel confusing at first, but once you map them to your project structure, it clicks.
First, let's recap your directory structure to make sure we're on the same page:
|-- CMakeLists.txt \-- src |-- main.cpp \-- graphics |-- context.h \-- private |-- context.c
You also have GLFW in dependencies/glfw and need to link Vulkan. Let's adjust your CMakeLists.txt to properly handle your own code, then integrate the dependencies you already set up.
Step 1: Demystify the Key Commands You're Confused About
Let's break down the core commands you mentioned to remove the confusion:
add_subdirectory(): Tells CMake to process another directory'sCMakeLists.txt(like GLFW's, which handles building that library for you). You don't need this for yoursrc/graphicsunless you split it into its own sub-project later.add_library(): Creates a library target (static or dynamic) from your source files. This is perfect for wrapping yourgraphicscode so it's cleanly separated frommain.cpp.target_include_directories(): Sets where CMake looks for header files for a specific target. UsePUBLICif other targets (like your main executable) need access to those headers,PRIVATEif only the target itself needs them.target_link_libraries(): Links a target (your executable or library) to other libraries or targets (like your graphics library, GLFW, or Vulkan).
Step 2: Updated CMakeLists.txt with Full Explanations
Here's a complete, commented version of your CMakeLists.txt that handles your project structure and dependencies correctly:
CMAKE_MINIMUM_REQUIRED(VERSION 3.7 FATAL_ERROR) SET(CMAKE_C_STANDARD 99) SET(CMAKE_CXX_STANDARD 11) SET(CMAKE_EXPORT_COMPILE_COMMANDS ON) PROJECT(Project VERSION 1.0.0) # 1. Build your graphics library first # Create a static library from context.c (use SHARED instead if you want a dynamic lib) add_library(graphics STATIC src/graphics/private/context.c ) # Set include directories for the graphics library: # - PUBLIC: main.cpp needs to find context.h in src/graphics # - PRIVATE: only context.c needs access to src/graphics/private target_include_directories(graphics PUBLIC src/graphics PRIVATE src/graphics/private ) # 2. Build your main executable add_executable(${PROJECT_NAME} src/main.cpp ) # 3. Integrate your external dependencies (GLFW and Vulkan) # GLFW - process its subdirectory to build the library, then link to your executable SET(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) SET(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) SET(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) ADD_SUBDIRECTORY(dependencies/glfw) # Vulkan - find the installed package, set include dirs and link libraries FIND_PACKAGE(Vulkan REQUIRED FATAL_ERROR) target_include_directories(${PROJECT_NAME} PRIVATE ${Vulkan_INCLUDE_DIRS}) # 4. Link all dependencies to your main executable # Link your custom graphics library first, then GLFW and Vulkan TARGET_LINK_LIBRARIES(${PROJECT_NAME} graphics glfw ${Vulkan_LIBRARIES} )
Why This Works:
- We first create the
graphicslibrary to encapsulate your graphics code. ThePUBLICinclude directory ensuresmain.cppcan#include "context.h"without messy relative paths. - The main executable links against the
graphicslibrary, so it has access to all the functionality incontext.c. - We moved the dependency setup (GLFW/Vulkan) after creating the main executable—your original code tried linking to
${PROJECT_NAME}before creating it, which would cause CMake errors.
Optional: Modularize with a Subdirectory CMakeLists.txt
If you want to keep things cleaner as your project grows, add a CMakeLists.txt inside src/graphics:
# src/graphics/CMakeLists.txt add_library(graphics STATIC private/context.c ) target_include_directories(graphics PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/private )
Then in your root CMakeLists.txt, replace the library setup with:
add_subdirectory(src/graphics)
This keeps the root file focused on top-level project config.
内容的提问来源于stack exchange,提问作者Karlos

