≡ Menu

How to Call C Function in C++, C++ Function in C (Mix C and C++)

There are times when it is required to mix the C and C++ code together. For example, while using a legacy C code or while using a specific C library the provides your C++ code with some specific functionality. So, some special steps are to be taken care of when using C code in C++ file or vice versa.

This article, through some examples discusses the steps required to mix C/C++ code.

1. Call C functions from C++

In this section we will discuss on how to call C functions from C++ code.

Here is the C code (Cfile.c):

#include <stdio.h>

void f(void)
{
    printf("\n This is a C code\n");
}

The first step is to create a library of this C code. The following steps create a shared library :

$ gcc -c -Wall -Werror -fPIC Cfile.c
$ gcc -shared -o libCfile.so Cfile.o

The shared library libCfile.so is produced as a result of above two commands.

Here is the main C++ code (main.cpp) :

#include <iostream>

extern "C" {
void f();
}

void func(void)
{
    std::cout<<"\n being used within C++ code\n";
}

int main(void)
{
    f();
    func();
    return 0;
}

The C function f() is declared within the notation extern “C” to tell the cpp compiler that it has C type linkage.

Now, compile the code (make sure that the shared library libCfile.so is linked to the code):

$ g++ -L/home/himanshu/practice/ -Wall main.cpp -o main -lCfile

Before running the executable make sure that the path of shared library is contain in the environment variable LD_LIBRARY_PATH.

$ export LD_LIBRARY_PATH=/home/himanshu/practice:$LD_LIBRARY_PATH

Now run the executable ‘main’ :

$ ./main

 This is a C code

 being used within C++ code

So we see that a C function was successfully called from a C++ code.

Also, read this for detailed information on how to create shared libraries in Linux.

2. Call C++ functions from C

In this section we will discuss on how to call C++ functions from C code.

Here is a C++ code (CPPfile.cpp) :

#include <iostream>

void func(void)
{
    std::cout<<"\n This is a C++ code\n";
}

We will see how the function func() can be called from a C code.

The first step for this is to change the declaration/definition of this function by introducing the notation extern “C”.

#include <iostream>

extern "C" void func(void)
{
    std::cout<<"\n This is a C++ code\n";
}

The next step is to create a library out of the code above. The following steps create a shared library:

 g++ -c -Wall -Werror -fPIC CPPfile.cpp
$ g++ -shared -o libCPPfile.so CPPfile.o

The above commands should result in libCPPfile.so shared library.

Here is main code in C language (main.c) :

#include <stdio.h>

extern void func(void);

void f(void)
{
    printf("\n being used within C code\n");
}

int main(void)
{
    func();
    f();
    return 0;
}

Please note that the C++ function is declared as extern here.

Compile the C code (main.c) like this:

gcc -L/home/himanshu/practice/ -Wall main.c -o main -lCPPfile

and add the current directory path to the environment variable LD_LIBRARY _PATH

export LD_LIBRARY_PATH=/home/himanshu/practice:$LD_LIBRARY_PATH

Now run the executable ‘main’ :

$ ./main

 This is a C++ code

 being used within C code

The output above shows that the C++ function was successfully called from C code.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • JohnP January 10, 2013, 8:37 am

    The key to understand for me was that C linkage is standard for the entire platform regardless of the compiler or linker used. Any C compiler can link to any C library regardless on the same platform.

    C++ linkage is extremely compiler/linker biased. This means that C++ libraries have to be created based on the C++ compiler used. Using a commercial C++ library created with a different compiler than you plan to use is not possible.

    I hope this is clear.

  • steve January 10, 2013, 8:32 pm

    Hello,

    Normally if you are writing C code you would normally do the following in the header file.
    ifdefine __cpluscplus
    extern “C”
    {
    #endif
    /* function declarations here */
    ifdefine __cplusplus
    }
    #endif

    I don’t think a c compiler will understand the extern “C” that is why you have to wrap it in a __cpluscplus macro.

    The second part calling C++ from C. You would normally create a wrapper that is compiled using a g++ compiler. Then your C code will call the functions that are wrapped in the c++ wrapper.

    Please comment on my above statement if I am incorrect, as I haven’t done this in a while. Maybe there are other tricks to doing this. I would be interested to know.

  • Júlio Hoffimann Mendes January 12, 2013, 3:43 pm

    As always, nice articles.

    Thanks,
    Júlio.

  • Vivek Kumar February 1, 2013, 2:28 am

    Hi,
    if CPP is not under

    extern “C”
    {
    }

    then it is not possible to use this CPP code in C!

    reasons??

  • DuskoKoscica February 8, 2013, 4:30 am

    If You have show to the people, how to call functions from C++, could you show Us how to call functions form other libraries, that are not C alike….

  • neha batra September 18, 2013, 1:08 am

    Is this concept “how to call c functions from c + +” can be Implemented using turbo c + + and borland compiler

  • Victor Moral October 29, 2014, 1:55 pm

    This is the only helpful post that explains the complete process compiling and linking c++ into c.
    And i’ve searched a lot!!
    Thanks!
    Many many thanks & best regards !!!

  • Clint Helton May 21, 2015, 10:51 am

    Great example!

    One thing… The line:
    g++ -L/home/himanshu/practice/ -Wall main.cpp -o main -lCfile

    …should be:
    g++ -Wall main.cpp -o main Cfile.o

    Your example would have worked if a Cfile.a library archive had been created in that folder.