add initial folder structure and the first basic examples
This commit is contained in:
parent
6c3c4c8d1d
commit
337cdeaa94
55
.gitignore
vendored
55
.gitignore
vendored
@ -3,3 +3,58 @@ CMakeFiles
|
||||
Makefile
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
|
||||
#
|
||||
# Sublime Test
|
||||
#
|
||||
# cache files for sublime text
|
||||
*.tmlanguage.cache
|
||||
*.tmPreferences.cache
|
||||
*.stTheme.cache
|
||||
|
||||
# workspace files are user-specific
|
||||
*.sublime-workspace
|
||||
|
||||
# project files should be checked into the repository, unless a significant
|
||||
# proportion of contributors will probably not be using SublimeText
|
||||
# *.sublime-project
|
||||
|
||||
#
|
||||
# C / C++
|
||||
#
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
*.ko
|
||||
*.elf
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
*.so.*
|
||||
*.dylib
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
*.lo
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
/**/build
|
10
01-basic/A-hello-cmake/CMakeLists.txt
Normal file
10
01-basic/A-hello-cmake/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
# Set the minimum version of CMake that can be used
|
||||
# To find the cmake version run
|
||||
# $ cmake --version
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
|
||||
# Set the project name
|
||||
project (hello_cmake)
|
||||
|
||||
# Add an executable
|
||||
add_executable(hello_cmake main.cpp)
|
73
01-basic/A-hello-cmake/README.md
Normal file
73
01-basic/A-hello-cmake/README.md
Normal file
@ -0,0 +1,73 @@
|
||||
# Hello CMake
|
||||
|
||||
Shows a very basic hello world example.
|
||||
|
||||
## New Concepts
|
||||
|
||||
### Minimum CMake version
|
||||
|
||||
```
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
```
|
||||
|
||||
When creating a project using CMake, you can specify the minimum version of CMake that is supported.
|
||||
|
||||
|
||||
### Projects
|
||||
|
||||
```
|
||||
project (hello_cmake)
|
||||
```
|
||||
|
||||
A CMake build can include a project name to make referencing certain variables when using multiple projects.
|
||||
|
||||
### Creating an executable
|
||||
|
||||
```
|
||||
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_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
|
||||
|
||||
```
|
||||
$ 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!
|
||||
```
|
||||
|
7
01-basic/A-hello-cmake/main.cpp
Normal file
7
01-basic/A-hello-cmake/main.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
std::cout << "Hello CMake!" << std::endl;
|
||||
return 0;
|
||||
}
|
23
01-basic/B-hello-headers/CMakeLists.txt
Normal file
23
01-basic/B-hello-headers/CMakeLists.txt
Normal file
@ -0,0 +1,23 @@
|
||||
# Set the minimum version of CMake that can be used
|
||||
# To find the cmake version run
|
||||
# $ cmake --version
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
|
||||
# Set the project name
|
||||
project (hello_headers)
|
||||
|
||||
# Set the direcoties that should be included in the build command
|
||||
# when running g++ these will be included as -I/directory/path/
|
||||
include_directories(
|
||||
${PROJECT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
# 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(${PROJECT_NAME} ${SOURCES})
|
117
01-basic/B-hello-headers/README.md
Normal file
117
01-basic/B-hello-headers/README.md
Normal file
@ -0,0 +1,117 @@
|
||||
# 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
|
||||
```
|
36
01-basic/C-static-library/CMakeLists.txt
Normal file
36
01-basic/C-static-library/CMakeLists.txt
Normal file
@ -0,0 +1,36 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
|
||||
project(hello_library)
|
||||
|
||||
include_directories(
|
||||
${PROJECT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
############################################################
|
||||
# Create a library
|
||||
############################################################
|
||||
|
||||
# Source files to be used in the library
|
||||
set(library_SOURCES
|
||||
src/Hello.cpp
|
||||
)
|
||||
|
||||
#Generate the static library from the library sources
|
||||
add_library(hello_library STATIC ${library_SOURCES})
|
||||
|
||||
############################################################
|
||||
# Create an executable
|
||||
############################################################
|
||||
|
||||
# Source fles for the binary
|
||||
set(binary_SOURCES
|
||||
src/main.cpp
|
||||
)
|
||||
|
||||
# Add an executable with the above sources
|
||||
add_executable(hello_binary ${binary_SOURCES})
|
||||
|
||||
# link the new hello_library target with the hello_binary target
|
||||
target_link_libraries( hello_binary
|
||||
hello_library
|
||||
)
|
0
01-basic/C-static-library/README.md
Normal file
0
01-basic/C-static-library/README.md
Normal file
10
01-basic/C-static-library/inc/Hello.h
Normal file
10
01-basic/C-static-library/inc/Hello.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef __HELLO_H__
|
||||
#define __HELLO_H__
|
||||
|
||||
class Hello
|
||||
{
|
||||
public:
|
||||
void print();
|
||||
};
|
||||
|
||||
#endif
|
8
01-basic/C-static-library/src/Hello.cpp
Normal file
8
01-basic/C-static-library/src/Hello.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "Hello.h"
|
||||
|
||||
void Hello::print()
|
||||
{
|
||||
std::cout << "Hello Static Library!" << std::endl;
|
||||
}
|
8
01-basic/C-static-library/src/main.cpp
Normal file
8
01-basic/C-static-library/src/main.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include "Hello.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Hello hi;
|
||||
hi.print();
|
||||
return 0;
|
||||
}
|
36
01-basic/D-shared-library/CMakeLists.txt
Normal file
36
01-basic/D-shared-library/CMakeLists.txt
Normal file
@ -0,0 +1,36 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
|
||||
project(hello_library)
|
||||
|
||||
include_directories(
|
||||
${PROJECT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
############################################################
|
||||
# Create a library
|
||||
############################################################
|
||||
|
||||
# Source files to be used in the library
|
||||
set(library_SOURCES
|
||||
src/Hello.cpp
|
||||
)
|
||||
|
||||
#Generate the shared library from the library sources
|
||||
add_library(hello_library SHARED ${library_SOURCES})
|
||||
|
||||
############################################################
|
||||
# Create an executable
|
||||
############################################################
|
||||
|
||||
# Source fles for the binary
|
||||
set(binary_SOURCES
|
||||
src/main.cpp
|
||||
)
|
||||
|
||||
# Add an executable with the above sources
|
||||
add_executable(hello_binary ${binary_SOURCES})
|
||||
|
||||
# link the new hello_library target with the hello_binary target
|
||||
target_link_libraries( hello_binary
|
||||
hello_library
|
||||
)
|
0
01-basic/D-shared-library/README.md
Normal file
0
01-basic/D-shared-library/README.md
Normal file
10
01-basic/D-shared-library/inc/Hello.h
Normal file
10
01-basic/D-shared-library/inc/Hello.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef __HELLO_H__
|
||||
#define __HELLO_H__
|
||||
|
||||
class Hello
|
||||
{
|
||||
public:
|
||||
void print();
|
||||
};
|
||||
|
||||
#endif
|
8
01-basic/D-shared-library/src/Hello.cpp
Normal file
8
01-basic/D-shared-library/src/Hello.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "Hello.h"
|
||||
|
||||
void Hello::print()
|
||||
{
|
||||
std::cout << "Hello Shared Library!" << std::endl;
|
||||
}
|
8
01-basic/D-shared-library/src/main.cpp
Normal file
8
01-basic/D-shared-library/src/main.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include "Hello.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Hello hi;
|
||||
hi.print();
|
||||
return 0;
|
||||
}
|
10
01-basic/README.md
Normal file
10
01-basic/README.md
Normal file
@ -0,0 +1,10 @@
|
||||
# Basic Examples
|
||||
|
||||
The basic examples in this directory show how the setup a cmake project and create an executable. The examples included are
|
||||
|
||||
- hello-cmake. A hello world example.
|
||||
- hello-headers. A slighly more complicated hello world example, with using Hello class and seperate source and include folders.
|
||||
- shared-library. An example using a shared library.
|
||||
- static-library. An example using a static library.
|
||||
- installing. Shows how to create a 'make install' target to install the binaries and libraries
|
||||
- build-type. An example showing how to set a default build type of your project.
|
1
02-sub-projects/README.md
Normal file
1
02-sub-projects/README.md
Normal file
@ -0,0 +1 @@
|
||||
Examples showing how to create sub-projects in different directories
|
3
03-code-generation/README.md
Normal file
3
03-code-generation/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
Examples showing code generation using various tools.
|
||||
|
||||
Also shows how to move variables from CMAKE into source files (configure_file).
|
1
04-static-analysis/README.md
Normal file
1
04-static-analysis/README.md
Normal file
@ -0,0 +1 @@
|
||||
How to call static analysis tools
|
1
04-static-analysis/cppcheck/README.md
Normal file
1
04-static-analysis/cppcheck/README.md
Normal file
@ -0,0 +1 @@
|
||||
Example of calling the cppcheck tool
|
35
README.md
35
README.md
@ -1,2 +1,35 @@
|
||||
# cmake-examples
|
||||
This includes some example cmake configurations which I have picked up when exploring it's usage for various projects
|
||||
This includes some example cmake configurations which I have picked up when exploring it's usage for various projects.
|
||||
|
||||
These examples have been tested on Ubuntu 14.04 but should work under any Linux system that supports cmake.
|
||||
|
||||
## Requirements
|
||||
|
||||
The basic requirements for most examples are:
|
||||
|
||||
* CMake
|
||||
* A c++ compiler (defaults to gcc)
|
||||
* make
|
||||
|
||||
The easiest way to install the above on Ubuntu is as follows
|
||||
|
||||
```
|
||||
$ sudo apt-get install cmake
|
||||
$ sudo apt-get install build-essential
|
||||
```
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
|
8
cmake-examples.sublime-project
Normal file
8
cmake-examples.sublime-project
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"folders":
|
||||
[
|
||||
{
|
||||
"path": "."
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user