cmake语法学习

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)

# Set the project name
project ("hello_headers")

# Create a sources variable with a link to all cpp files to compile
set(SOURCES
    src/Hello.cpp
    src/main.cpp
)

# Add an executable with the above sources
add_executable(hello_headers ${SOURCES})

message("PROJECT_SOURCE_DIR = ${PROJECT_SOURCE_DIR}")
message("PROJECT_NAME = ${PROJECT_NAME}")

# Set the directories that should be included in the build command for this target
# when running g++ these will be included as -I/directory/path/
target_include_directories(hello_headers
    PRIVATE
        ${PROJECT_SOURCE_DIR}/include
)

*

 set(SOURCE

        src/Hello.cpp

        src/main.cpp

)

- SOURCE is the variable contained the file paths you listed behind it in set().

- src/Hello.cpp is the first file.

- src/main.cpp is the second one.

You can add as many as you need in this project. 

*

${XXXX}

- Get/Use the value of XXXX

*

message("a sentence")

- Print a sentence to console window.

*

target_include_directories(hello_headers
    PRIVATE
        ${PROJECT_SOURCE_DIR}/include
)

- Add include file to your project

After seting that, the #include "Hello.h" in main.cpp will be avariable.

The true thing is, this tell CMake where to find Hello.h when compile main.cpp.

But for some beginnners and expecially for the ones get used to Visual Studio, that will be quite abstract.

Take the example of Visual Studio again, if you do not set include path in property, can you open the head file in editer?

Of course not.

So, if you are using CLion in Ubuntu ( The same as other IDEs ) and you do not set target_include_directories(), you can not open the head file also.

That all.

原文地址:https://www.cnblogs.com/alexYuin/p/12772316.html