cmake语法学习 - 01 Basic - B Hello headers

Posted alexyuin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cmake语法学习 - 01 Basic - B Hello headers相关的知识,希望对你有一定的参考价值。

# 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 to other IDEs ) and you do not set target_include_directories(), you can not open the head file also.

 

 

That all.

 

以上是关于cmake语法学习 - 01 Basic - B Hello headers的主要内容,如果未能解决你的问题,请参考以下文章

cmake语法学习 - 01 Basic - E- installing

cmake语法学习 - 01 Basic - D - shared - library

cmake 学习笔记

01-cmake语法-基本

cmake basic

[cmake] Basic Tutorial