Skip to content

Exporting C++ Function to C Program

March 1, 2013

This article will describe how we can enable C++ function to be call in C language program. In order to accomplish this, we are require to implement a C++ interface function that follows standard C function convention. Below is the source code of C++ example program.

printf.cpp

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

void CPP_Printf(char *string)
{
    printf("C++: %s\n", string);
    //calling c++ routine here
}

int CPP_Add(int num1, int num2)
{
    //calling c++ routine here
    return num1 + num2;
}

printf.h

#ifndef _PRINTF_H
#define _PRINTF_H

#ifdef __cplusplus
extern "C" {
#endif

//extern "C" void CPP_Printf(char *string);
//extern "C" int CPP_Add(int num1, int num2);
extern void CPP_Printf(char *string);
extern int CPP_Add(int num1, int num2);

#ifdef __cplusplus
}
#endif

#endif //#ifndef _PRINTF_H

In the printf.cpp, we require to have the interface function (CPP_Printf, CPP_Add) between C++ and C language.

Once we have the interface function, we still have to modify on printf.h to include below syntax to avoid name mangling

#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif

Once both source file(cpp) and header file(h) is ready, we can proceed to build the object file(printf.o) as below:

g++ -c printf.c

Inspecting the object file with below command,

nm -g printf.o
0000001b T CPP_Add
00000000 T CPP_Printf
         U printf

From above, we can see the symbol is correctly ‘exported’ –  CPP_Add and CPP_Printf. If we do not include the syntax ‘extern “C” {}’ in the printf.h, below output will be observed. We can see the function name is not output correctly, it has been rename by C++ compiler.

00000000 T _Z10CPP_PrintfPc
0000001b T _Z7CPP_Addii
         U printf

Next, lets try construct a C code that call C++ interface function

cprintf.c

#include <stdio.h>
#include "printf.h" //c++ interface function

int main(void)
{
    int a,b,c;
    char str[100];

    a = 10;
    b = 5;

    c = CPP_Add(a,b);

    sprintf(str, "a=%d, b=%d, c=%d\n", a, b, c);
    CPP_Printf(str);

    return 0;
}

Compile the above code with print.o as below:

gcc cprintf.c ../cplusplus/printf.o -o cprintf

Execution of cprintf show working examples.

./cprintf 
C++: a=10, b=5, c=15

Final note to mention, the printf.o object file above can be convert to library file (print.a/print.so) and then can be static/dynamically link. More reference below:

  1. http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
  2. http://www.techytalk.info/c-cplusplus-library-programming-on-linux-part-one-static-libraries/
Advertisements

From → Language

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: