Variables

Filename: CMakeLists.txt

cmake_minimum_required(VERSION 3.31) project(variables LANGUAGES C) message("CMAKE_SOURCE_DIR=${CMAKE_SOURCE_DIR}") # "/workspace" or similar message("CMAKE_BINARY_DIR=${CMAKE_BINARY_DIR}") # "/workspace/build" or similar message("CMAKE_C_COMPILER=${CMAKE_C_COMPILER}") # "/usr/bin/cc" or similar message("CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}") # "/usr/bin/c++" or similar message("CMAKE_AR=${CMAKE_AR}") # "/usr/bin/ar" or similar message("CMAKE_RANLIB=${CMAKE_RANLIB}") # "/usr/bin/ranlib" or similar message("PROJECT_NAME=${PROJECT_NAME}") # "variables" message("CMAKE_HOST_UNIX=${CMAKE_HOST_UNIX}") # TRUE if the host system is UNIX-like message("CMAKE_WIN32=${CMAKE_WIN32}") # TRUE if the target system is Windows set(MY_BOOL ON) # BOOL type set(MY_EXE_NAME "variables") # STRING type set(MY_SOURCE_FILES "./src/main.c" "./src/stats.c") # list type add_executable("${MY_EXE_NAME}" ${MY_SOURCE_FILES})

Filename: src/main.c

#include <stdio.h> #include "./stats.h" int main() { printf("Hello, %s!\n", "World"); printf("The average of 1, 2, and 3 is %f.\n", average_of_three(1, 2, 3)); return 0; }

Filename: src/stats.c

#include "./stats.h" double average_of_three(double a, double b, double c) { return (a + b + c) / 3; }

Filename: src/stats.h

#pragma once double average_of_three(double a, double b, double c);
$ 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 CMAKE_SOURCE_DIR=/workspaces/cmakebyexample.jcbhmr.com/listings/03-variables CMAKE_BINARY_DIR=/workspaces/cmakebyexample.jcbhmr.com/listings/03-variables/build CMAKE_C_COMPILER=/usr/bin/cc CMAKE_CXX_COMPILER=/usr/bin/c++ CMAKE_AR=/usr/bin/ar CMAKE_RANLIB=/usr/bin/ranlib PROJECT_NAME=variables CMAKE_HOST_UNIX=1 CMAKE_WIN32= -- Configuring done (0.4s) -- Generating done (0.0s) -- Build files have been written to: /workspaces/cmakebyexample.jcbhmr.com/listings/03-variables/build $ cmake --build ./build/ [ 33%] Building C object CMakeFiles/variables.dir/src/main.c.o [ 66%] Building C object CMakeFiles/variables.dir/src/stats.c.o [100%] Linking C executable variables [100%] Built target variables $ ./build/variables Hello, World! The average of 1, 2, and 3 is 2.000000.