Set C/C++ standard

Open in GitHub Codespaces

main1.c
#include <stddef.h>
#include <stdio.h>

int main() {
    int *int_ptr;
    printf("int_ptr == nullptr: %d\n", int_ptr == nullptr);
    return 0;
}
main2.cpp
#include <print>
 
int main() {
    std::println("Hello, World!");
    return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.30)

project(set-c-cxx-standard)

add_executable(main1 main1.c)
target_compile_features(main1 PRIVATE c_std_23)

add_executable(main2 main2.cpp)
target_compile_features(main2 PRIVATE cxx_std_23)
cmake -B ./build/
-- The C compiler identification is GNU 13.3.0
-- The CXX compiler identification is GNU 13.3.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 (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: /home/runner/work/cmakebyexample.jcbhmr.com/cmakebyexample.jcbhmr.com/src/set-c-cxx-standard/build
cmake --build ./build/
[ 25%] Building C object CMakeFiles/main1.dir/main1.c.o
[ 50%] Linking C executable main1
[ 50%] Built target main1
[ 75%] Building CXX object CMakeFiles/main2.dir/main2.cpp.o
/home/runner/work/cmakebyexample.jcbhmr.com/cmakebyexample.jcbhmr.com/src/set-c-cxx-standard/main2.cpp:1:10: fatal error: print: No such file or directory
    1 | #include <print>
      |          ^~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/main2.dir/build.make:79: CMakeFiles/main2.dir/main2.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:122: CMakeFiles/main2.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
./build/main1
int_ptr == nullptr: 0
./build/main2
sh: 1: ./build/main2: not found