mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 20:24:35 +03:00
108 lines
2.4 KiB
Plaintext
108 lines
2.4 KiB
Plaintext
:toc:
|
|
:toc-placement!:
|
|
|
|
toc::[]
|
|
|
|
[[hello-cmake]]
|
|
Hello CMake
|
|
-----------
|
|
|
|
Shows a very basic hello world example.
|
|
|
|
[[concepts]]
|
|
Concepts
|
|
~~~~~~~~
|
|
|
|
[[minimum-cmake-version]]
|
|
Minimum CMake version
|
|
^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
[source,cmake]
|
|
|
|
When creating a project using CMake, you can specify the minimum version
|
|
of CMake that is supported.
|
|
|
|
----
|
|
cmake_minimum_required(VERSION 2.6)
|
|
----
|
|
|
|
[[projects]]
|
|
Projects
|
|
^^^^^^^^
|
|
|
|
A CMake build can include a project name to make referencing certain
|
|
variables easier when using multiple projects.
|
|
|
|
[source,cmake]
|
|
----
|
|
project (hello_cmake)
|
|
----
|
|
|
|
[[creating-an-executable]]
|
|
Creating an Executable
|
|
^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
The +add_executable()+ command specifies that an executable should be
|
|
build from the specified source files, in this example main.cpp. The
|
|
first argument to the +add_executable()+ function is the name of the
|
|
executable to be built, and the second argument is the list of source files to compile.
|
|
|
|
[source,cmake]
|
|
----
|
|
add_executable(hello_cmake main.cpp)
|
|
----
|
|
|
|
|
|
[NOTE]
|
|
====
|
|
A shorthand that some people use is to have the project name and
|
|
executable name the same. This allows you to specify the CMakeLists.txt
|
|
as follows,
|
|
|
|
[source,cmake]
|
|
----
|
|
cmake_minimum_required(VERSION 2.6)
|
|
project (hello_cmake)
|
|
add_executable(${PROJECT_NAME} main.cpp)
|
|
----
|
|
|
|
In this example, the +project()+ function, will create a variable
|
|
+${PROJECT_NAME}+ with the value hello_cmake. This can then be passed to
|
|
the +add_executable()+ function to output a 'hello_cmake' executable.
|
|
====
|
|
|
|
[[building-the-example]]
|
|
Building the Example
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
[source,bash]
|
|
----
|
|
$ mkdir build
|
|
|
|
$ cd build
|
|
|
|
$ cmake ..
|
|
-- The C compiler identification is GNU 4.8.4
|
|
-- The CXX compiler identification is GNU 4.8.4
|
|
-- Check for working C compiler: /usr/bin/cc
|
|
-- Check for working C compiler: /usr/bin/cc -- works
|
|
-- Detecting C compiler ABI info
|
|
-- Detecting C compiler ABI info - done
|
|
-- Check for working CXX compiler: /usr/bin/c++
|
|
-- Check for working CXX compiler: /usr/bin/c++ -- works
|
|
-- Detecting CXX compiler ABI info
|
|
-- Detecting CXX compiler ABI info - done
|
|
-- Configuring done
|
|
-- Generating done
|
|
-- Build files have been written to: /workspace/cmake-examples/01-basic/hello_cmake/build
|
|
|
|
$ make
|
|
Scanning dependencies of target hello_cmake
|
|
[100%] Building CXX object CMakeFiles/hello_cmake.dir/hello_cmake.cpp.o
|
|
Linking CXX executable hello_cmake
|
|
[100%] Built target hello_cmake
|
|
|
|
$ ./hello_cmake
|
|
Hello CMake!
|
|
----
|