Starting a C/C++ project can be as easy or as difficult as you want. Personally, I don’t like to fire up an IDE just for a small program. So, I end up using a text editor and compiling by terminal. However, the compilation process can get tedious. In this post, we will build a simple project template for a C/C++ program. This project will use the CMake tool to handle all the compilation process.
The first thing we need is a C/C++ compiler. Most Linux distribution will have GCC installed by default. To check if you have GCC install run:
1 2 3 4 5
$ gcc --version
gcc (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If you don’t get a similar message you need to install GCC. For Debian based distributions (Ubuntu, Linux Mint) you can install the build-essential package:
build-essential package includes GCC and other required packages. In case you are using Windows, you can install MinGW to install and setup GCC.
CMake
CMake is an open-source cross-platform project that provides a set of tools to build, test and install software. It uses configuration files to control the configuration process. The installation process should be easy, and for Debian bases distributions, you just need to run the command:
cpp-project-template/
├── CMakeLists.txt
├── include
├── src
│ └── main.cpp
CMakeLists.txt is the configuration file for CMake. We will put our header files on /include and our source files on /src.
CMake configuration file
Let’s see the first configuration file in more details:
CMakeLists.txt
1 2 3 4
cmake_minimum_required (VERSION 2.8)
set (PROY cpp-project-template)
project (${PROY} C CXX)
First, we need to set the minimum CMake version, in our case version 2.8. Then, we need to define a name for our project. Change the project name for one of your preference, the executable will be name after this.
CMakeLists.txt
1 2 3 4 5
# Source files folder
set (SRC_DIR src)
# Header files folder
set (INCL_DIR include)
Here we set the folders that contain our code. If you are going to create additional folders, make sure to create proper variables for those folders too. We will need these variables to include our files.
We can set our compilation flags in the CMAKE_CXX_FLAGS variable. For our project we will be using C++11 version. The -Wall flag enables all warning, and the -Werror flag treat warnings as errors.
CMakeLists.txt
1 2 3 4 5 6
include_directories (${PROJECT_SOURCE_DIR}/${INCL_DIR})
# Important: Include all source files on this list
set (SOURCE
${SRC_DIR}/main.cpp
)
include_directories command will include our headers to the build. Also, we need to fill in SOURCE variable with all of our source files. Otherwise, CMake will not compile our source files.
CMakeLists.txt
1 2 3 4 5 6
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")
add_executable (${PROY} ${SOURCE})
# Unit tests
add_subdirectory(tests)
CMAKE_RUNTIME_OUTPUT_DIRECTORY specifies where CMake will save our output files. In our case, executables will be on /bin folder. Finally, the add_executable command will create a executable file with our source files.
Using the template
Let’s define a Vector class. For simplicity, we are going to represent 2D vectors. Also, our class will implement some basic operations between vectors.
cmake_minimum_required (VERSION 2.8)
set (PROY math-vector-example)
project (${PROY} C CXX)
# Source files folder
set (SRC_DIR src)
# Header files folder
set (INCL_DIR include)
# Compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror")
include_directories (${PROJECT_SOURCE_DIR}/${INCL_DIR})
set (SOURCE
${SRC_DIR}/main.cpp
${SRC_DIR}/Vector.cpp
)
# Output folder
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")
add_executable (${PROY} ${SOURCE})
To compile our program, we will need to run these commands:
1 2 3 4
$ mkdir build
$ cd build
$ cmake ..
$ make
You only need to create the build/ folder once. The cmake .. command will generate the makefiles and make will build the program. If everything went all right, the executable will be saved in the bin/ folder. Let’s run our example:
Ever since the C++ language was first standardized, new and delete were defined as the methods to create/delete objects dynamically. The new operator allocates a memory block to construct an object and then calls the proper class’ constructor to initialize it. If successful, this operator will return a pointer to the location of the memory block. Otherwise, it will return nullptr or it will throw an exception. The delete operator executes the inverse operation, it deallocates object’s memory block.
Writing unit tests is a great way to ensure that your code is behaving correctly. But how can you tell how much of your code are you testing? The GCC compiler provides tools to analyze the testing coverage of your project. In this post, I will show you how to integrate these tools in a CMake project.
In a previous post, I showed you a C/C++ template that you can use for a project. I felt that it needed a basic testing framework. Therefore, we are going to learn how to install and use the Google Test framework to write tests. When we have finished this tutorial, we will have an executable that will run tests for our code.
Go is a simple and powerful programming language. Its syntax is familiar to C/C++ but it definately has improvements in comparison. It has a bunch of great features such as static types, memory safety, garbage collection, and it is targeted to concurrent programming. I discovered it very recently, so I want to start simple and learn the basics of this language. In this post, we will write our first Go program and library. Also, we will learn how to unit tests our Go programs.
In this post, I will describe a source-to-source compiler that transforms a Brainfuck program into its equivalent 64 bits assembly code. The assembly program can be built into a executable, thus allowing you to run Brainfuck programs natively.
Starting a C/C++ project can be as easy or as difficult as you want. Personally, I don’t like to fire up an IDE just for a small program. So, I end up using a text editor and compiling by terminal. However, the compilation process can get tedious. In this post, we will build a simple project template for a C/C++ program. This project will use the CMake tool to handle all the compilation process.