From c4a224fc634d08603e9af1cdab51f129917f4092 Mon Sep 17 00:00:00 2001 From: luhuadong Date: Thu, 3 Nov 2022 01:53:58 +0800 Subject: [PATCH] [cpp] Mix C & C++ --- cpp/mix_c_cpp/cfunc.c | 12 ++++++++++++ cpp/mix_c_cpp/cheader.h | 17 +++++++++++++++++ cpp/mix_c_cpp/main.cpp | 15 +++++++++++++++ cpp/mix_c_cpp/makefile | 5 +++++ 4 files changed, 49 insertions(+) create mode 100644 cpp/mix_c_cpp/cfunc.c create mode 100644 cpp/mix_c_cpp/cheader.h create mode 100644 cpp/mix_c_cpp/main.cpp create mode 100644 cpp/mix_c_cpp/makefile diff --git a/cpp/mix_c_cpp/cfunc.c b/cpp/mix_c_cpp/cfunc.c new file mode 100644 index 0000000..a1b465b --- /dev/null +++ b/cpp/mix_c_cpp/cfunc.c @@ -0,0 +1,12 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int add(int a, int b) +{ + return a+b; +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/cpp/mix_c_cpp/cheader.h b/cpp/mix_c_cpp/cheader.h new file mode 100644 index 0000000..97a8f27 --- /dev/null +++ b/cpp/mix_c_cpp/cheader.h @@ -0,0 +1,17 @@ +#ifndef _C_HEADER_ +#define _C_HEADER_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +int add(int a, int b); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/cpp/mix_c_cpp/main.cpp b/cpp/mix_c_cpp/main.cpp new file mode 100644 index 0000000..b057133 --- /dev/null +++ b/cpp/mix_c_cpp/main.cpp @@ -0,0 +1,15 @@ +#include +#include "cheader.h" + +using namespace std; + +int main(void) +{ + int a = 0; + + a = add(2, 3); + + cout << a << endl; + + return 0; +} \ No newline at end of file diff --git a/cpp/mix_c_cpp/makefile b/cpp/mix_c_cpp/makefile new file mode 100644 index 0000000..0427ff2 --- /dev/null +++ b/cpp/mix_c_cpp/makefile @@ -0,0 +1,5 @@ +CC ?= gcc + +all: + $(CC) main.cpp cfunc.c -lstdc++ + g++ main.cpp cfunc.c \ No newline at end of file