CMake项目中include_directories命令的必要性疑问
Hey there! Let's unpack whether you need include_directories in your CMake setup, based on your project structure.
First, let's recap your setup to make sure we're aligned:
- Root workspace:
main.cpp, top-levelCMakeLists.txt, config files,MathFunction/directory, andbuild/directory MathFunction/: Contains its ownCMakeLists.txtandmysqrt.cxx(the implementation file for your math functions)
When would you need include_directories?
The core job of include_directories is to tell the compiler where to look for header files that your source code references. Here's the most common scenario where you'd need it:
Suppose your mysqrt.cxx has a corresponding header file (like MathFunction/mysqrt.h) that declares the functions you want to use in main.cpp. If main.cpp includes this header with something like:
#include "mysqrt.h"
...the compiler won't know to look inside the MathFunction/ directory for that header by default. It'll only check the current directory (where main.cpp lives) and system include paths. That's where include_directories comes in—adding include_directories(MathFunction) to your top-level CMakeLists.txt would tell the compiler to search that directory for headers.
When might you NOT need it?
If you're using full relative paths directly in your #include statements, like:
#include "MathFunction/mysqrt.h"
...the compiler will look for the header relative to main.cpp's location, so you won't need include_directories. That said, this approach is fragile—if you ever rearrange your directory structure, you'll have to update every single #include line that uses this path.
A better modern CMake alternative
Instead of using the global include_directories (which affects all targets in your project), the recommended approach now is to use target-specific include directories with target_include_directories. Here's how to adjust your setup:
- In
MathFunction/CMakeLists.txt, turn yourmysqrt.cxxinto a library (this is good practice for modular code anyway):
add_library(MathFunctions mysqrt.cxx) # Make the MathFunction directory visible to anyone linking this library target_include_directories(MathFunctions PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
- In your top-level
CMakeLists.txt, link this library to your main executable:
add_executable(MyProject main.cpp) target_link_libraries(MyProject PRIVATE MathFunctions)
The PUBLIC keyword here means that any target linking to MathFunctions (like your main executable) will automatically inherit the include path for MathFunction/. So main.cpp can still use #include "mysqrt.h" without any extra include_directories calls—and you avoid polluting the global scope with include paths.
Quick summary
- You need
include_directories(or its modern alternative) if your source files reference headers outside their own directory without using full relative paths. - Modern CMake prefers
target_include_directoriesover the globalinclude_directoriesbecause it's more modular and avoids unintended side effects.
内容的提问来源于stack exchange,提问作者Cantaff0rd

