Linking C++ to C code using gcc

I encountered a strange problem recently. I had a C++ file which used certain custom system calls. The system calls had a few initialization functions to be performed before their usage. It was obvious that i needed a library for this. I made a static library which performed the initialization functions and which had to be linked with any C (or C++ until recently) file.

If you are not with me, here is an example:

  1. syscalls.h -> the header file with all the system calls/structures etc . this would of course be in /include or /include/linux directories
  2. using-syscalls.cpp -> a cpp file using the system calls in syscalls.h
  3. mylib.c ->the library file which had the necessary system call declarations of the form :
  4. syscallN(....)

    and some other initialization functions which need to be frequently called

  5. mylib.h -> a header file consisting of the function declaration used in mylib.c

Now the idea was to have using-syscalls.cpp link to a library created by mylib.c here’s how you can proceed.

  • obviously using-syscalls.cpp will have the includes of syscalls.h and mylib.h
  • yet when you create a static library say, you may get “implicit declarations……” of your custom system calls
  • It is necessary to create the library also with g++. Remember to use g++ only. the c++ compiler is more strict than the c compiler
  • so first create the library:

#g++ -Wall -c mylib.c
#ar -cvq libtest.a mylib.o

  • now for the linking. in your cpp file you must have an extern “C” declaration despite of all the includes. Surprisingly they do not conflict with the header files.So now in our cpp file we have :

extern "C"{
syscallN(....)
syscallN(....)
.......
}

  • once this is done, do the compiling and linking in one command (rather than creating the object file of the cpp first)

#g++ -o progname syscalls.cpp -L. -ltest

There…some Not-so-FAQ!

This entry was posted in linux, linux kernel, tutorials. Bookmark the permalink.

4 Responses to Linking C++ to C code using gcc

  1. basu says:

    Hi,
    I created a c library which has a exported method like void helloworld().Now in my cpp file i am calling this method.i have included header file and library.i declared the c method helloworld in my cpp file as
    extern “C”
    {
    void helloworld();
    }
    but still i am getting linking error.what may be the issue.thanks in advance

  2. divya says:

    @Basu

    Hi.. have you followed the steps that i have listed properly? mylib.h is a file you need to create…

    also i would be in a better position to help if you could be a little more descriptive…

  3. neha says:

    hi,
    plz state all the steps in clearly,so we can understood easily.
    thanks,

  4. riya roy says:

    i got the message ” Linking\…\….\TC\program.exe” what does it means?

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