Mixing c++ and c code during compilation

TL;DR: use extern "C" for calling C++ functions from C code and compile using gcc -lstdc++

Suppose that you need to use some C++ code in your C environment and translating this legacy code is not an option. That is, C is your principal language and you need some libraries in C++. In this post, I will show you how to compile C++ and C sources togheter You can use both languages and link the object files in a single executable. Consider the following example with three source files:

/*
| legacy_code.h
| 
| Interface between legacy code (c++) and an external (c) program.
| If your program need c++ libraries, such as iostream, include them in legacy_code.cpp
| 
| Things to consider:
| 
| 1) Note that both c and c++ programs could read this file, thus this file need to be 'readable' or compatible with respect to both languages.
| For that reason, dont put things like 
|   
|   #include <iostream>  
|   using namespace std;
|   cout << var;
| 
| 2) Put the following block before each function header
| #ifdef __cplusplus
|   extern "C"
| #endif
*/

#ifndef LEGACY_CODE_H
#define LEGACY_CODE

// printer for int
#ifdef __cplusplus
extern "C"
#endif
void print_int (int a);

// printer for double
#ifdef __cplusplus
extern "C"
#endif
void print_double(double a);

#endif
/*
| legacy_code.cpp
|
| All your legacy code (c++ functions) are here. 
| 
| Two things to notice in this file
| 
| 1) The header of every function must be accessible from c, that is
| 
|   void print_vector ( int *v )        it is OK
|   void print_vector ( vector<int> v ) NOT OK
| 
| 2) If you need some c++ library, such as iostream, put it here, not in legacy_code.h
| 
*/

// -- c++ libraries here --
#include <iostream>
// -- end c++ libreries --

#include "legacy_code.h"


void print_int (int a) {
    std::cout << "integer " << a << std::endl;   
}

void print_double(double a) {
    std::cout << "double " << a << std::endl;    
}


/*
| main.c
| 
| This code will use c++ functions trough the legacy_code.h interface.
| 
| how to compile (note the -lstdc++ flag)
| $ g++ -c legacy_code.cpp
| $ gcc -c main.c 
| $ gcc legacy_code.o main.o -o program -lstdc++
*/

#include <stdio.h>
#include "legacy_code.h"

int main () {
 
    // we are using c++ functions right here
    print_int(10);
    print_double(2.5);
 
    return 0;   
}

# makefile
all: 
	g++ -c legacy_code.cpp
	gcc -c main.c 
	gcc legacy_code.o main.o -o program.out -lstdc++
	
clean:
	rm *.o *.out

Finally, compile these files using make or the following commands:

g++ -c legacy_code.cpp
gcc -c main.c
gcc legacy_code.o main.o -o program -lstdc++

Stackoverflow
Oracle

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s