user1051003
user1051003

Reputation: 1231

mixing c and c++

I have couple of questions that about mixing code:

  1. a complete project based on c, if I wanna use a c++ library, then I create a wrapper around it with pure c code,then build that shared library, do I have to change to g++ instead of gcc compiler?

  2. what if the wrapper compile as static library with the library?

Upvotes: 0

Views: 1266

Answers (1)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137547

I'm interested to know what library you're using that has only a C++ version, and not a pure C interface.

Regardless, Since you're going to be calling C++ code, your wrapper will be considered C++ and will need to be compiled with g++. This has to do with name-mangling, and you'll need to be able to call those functions the c++ library exposes. You can look at them with readelf -s.

The functions your wrapper exposes, however, will need to be marked extern "C" so that their names are not mangled. Then you will be able to call them from your pure C application.

Static vs. Shared library shouldn't really matter here. It's just a matter of the correct symbols being generated so that linking can happen.

Upvotes: 6

Related Questions