mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 12:14:36 +03:00
updates to adoc and readd some missing files
This commit is contained in:
@@ -9,19 +9,39 @@ Hello CMake
|
||||
|
||||
Shows a very basic hello world example.
|
||||
|
||||
The files in this tutorial are below:
|
||||
|
||||
```
|
||||
A-hello-cmake$ tree
|
||||
.
|
||||
├── CMakeLists.txt
|
||||
├── main.cpp
|
||||
```
|
||||
|
||||
* CMakeLists.txt - Contains the CMake commands you wish to run
|
||||
* main.cpp - A simple "Hello World" cpp file.
|
||||
|
||||
[[concepts]]
|
||||
Concepts
|
||||
~~~~~~~~
|
||||
|
||||
|
||||
[[cmakelist-txt]]
|
||||
CMakeLists.txt
|
||||
^^^^^^^^^^^^^^
|
||||
|
||||
CMakeLists.txt is the file which should store all your CMake commands. When
|
||||
cmake is run in a folder it will look for this file and if it does not exist cmake
|
||||
will exit with an error.
|
||||
|
||||
[[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.
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
----
|
||||
@@ -71,6 +91,149 @@ In this example, the +project()+ function, will create a variable
|
||||
the +add_executable()+ function to output a 'hello_cmake' executable.
|
||||
====
|
||||
|
||||
[[out-of-source-builds]]
|
||||
Out-of-Source Builds
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
The root or top level folder that you run the cmake command from is known as your
|
||||
CMAKE_BINARY_DIR and is the root folder for all your binary files.
|
||||
CMake supports building and generating your binary files both in-place and also
|
||||
out-of-source.
|
||||
|
||||
In place builds generate all temporary build files in the same directory structure
|
||||
as your source code. This means that all Makefiles and object files are interspersed
|
||||
with your normal code. To create an in source build target run the cmake command
|
||||
in your root directory. For example:
|
||||
|
||||
```
|
||||
A-hello-cmake$ 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: /home/matrim/workspace/cmake-examples/01-basic/A-hello-cmake
|
||||
|
||||
A-hello-cmake$ tree
|
||||
.
|
||||
├── CMakeCache.txt
|
||||
├── CMakeFiles
|
||||
│ ├── 2.8.12.2
|
||||
│ │ ├── CMakeCCompiler.cmake
|
||||
│ │ ├── CMakeCXXCompiler.cmake
|
||||
│ │ ├── CMakeDetermineCompilerABI_C.bin
|
||||
│ │ ├── CMakeDetermineCompilerABI_CXX.bin
|
||||
│ │ ├── CMakeSystem.cmake
|
||||
│ │ ├── CompilerIdC
|
||||
│ │ │ ├── a.out
|
||||
│ │ │ └── CMakeCCompilerId.c
|
||||
│ │ └── CompilerIdCXX
|
||||
│ │ ├── a.out
|
||||
│ │ └── CMakeCXXCompilerId.cpp
|
||||
│ ├── cmake.check_cache
|
||||
│ ├── CMakeDirectoryInformation.cmake
|
||||
│ ├── CMakeOutput.log
|
||||
│ ├── CMakeTmp
|
||||
│ ├── hello_cmake.dir
|
||||
│ │ ├── build.make
|
||||
│ │ ├── cmake_clean.cmake
|
||||
│ │ ├── DependInfo.cmake
|
||||
│ │ ├── depend.make
|
||||
│ │ ├── flags.make
|
||||
│ │ ├── link.txt
|
||||
│ │ └── progress.make
|
||||
│ ├── Makefile2
|
||||
│ ├── Makefile.cmake
|
||||
│ ├── progress.marks
|
||||
│ └── TargetDirectories.txt
|
||||
├── cmake_install.cmake
|
||||
├── CMakeLists.txt
|
||||
├── main.cpp
|
||||
├── Makefile
|
||||
```
|
||||
|
||||
|
||||
Out-of-source builds allow you to create a single build folder that can be anywhere on
|
||||
your file system. All temporary build and object files are located in this directory keeping
|
||||
your source tree clean. To create an out-of-source build run the cmake command in
|
||||
the build folder and point it to the directory with your root CMakeLists.txt file.
|
||||
Using out-of-source builds if you want to recreate your cmake environment
|
||||
from scratch, you only need to delete your build directory and then rerun cmake.
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
A-hello-cmake$ mkdir build
|
||||
|
||||
A-hello-cmake$ cd build/
|
||||
|
||||
A-hello-cmake/build$ make ..
|
||||
make: Nothing to be done for `..'.
|
||||
matrim@freyr:~/workspace/cmake-examples/01-basic/A-hello-cmake/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: /home/matrim/workspace/cmake-examples/01-basic/A-hello-cmake/build
|
||||
|
||||
A-hello-cmake/build$ cd ..
|
||||
|
||||
A-hello-cmake$ tree
|
||||
.
|
||||
├── build
|
||||
│ ├── CMakeCache.txt
|
||||
│ ├── CMakeFiles
|
||||
│ │ ├── 2.8.12.2
|
||||
│ │ │ ├── CMakeCCompiler.cmake
|
||||
│ │ │ ├── CMakeCXXCompiler.cmake
|
||||
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
|
||||
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
|
||||
│ │ │ ├── CMakeSystem.cmake
|
||||
│ │ │ ├── CompilerIdC
|
||||
│ │ │ │ ├── a.out
|
||||
│ │ │ │ └── CMakeCCompilerId.c
|
||||
│ │ │ └── CompilerIdCXX
|
||||
│ │ │ ├── a.out
|
||||
│ │ │ └── CMakeCXXCompilerId.cpp
|
||||
│ │ ├── cmake.check_cache
|
||||
│ │ ├── CMakeDirectoryInformation.cmake
|
||||
│ │ ├── CMakeOutput.log
|
||||
│ │ ├── CMakeTmp
|
||||
│ │ ├── hello_cmake.dir
|
||||
│ │ │ ├── build.make
|
||||
│ │ │ ├── cmake_clean.cmake
|
||||
│ │ │ ├── DependInfo.cmake
|
||||
│ │ │ ├── depend.make
|
||||
│ │ │ ├── flags.make
|
||||
│ │ │ ├── link.txt
|
||||
│ │ │ └── progress.make
|
||||
│ │ ├── Makefile2
|
||||
│ │ ├── Makefile.cmake
|
||||
│ │ ├── progress.marks
|
||||
│ │ └── TargetDirectories.txt
|
||||
│ ├── cmake_install.cmake
|
||||
│ └── Makefile
|
||||
├── CMakeLists.txt
|
||||
├── main.cpp
|
||||
``
|
||||
|
||||
All examples in this tutorial will use out-of-source builds.
|
||||
|
||||
[[building-the-example]]
|
||||
Building the Example
|
||||
~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@@ -11,6 +11,24 @@ Hello CMake
|
||||
Shows a hello world example, which uses a different folder for source and include
|
||||
files.
|
||||
|
||||
The files in this tutorial are below:
|
||||
|
||||
```
|
||||
B-hello-headers$ tree
|
||||
.
|
||||
├── CMakeLists.txt
|
||||
├── inc
|
||||
│ └── Hello.h
|
||||
└── src
|
||||
├── Hello.cpp
|
||||
└── main.cpp
|
||||
```
|
||||
|
||||
* CMakeLists.txt - Contains the CMake commands you wish to run
|
||||
* inc/Hello.h - The header file to include
|
||||
* src/Hello.cpp - A source file to compile
|
||||
* src/main.cpp - The source file with main
|
||||
|
||||
[[concepts]]
|
||||
Concepts
|
||||
~~~~~~~~
|
||||
|
||||
10
01-basic/B-hello-headers/inc/Hello.h
Normal file
10
01-basic/B-hello-headers/inc/Hello.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef __HELLO_H__
|
||||
#define __HELLO_H__
|
||||
|
||||
class Hello
|
||||
{
|
||||
public:
|
||||
void print();
|
||||
};
|
||||
|
||||
#endif
|
||||
8
01-basic/B-hello-headers/src/Hello.cpp
Normal file
8
01-basic/B-hello-headers/src/Hello.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "Hello.h"
|
||||
|
||||
void Hello::print()
|
||||
{
|
||||
std::cout << "Hello Headers!" << std::endl;
|
||||
}
|
||||
8
01-basic/B-hello-headers/src/main.cpp
Normal file
8
01-basic/B-hello-headers/src/main.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "Hello.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Hello hi;
|
||||
hi.print();
|
||||
return 0;
|
||||
}
|
||||
50
README.adoc
50
README.adoc
@@ -1,17 +1,23 @@
|
||||
|
||||
= CMake Examples
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
[[cmake-examples]]
|
||||
cmake-examples
|
||||
--------------
|
||||
[[introduction]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
This includes some example cmake configurations which I have picked up
|
||||
This repository includes some example https://cmake.org/[CMake] configurations which I have picked up
|
||||
when exploring it's usage for various projects.
|
||||
|
||||
CMake is a cross-platform open-source build system which can build, test and package
|
||||
software. It can be used to support multiple native build environments including
|
||||
make, Apple's xcode and Microsoft Visual Studio.
|
||||
|
||||
These examples have been tested on Ubuntu 14.04 but should work under
|
||||
any Linux system that supports cmake.
|
||||
any Linux system that supports CMake.
|
||||
|
||||
[[requirements]]
|
||||
Requirements
|
||||
@@ -27,12 +33,38 @@ The easiest way to install the above on Ubuntu is as follows
|
||||
|
||||
[source,bash]
|
||||
----
|
||||
$ sudo apt-get install cmake
|
||||
$ sudo apt-get install build-essential
|
||||
$ sudo apt-get install cmake
|
||||
----
|
||||
|
||||
Some specific examples may require other tools including:
|
||||
|
||||
* boost `$ sudo apt-get libboost-all-dev`
|
||||
* protobuf `$ sudo apt-get install libprotobuf-dev`
|
||||
* cppcheck `$ sudo apt-get install cppcheck`
|
||||
* http://www.boost.org/[boost]
|
||||
|
||||
`$ sudo apt-get libboost-all-dev`
|
||||
|
||||
* https://github.com/google/protobuf[protobuf]
|
||||
|
||||
`$ sudo apt-get install libprotobuf-dev`
|
||||
|
||||
* http://cppcheck.sourceforge.net/[cppcheck]
|
||||
|
||||
`$ sudo apt-get install cppcheck`
|
||||
|
||||
[[other-links]]
|
||||
Other Links
|
||||
~~~~~~~~~~~
|
||||
|
||||
There are many CMake tutorials and examples online. The list below includes links
|
||||
to some of these which I have found helpful in my CMake journey.
|
||||
|
||||
* https://cmake.org/cmake-tutorial/[Official CMake Tutorial]
|
||||
* https://cmake.org/Wiki/Main_Page[Official CMake Wiki]
|
||||
* https://cmake.org/Wiki/CMake_Useful_Variables[CMake Useful Variables]
|
||||
* http://derekmolloy.ie/hello-world-introductions-to-cmake/[Derek Molloy - Intro to CMake]
|
||||
* http://techminded.net/blog/modular-c-projects-with-cmake.html[Modular C++ Projects]
|
||||
* http://voices.canonical.com/jussi.pakkanen/2013/03/26/a-list-of-common-cmake-antipatterns/[Common CMake Anti-Patterns]
|
||||
* http://baptiste-wicht.com/posts/2014/04/install-use-clang-static-analyzer-cmake.html[Using clang static analyser with CMake]
|
||||
* https://cmake.org/pipermail/cmake/2011-April/043709.html[Static Analysis with CDash] - Includes some info about using CppCheck with CMake
|
||||
* https://www.openfoundry.org/svn/cms/trunk/cmake/CppcheckTargets.cmake[CppCheck Targets]
|
||||
* https://samthursfield.wordpress.com/2015/10/20/some-cmake-tips/[CMake Tips]
|
||||
|
||||
Reference in New Issue
Block a user