// sample-main.cpp: a C++ program that uses both sample headers. // Free sample from 99helpers.com/file-types/h // // sample-header.h belongs to a C library, so compile its .c file with a C // compiler, then link the object file into the C++ program: // cc -Wall -Wextra -std=c17 -c sample-header.c // c++ -Wall -Wextra -std=c++20 sample-main.cpp sample-header.o -o stats-cpp // ./stats-cpp // sample-header-only.hpp needs no build step at all. #include #include #include #include "sample-header-only.hpp" #include "sample-header.h" // its extern "C" block makes the C functions link int main() { const std::vector temperatures{12.5, 14.0, 9.5, 17.25, 15.0, 11.75}; // The C library, called from C++. stats_t week; stats_init(&week); for (double t : temperatures) { stats_add(&week, t); } std::cout << stats_library_name << " (C++)\n" << std::fixed << std::setprecision(2) << stats_count(&week) << " values, mean " << stats_mean(&week) << '\n'; // The header-only C++ library. std::cout << sample::library_name << ", average " << sample::average(temperatures) << '\n'; for (double t : temperatures) { std::cout << std::setw(6) << t << ' ' << sample::bar(t, stats_max(&week)) << '\n'; } }