码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-04-25 13:10:15      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:var   get   nes   fir   cto   example   include   director   directory   

# 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

标签:var   get   nes   fir   cto   example   include   director   directory   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!