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_cmake)

# Add an executable
add_executable(hello_cmake main.cpp)

*

cmake_minium_required(VERSION 3.5)

- Set a lowest version limitation for user.

- VERSION must be upper case.

- 3.5 is the version of CMake.

*

project(hello_cmake)

- Name you project. I want to use the example of Visual Studio to give you a better understanding.

In Visual Studio, all things are belong to a solution. Yes, that is the *.sln file.

When you want to start a project, actually the first file to be created is the solution file.

After that, you can new some your projects to the solution. Like that:

|___Solution

        |_____Project 1

        |_____Project 2

But in CMake, there is not a thing called solution, only several peojects.

So, how can CMake know where is the “solution (The root project)”, where are the "projects(The sub projects)" ?

The answer is in the folders. Here is a the CMakeList.txt example of (02-sub-projects).

|___CMakeList.txt ( ./02-sub-projects/A-basic ) (“Solution”)

        |_____CMakeList.txt ( ./02-sub-projects/A-basic/subbinary ) ("Project")

        |_____CMakeList.txt ( ./02-sub-projects/A-basic/sublibrary1 )
        |_____CMakeList.txt ( ./02-sub-projects/A-basic/sublibrary2 )

project() is used to name your project (Both solution and project. If you are familar with Visual Studio.)

And the working scope is within the folder where it is.

*

add_executable(hello_cmake main.cpp)

- hello_cmake is the name of the output executable file.

- main.cpp is the file

There is a tip, please keep the "hello_cmake" here the same as the name you set in project(). 

For skillfull user, they will write as "add_executable(${PROJECT_NAME} main.cpp)"

PROJECT_NAME is a variable kept by CMake. The value is the name set by project().

That all.

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