Cross-Platform C++ dev environment— Part 1: Basic Setup, get it building

Written by jodo | Published 2019/09/02
Tech Story Tags: cross-platform | c-dev | dev-environment | latest-tech-stories | cmake | google-test | visual-studio-code | visual-studio

TLDR The goal is to set up a full C++ development environment running on Linux and Windows. Using Visual Studio Code (Linux) and Visual Studio (Windows) as our editor and debugger and Google Test as our test framework. We will focus on the Linux part in this series. Part 1: Basic Setup, get it building with CMake. Part 2: Visual Studio code to debug (coming up) and Part 3: Testing with GoogleTest. Part 4: Building C++ code with Visual Studio to run and install our code.via the TL;DR App

The goal is to set up a full C++ development environment running on Linux and Windows using CMake as our build system, Visual Studio Code (Linux) / Visual Studio (Windows) as our editor and debugger and Google Test as our test framework. We will focus on the Linux part in this series.
Part 1: Basic Setup, get it building
Part 2: Visual Studio Code to debug (coming up)
Part 3: Testing with GoogleTest (coming up)

Sample Code

Let’s start by creating a src folder and put some sample code we can build and run in there. We will need a main.cpp with the main function. A simple hello world would be a bit boring so let's add at least one class that is adding up some numbers:
// main.cpp
#include "adder.h"
#include <iostream>


int main(){
  Adder adder = Adder();
  int added_value = adder.add(2, 4);

  std::cout << added_value << std::endl;

  return 0;
}


// adder.h

#ifndef Adder_H
#define Adder_H

class Adder
{
  public:
    int add(int, int);
};

#endif


// adder.cpp
#include "adder.h"

int Adder::add(int a, int b)
{
  return (a + b);
}

CMake, please build this

Our project will contain two CMake files. Our “main” CMake file within the root directory and one inside the src folder.
The main CMake file bundles our project together and sets some config variables such as the directory the executable should be installed to:
# CMakeLists.txt
cmake_minimum_required (VERSION 3.5)
project(CppStarter)

set(INSTALL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dist/${CMAKE_BUILD_TYPE}")

add_subdirectory(src)
As we add the src folder as a subdirectory, we should also add a CMakeLists.txt file there which tells our build system which files to compile and where to save the executables to:
# src/CMakeLists.txt
add_executable(${PROJECT_NAME} main.cpp adder.cpp)

install(TARGETS ${PROJECT_NAME} DESTINATION ${INSTALL_DIR})
And that’s it. With this setup we are ready to build and run our fancy cpp code:
>> mkdir build
>> cd build
>> cmake ..
>> make
>> make install
And to run the code:
>> cd dist/release
>> ./CppStarter
And since it is a bit tedious to type these commands every time, I’d recommend to hack them into a build script.
All the code can also be found here: https://github.com/j-o-d-o/cpp_starter/tree/part_1
On Windows, the >> cmake .. command will create a Visual Studio solution instead which can be used to build, run and install our code.

Published by HackerNoon on 2019/09/02