How to Set up a C++ Development Environment

There’s nothing worst than wanting to start a new project only to waste hours trying to get a dev. environment setup. In this post, I will be going through how to set up a C++ development environment that utilizes CMake to make building the project quick and easy.

Software Requirements

Although Git isn’t required, you should get it and start using it. Every software company uses a repository system, whether it’s Git, SVN, etc… Additionally, I’m using Visual Studios because it is yet another popular IDE that many many useful features. But, you do you!

Explaining the CMake in the Project Template

CMake can be very confusing at first (and many times after that), but once you’re used to it, it makes starting a new project so much more enjoyable! This is why I created the project template for anyone to use to get their new project up and running.

The Root CMakeLists.txt

This is the main CMake file that will set up the rest of the project’s build structure. The build.bat calls this and will also create a build directory to place all of the different solution files, libraries, .dlls, etc.

Here you can name the project and declare the version of C++ that is going to be used.

The add_subdirectory(<dir name>) will call the CMakeLists.txt in the specified directory and build that code. Furthermore, it’ll create that directory in the build directory to organize the solution structure even more.

# Minimum version of cmake required
cmake_minimum_required(VERSION 3.25)

# C++ Version
set(CMAKE_CXX_STANDARD 20)

# Set project name
project(
	project_name
	VERSION 1.0.0
	DESCRIPTION "Project Description"
)

# Set the projects root directory
message ("\n****************************************")
set(ROOT_DIR ${PROJECT_SOURCE_DIR})
message (" Project Name: ${PROJECT_NAME} \n Root Directory: ${ROOT_DIR}")
message ("****************************************")


# Add subdirectories
add_subdirectory(code)

The Code Directory CMakeLists.txt

The CMakeLists.txt within the code directory contains the project.cpp (which has our main function) as well as a Class, named ClassObject.

The set(projectSOURCES …) and set(projectHEADERS …) could have been named anything, but this makes the most sense and helps organize the CMake includes. The add_executable(${PROJECT_NAME} … includes the two previous declarations so that it can build them – this call is required.

Finally, we set the install directories for where the executables will be created!

# Minimum version of cmake required
cmake_minimum_required(VERSION 3.25)

# Set project name
project(
	module_name
	VERSION 1.0.0
	DESCRIPTION "Project Description"
)

# Set the projects root directory
message ("\n****************************************")
set(CODE_DIR ${PROJECT_SOURCE_DIR})
message (" Project Name: ${PROJECT_NAME} \n Root Directory: ${CODE_DIR}")
message ("****************************************")

# Set sources for project
set(projectSOURCES
	project.cpp
	
	ClassObject.cpp
)

# Set headers for project
set(projectHEADERS
	ClassObject.hpp
)

# Create executable target
add_executable(${PROJECT_NAME} 
	${projectSOURCES}
	${projectHEADERS}
)

# Set the install directory for the executable
install(TARGETS ${PROJECT_NAME}
	CONFIGURATIONS Debug
	RUNTIME DESTINATION Debug/bin
)
		
install(TARGETS ${PROJECT_NAME}
	CONFIGURATIONS Release
	RUNTIME DESTINATION Release/bin
)

Linking Libraries

insert here

Insert a target_link_libraries(${PROJECT_NAME} before the install calls to link third-party libraries into your project like SFML, OpenGL, Vulkan, etc.

The paths below can be hard coded to the .lib exactly, or you can declare a path:

set(ENV{SFML_DIR} $ENV{VENDOR_DIR}/sfml/SFML)

So then there is only one place to modify it – and you can even use environment variables.

# Link libraries
target_link_libraries(${PROJECT_NAME}
	$ENV{GLFW_DIR}/lib-vc2022/glfw3.lib
	$ENV{VULKAN_DIR}/Lib/vulkan-1.lib
	
	$ENV{SFML_DIR}/lib/Release/sfml-main.lib
	$ENV{SFML_DIR}/lib/Release/sfml-window.lib
	
	$ENV{RAKNET_DIR}/Lib/DLL/Debug/RakNetDLL.lib
)

Final Words

There are many ways to use CMake and it can get quite advanced. This is a simple method, so as to not overcomplicate things, but will still get you going with a well-organized dev. environment.

Frequently Asked Questions (FAQ)

Why use CMake?

CMake is a widely used cross-platform build system that helps simplify the process of building, configuring, and managing software projects. Overall, CMake simplifies the build process, improves cross-platform compatibility, and provides flexibility and scalability for managing software projects, making it a popular choice for many developers and organizations.

Benefits of using CMake

There are many benefits to choosing CMake as a build system:

  • Cross-platform compatibility: It generates native build files (such as Makefiles or project files for IDEs) for various platforms, including Windows, macOS, and Linux.
  • Build system abstraction: CMake provides an abstraction layer over different build systems, such as Make, Ninja, and Visual Studio. This means that developers can write CMake scripts once and generate build files for multiple build systems, allowing for flexibility and choice.
  • Modular and organized project structure: CMake promotes a modular and organized project structure by allowing developers to define targets, and dependencies, and build rules in a clear and concise manner.
  • Dependency management: CMake offers features to handle dependencies between libraries and external packages. It provides mechanisms to locate and link against required libraries, set compiler flags, and handle optional dependencies, making it easier to manage project dependencies and ensure that all required components are available for successful compilation.
  • Integration with IDEs: CMake integrates well with popular integrated development environments (IDEs) such as Visual Studio, Xcode, and CLion.
  • Flexibility and extensibility: CMake provides many commands, variables, and modules that can be used to customize the build process.
  • Community support and ecosystem: CMake has a large and active community, which means there are abundant resources, tutorials, and examples available.

Why use a code repository?

Code repositories, such as Git repositories, provide version control systems that track changes to source code over time, enabling developers to manage different versions and collaborate effectively. Code repositories facilitate teamwork, code backup, and disaster recovery.

Additionally, code repositories integrate well with CI/CD pipelines, support branching for experimentation, enhance documentation, and foster open-source collaboration.