Skip to content

Commit

Permalink
[cpp] Mix C & C++
Browse files Browse the repository at this point in the history
  • Loading branch information
luhuadong committed Nov 2, 2022
1 parent c73451a commit c4a224f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cpp/mix_c_cpp/cfunc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifdef __cplusplus
extern "C" {
#endif

int add(int a, int b)
{
return a+b;
}

#ifdef __cplusplus
}
#endif
17 changes: 17 additions & 0 deletions cpp/mix_c_cpp/cheader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _C_HEADER_
#define _C_HEADER_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h>

int add(int a, int b);

#ifdef __cplusplus
}
#endif

#endif
15 changes: 15 additions & 0 deletions cpp/mix_c_cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include "cheader.h"

using namespace std;

int main(void)
{
int a = 0;

a = add(2, 3);

cout << a << endl;

return 0;
}
5 changes: 5 additions & 0 deletions cpp/mix_c_cpp/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CC ?= gcc

all:
$(CC) main.cpp cfunc.c -lstdc++
g++ main.cpp cfunc.c

0 comments on commit c4a224f

Please sign in to comment.