Stephane Rolland
Stephane Rolland

Reputation: 39906

linking C++ library so as it is callable as C

I am really not an expert at C/C++ Linking magic. g++ and Cygwin neither, rather a novice indeed.

Imagine I have an executable, (Apache Server in my case), which accepts C library as modules. e.g. compiled file libMyServer.so

If I do the guts coding in C++, and export only vanilla functions, for example static member functions of a class I'd call CExportToC... if I do this will the linking magic make my library libMyServer reachable from a program requesting C libraries ?

Upvotes: 0

Views: 118

Answers (1)

user395760
user395760

Reputation:

You don't do that at link time, you declare to the compiler not to mangle names when generating code (which of course disallows overloads). Put the declarations of the things you want to expose to C into an extern "C" { ... } block. See e.g. the FAQ. Classes are pretty much out of luck here, unless you're willing to do a lot of work and live with the PITA. And even then, a single compiler writer can break your code (in ways that will only show in runtime bugs, not in compile errors).

Upvotes: 2

Related Questions