Executable

Filename: CMakeLists.txt

cmake_minimum_required(VERSION 3.31)

project(executable LANGUAGES C CXX)

add_executable(hello-world ./src/bin/hello-world.c)

add_executable(goodbye-world ./src/bin/goodbye-world.cpp)

Filename: src/bin/hello-world.c

#include <stdio.h>

int main() {
    printf("Hello, %s!\n", "World");
    return 0;
}

Filename: src/bin/goodbye-world.cpp

#include <iostream>

int main() {
    std::cout << "Goodbye, " << "World" << "!\n";
    return 0;
}
$ cmake -B ./build/
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (5.6s)
-- Generating done (0.0s)
-- Build files have been written to: /workspaces/cmakebyexample.jcbhmr.com/listings/02-executable/build
$ cmake --build ./build/
[ 25%] Building C object CMakeFiles/hello-world.dir/src/bin/hello-world.c.o
[ 50%] Linking C executable hello-world
[ 50%] Built target hello-world
[ 75%] Building CXX object CMakeFiles/goodbye-world.dir/src/bin/goodbye-world.cpp.o
[100%] Linking CXX executable goodbye-world
[100%] Built target goodbye-world
$ ./build/hello-world
Hello, World!
$ ./build/goodbye-world
Goodbye, World!