Re: Linking a C program to a C++ library which uses STL
* Joe.pHsiao@gmail.com:
I looked at the C++ faq at http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html,
and wrote some simple code to experiment.
But in the end, the compiler complains about not recognize "extern".
[snip code]
What should I do to make it work?
As Ian Collins remarked else-thread, in order to make 'extern "C"'
compile you need to compile that as C++, because it's a C++ feature.
C++ supports C. C does not support C++.
And a special consequence of that is the very first thing mentioned in
the FAQ page you refer to above:
* You must use your C++ compiler when compiling main() (e.g., for
static initialization)
This means that your attempt to have a C main program that calls a C++
library is ill-fated from the beginning.
If you absolutely must keep the 'main' etc. in C then transform the C
'main' into an ordinary C function,
int cLanguageMain( int argc, char* argv[] ) { ... }
then make a C++ main program that calls that,
extern "C" int cLanguageMain( int argc, char* argv[] );
int main( int argc, char* argv[] )
{
return cLanguageMain( argc, argv );
}
and of course compile this C++ main with your C++ compiler.
That said, back to 'extern "C"'.
When used in a header file, the usual intent is that that header file is
to be used by both a C++ and a C compiler. Typically by a C compiler
when building the library or object file, and a C++ compiler when using
it. You have it opposite, but that doesn't matter: the point is that
you need to support both C and C++ compilers in the same file.
And that's discussed in FAQ item 32.4 on the same page you referred to
above.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?