From ba0d09b6c42d6af7222dcdb9bbba276596bc3ce7 Mon Sep 17 00:00:00 2001 From: Athanasios Xygkis Date: Sun, 20 Sep 2020 20:38:36 +0200 Subject: [PATCH] Compile C with C++ files together --- template_cpp/src/CMakeLists.txt | 2 +- template_cpp/src/include/hello.h | 13 +++++++++++++ template_cpp/src/src/hello.c | 11 +++++++++++ template_cpp/src/src/main.cpp | 9 +++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 template_cpp/src/include/hello.h create mode 100644 template_cpp/src/src/hello.c diff --git a/template_cpp/src/CMakeLists.txt b/template_cpp/src/CMakeLists.txt index c1d306a..14756e8 100644 --- a/template_cpp/src/CMakeLists.txt +++ b/template_cpp/src/CMakeLists.txt @@ -1,7 +1,7 @@ # DO NAME THE SYMBOLIC VARIABLE `SOURCES` include_directories(include) -set(SOURCES src/main.cpp) +set(SOURCES src/main.cpp src/hello.c) # DO NOT EDIT THE FOLLOWING LINE find_package(Threads) diff --git a/template_cpp/src/include/hello.h b/template_cpp/src/include/hello.h new file mode 100644 index 0000000..b41ec3f --- /dev/null +++ b/template_cpp/src/include/hello.h @@ -0,0 +1,13 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +void hello(); + +#ifdef __cplusplus +} +#endif diff --git a/template_cpp/src/src/hello.c b/template_cpp/src/src/hello.c new file mode 100644 index 0000000..ecaf0d2 --- /dev/null +++ b/template_cpp/src/src/hello.c @@ -0,0 +1,11 @@ +#include "hello.h" + +void hello() { + printf("Source file that end in .c will be compiled automatically with gcc,\n" + "while files that end in .cpp will be compiled automatically with g++.\n" + "For those that prefer C, they can either use the provided C++\n" + "argument parser and write everything else in C, or delete the\n" + "provided main.cpp and start from scratch.\n" + "Make sure to have compatible ABI when calling C functions from cpp:\n" + "See `hello.h` on how to do this.\n"); +} diff --git a/template_cpp/src/src/main.cpp b/template_cpp/src/src/main.cpp index 52a5f16..c736a8c 100644 --- a/template_cpp/src/src/main.cpp +++ b/template_cpp/src/src/main.cpp @@ -4,8 +4,10 @@ #include "barrier.hpp" #include "parser.hpp" +#include "hello.h" #include + static void stop(int) { // reset signal handlers to default signal(SIGTERM, SIG_DFL); @@ -32,6 +34,9 @@ int main(int argc, char **argv) { Parser parser(argc, argv, requireConfig); parser.parse(); + hello(); + std::cout << std::endl; + std::cout << "My PID: " << getpid() << "\n"; std::cout << "Use `kill -SIGINT " << getpid() << "` or `kill -SIGTERM " << getpid() << "` to stop processing packets\n\n"; @@ -81,5 +86,9 @@ int main(int argc, char **argv) { std::cout << "Broadcasting messages...\n\n"; + while (true) { + std::this_thread::sleep_for(std::chrono::seconds(60)); + } + return 0; }