|
|
|
This page contains brief explanations of C++ and CMake. This page does not contain a (C++ || CMake || OpenCV ||CUDA) course or C++ guidelines. It will give you some intuition on what you must do if you want to start C++.
|
|
|
|
|
|
|
|
A cookie-cutter with a template for a basic CMake is available [here](templates/cmake-cookiecutter-for-image)
|
|
|
|
|
|
|
|
# C++
|
|
|
|
C++ is a language that must be compiled before running. To do so, the different project files must be given as input to a C++ compilator. This compilator will check for C++ syntax errors and link the different files to generate a single executable file.
|
|
|
|
|
|
|
|
The most trivial way to automatize the C++ compilation of a project is by using a *Makefile*. While writing a *Makefile* for simple projects is doable, it is time-consuming, redundant, highly boring, and error-prone for larger projects and beginners.
|
|
|
|
|
|
|
|
A modern and preferred way is by using *CMake*. *CMake* is a program that prepares and generates all necessary files (e.g., the *Makefile*) for the C++ compilation by using a *CMakelists.txt* file. The *CMakelists.txt* file has a light and simple syntax which makes it easier to write than *Makefile* (bash script syntax). So, *CMake* is not a C++ compilator but a program setting up the compilation for you. It's available on all platforms and is highly used in modern C++ projects.
|
|
|
|
|
|
|
|
# CMake and libraries
|
|
|
|
|
|
|
|
Imagine that you developed an image-processing library. To install it in Python, one must do
|
|
|
|
|
|
|
|
```
|
|
|
|
pip install opencv
|
|
|
|
```
|
|
|
|
|
|
|
|
In a Linux environment, one could install the C++ library by using *apt*. However, there is no *apt* equivalent on Windows. Hopefully and thanks to CMake, we do not need to get rid of Windows or spend a lot of time installing a C++ library (if the *CMakeLists* was well defined by the developer).
|
|
|
|
|
|
|
|
With CMake, it is possible to specify that you want to export your project as a library. Moreover, an important feature of CMake is the *Find_Lib.cmake* files. These files allow CMake to catch all the necessary information of a library to link it properly with your project. Therefore, linking your project with any library is a simple task. Here is a simple example of *CMakeLists.txt* which sets up a C++ project of one file that uses the OpenCV lib:
|
|
|
|
|
|
|
|
```cmake
|
|
|
|
#The project name
|
|
|
|
project(Wiki_example)
|
|
|
|
|
|
|
|
#Minimum cmake version
|
|
|
|
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
|
|
|
|
#Choose the version of Cpp
|
|
|
|
set(CMAKE_CXX_STANDARD 17)
|
|
|
|
set(CMAKE_VERBOSE_MAKEFILE ON)
|
|
|
|
|
|
|
|
#find the opencv lib
|
|
|
|
find_package(OpenCV)
|
|
|
|
|
|
|
|
#Create a variable with the name of the source files
|
|
|
|
set(SOURCES_FILES "src/main.cpp")
|
|
|
|
|
|
|
|
#Add an executable to the project
|
|
|
|
add_executable(${PROJECT_NAME} ${SOURCES_FILES} ${OpenCV_LIBS})
|
|
|
|
```
|
|
|
|
|
|
|
|
# Advanced readers
|
|
|
|
|
|
|
|
CMake is much more sophisticated than what we explained. For example, it is possible to change how the compilation is performed depending on the OS or developer choices, to define predefined macros to use them in the code, to change the number of bits of C++ types, etc., etc...
|
|
|
|
|
|
|
|
C++ is a difficult language to learn and takes years to master. If you are interested in this language, we encourage you to have a look at these:
|
|
|
|
|
|
|
|
- [LearnCpp](learncpp.com)
|
|
|
|
- [Cpp guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)
|
|
|
|
- [Cppcon](https://www.youtube.com/@CppCon)
|
|
|
|
|
|
|
|
# Common issues
|
|
|
|
|
|
|
|
Below is a list of common issues encountered when starting C++.
|
|
|
|
```json::table
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
# Other Ressources
|
|
|
|
|
|
|
|
- [Using cuda with cmake](https://cliutils.gitlab.io/modern-cmake/chapters/packages/CUDA.html) (modern way) |
|
|
|
\ No newline at end of file |