1
0

Compile C with C++ files together

This commit is contained in:
Athanasios Xygkis 2020-09-20 20:38:36 +02:00 committed by Thanasis Xigis
parent e4f3239a37
commit ba0d09b6c4
4 changed files with 34 additions and 1 deletions

View File

@ -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)

View File

@ -0,0 +1,13 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
void hello();
#ifdef __cplusplus
}
#endif

View File

@ -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");
}

View File

@ -4,8 +4,10 @@
#include "barrier.hpp"
#include "parser.hpp"
#include "hello.h"
#include <signal.h>
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;
}