mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 12:14:36 +03:00
Remove old md files and update asciidoc headers. Fix some cppcheck docs
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
= Hello CMake
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
[[hello-cmake]]
|
||||
Hello CMake
|
||||
-----------
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Shows a very basic hello world example.
|
||||
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
# Hello CMake
|
||||
|
||||
Shows a very basic hello world example.
|
||||
|
||||
## New Concepts
|
||||
|
||||
### Minimum CMake version
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
```
|
||||
|
||||
When creating a project using CMake, you can specify the minimum version of CMake that is supported.
|
||||
|
||||
|
||||
### Projects
|
||||
|
||||
```cmake
|
||||
project (hello_cmake)
|
||||
```
|
||||
|
||||
A CMake build can include a project name to make referencing certain variables when using multiple projects.
|
||||
|
||||
### Creating an executable
|
||||
|
||||
```cmake
|
||||
add_executable(hello_cmake main.cpp)
|
||||
```
|
||||
|
||||
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 source file from which to build the executable.
|
||||
|
||||
HINT: 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,
|
||||
|
||||
```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
|
||||
|
||||
```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!
|
||||
```
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
= Hello Headers
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
|
||||
[[hello-headers]]
|
||||
Hello Headers
|
||||
-------------
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Shows a hello world example which uses a different folder for source and include
|
||||
files.
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
# Hello CMake
|
||||
|
||||
Shows a hello world example, which uses a seperate cpp file and include directory.
|
||||
|
||||
## New Concepts
|
||||
|
||||
### Directory Paths
|
||||
|
||||
CMake syntax specifies a number of variables which can be used to help find useful directories in your project or source tree. Some of these include
|
||||
|
||||
Variable | Info
|
||||
-------- | ------
|
||||
CMAKE_SOURCE_DIR | The root source directory
|
||||
CMAKE_CURRENT_SOURCE_DIR | The current source directory if using sub-projects and directories
|
||||
PROJECT_SOURCE_DIR | The source director of the current cmake project.
|
||||
CMAKE_BINARY_DIR | The root binary / build directory. This is the directory you run the cmake command from
|
||||
CMAKE_CURRENT_BINARY_DIR | The build directory you are currently in.
|
||||
PROJECT_BINARY_DIR | The build directory for the current project.
|
||||
|
||||
### Including Directories
|
||||
|
||||
```
|
||||
include_directories(
|
||||
${PROJECT_SOURCE_DIR}/inc
|
||||
)
|
||||
```
|
||||
|
||||
When you have a seperate include folders, you can include them using the 'include_directories' function.
|
||||
This will add these directories to the compiler with the -I flag, e.g. -I/directory/path
|
||||
|
||||
### Setting Source files
|
||||
|
||||
```
|
||||
# Create a sources variable with a link to all cpp files to compile
|
||||
set(SOURCES
|
||||
src/Hello.cpp
|
||||
src/main.cpp
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
```
|
||||
|
||||
Creating a variable which includes the source files allows you to be clearer about these files and add them to various commands, for example, the 'add_executable()' function.
|
||||
|
||||
An alternative to setting specific file names in the SOURCES variable is to use a GLOB command to find files using wildcard pattern matching.
|
||||
|
||||
```
|
||||
file(GLOB SOURCES "src/*.cpp")
|
||||
```
|
||||
|
||||
## Building the example
|
||||
|
||||
```
|
||||
$ 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: /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build
|
||||
|
||||
$ make
|
||||
Scanning dependencies of target hello_headers
|
||||
[ 50%] Building CXX object CMakeFiles/hello_headers.dir/src/Hello.cpp.o
|
||||
[100%] Building CXX object CMakeFiles/hello_headers.dir/src/main.cpp.o
|
||||
Linking CXX executable hello_headers
|
||||
[100%] Built target hello_headers
|
||||
|
||||
$ ./hello_headers
|
||||
Hello Headers!
|
||||
```
|
||||
|
||||
### Verbose Output
|
||||
|
||||
In the previous examples, when running the make command the output only shows the status of the build. To see the full output for debugging purposes you can add 'VERBOSE=1' to the command to show all commands.
|
||||
|
||||
The VERBOSE output is show below, and looking at the output lets you see the include directories being added to the g++ command.
|
||||
|
||||
```
|
||||
$ make clean
|
||||
$ make VERBOSE=1
|
||||
/usr/bin/cmake -H/home/matrim/workspace/cmake-examples/01-basic/hello_headers -B/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build --check-build-system CMakeFiles/Makefile.cmake 0
|
||||
/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles/progress.marks
|
||||
make -f CMakeFiles/Makefile2 all
|
||||
make[1]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
make -f CMakeFiles/hello_headers.dir/build.make CMakeFiles/hello_headers.dir/depend
|
||||
make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
cd /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/matrim/workspace/cmake-examples/01-basic/hello_headers /home/matrim/workspace/cmake-examples/01-basic/hello_headers /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles/hello_headers.dir/DependInfo.cmake --color=
|
||||
make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
make -f CMakeFiles/hello_headers.dir/build.make CMakeFiles/hello_headers.dir/build
|
||||
make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 1
|
||||
[ 50%] Building CXX object CMakeFiles/hello_headers.dir/src/Hello.cpp.o
|
||||
/usr/bin/c++ -I/home/matrim/workspace/cmake-examples/01-basic/hello_headers/inc -o CMakeFiles/hello_headers.dir/src/Hello.cpp.o -c /home/matrim/workspace/cmake-examples/01-basic/hello_headers/src/Hello.cpp
|
||||
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 2
|
||||
[100%] Building CXX object CMakeFiles/hello_headers.dir/src/main.cpp.o
|
||||
/usr/bin/c++ -I/home/matrim/workspace/cmake-examples/01-basic/hello_headers/inc -o CMakeFiles/hello_headers.dir/src/main.cpp.o -c /home/matrim/workspace/cmake-examples/01-basic/hello_headers/src/main.cpp
|
||||
Linking CXX executable hello_headers
|
||||
/usr/bin/cmake -E cmake_link_script CMakeFiles/hello_headers.dir/link.txt --verbose=1
|
||||
/usr/bin/c++ CMakeFiles/hello_headers.dir/src/Hello.cpp.o CMakeFiles/hello_headers.dir/src/main.cpp.o -o hello_headers -rdynamic
|
||||
make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 1 2
|
||||
[100%] Built target hello_headers
|
||||
make[1]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 0
|
||||
```
|
||||
@@ -1,12 +1,14 @@
|
||||
= Static Library
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
|
||||
[[static-lib]]
|
||||
Static Library
|
||||
--------------
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Shows a hello world example which first creates and links a static library
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
= Shared Library
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
@@ -5,9 +6,9 @@
|
||||
toc::[]
|
||||
|
||||
|
||||
[[shared-lib]]
|
||||
Shared Library
|
||||
--------------
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Shows a hello world example which first creates and links a shared library
|
||||
|
||||
|
||||
Reference in New Issue
Block a user