C++ is a low-level programming language, primarily used for systems programming. Let's try creating a simple program using basic development tools.
- Prerequisite: C++ development environment setup
Creating the First C++ Program using cmake
If you are interested in doing cross-platform development in C++,
cmake
is a fantastic tool.
Although it is not "required",
we will use cmake
in all of our C++ projects on this site.
Here's a general process (albeit a bit simplified).
First create a project folder.
$ mkdir hello-world && cd $_
Create a CMakeLists.txt file:
cmake_minimum_required(VERSION 3.16)
project(HelloWorld VERSION 1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
configure_file(headers/hello-world.h.in headers/hello-world.h)
add_executable(hello hello-world.cpp)
target_include_directories(hello PRIVATE
headers/
)
target_include_directories(hello PUBLIC
"${PROJECT_BINARY_DIR}"
)
We will use the following simple hello-world.cpp
#include <iostream>
#include <stdlib.h>
#include "hello-world.h"
std::string hello_world::Greeter::greeting() {
return std::string("Hello, World!");
}
int main () {
hello_world::Greeter greeter;
std::cout << greeter.greeting() << std::endl;
return 0;
}
In the headers folder, we include the following files, for illustration.
hello-world.h
#include <string>
namespace hello_world {
class Greeter {
public:
std::string greeting();
};
}
hello-world.h.in
#define HelloWorld_VERSION_MAJOR @HelloWorld_VERSION_MAJOR@
#define HelloWorld_VERSION_MINOR @HelloWorld_VERSION_MINOR@
We use a separate directory for build.
We provide the project/source folder to the argument to the cmake
command:
$ cd .. && mkdir hello-world_build && cd $_
$ cmake ../hello-world
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/harry/Projects/codeandtips/code/cpp/hello-world_build
Then we build the project using the generated makefile:
$ cmake --build .
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/hello-world.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
Now, we can run the created executable:
$ ./hello
Hello, World!
Using Gradle
Gradle is a build and dependency management system, primarily used for Java and other JVM languages like Kotlin. Gradle also supports building C++ programs.
You can scaffold a simple C++ boilerplate app using gradle init
:
$ mkdir hello-world && cd $_
$ gradle init --type cpp-application --dsl kotlin --project-name "$_"
(You can just type gradle init
without arguments
to do this interactively.)
Let's try building and running the program.
$ ./gradlew run
BUILD SUCCESSFUL in 2s
6 actionable tasks: 6 executed
$ ./app/build/exe/main/debug/app
Hello, World!
C C# Clojure Crystal D Dart Erlang F# Go Haskell Java Javascript Julia Kotlin Lua Python Rust Scala Swift Typescript