Skip to content

Build multiple targets

cmake
cmake_minimum_required(VERSION 3.28)
project(my-project)
add_executable(hello-world hello-world.c)
add_executable(goodbye-world goodbye-world.cpp)
add_library(greet STATIC greet.c)
add_library(greet SHARED greet.c)
c
#include <stdio.h>
int main() {
  puts("Hello world!");
  return 0;
}
cpp
#include <iostream>
int main() {
  std::cout << "Goodbye world!\n";
  return 0;
}
c
#include <stdio.h>
void greet(char *name) { printf("Hello %s!\n", name); }
sh
cmake -B build
cmake --build build

# It built four things! 🎉
./build/hello-world
./build/goodbye-world
file ./build/greet.a
file ./build/greet.so