Discussion:
extern "C" and undefined reference
(too old to reply)
Andrew Pantyukhin
2007-04-22 21:54:47 UTC
Permalink
7zip developers converted some code from C++ to C,
while leaving the main stand-alone lzma app in C++.
They use 'extern "C" { }' blocks around #include's
referencing C headers.

Everything compiles fine, but "undefined reference"
errors appear at linkage. The undefined references
are to the C functions included from withing those
'extern "C"' wrappers. I tried to remove the
wrappers from some files and the amount of errors
decreased a bit. Is there a better workaround?

Google came up with two results: remove the wrappers
or use c++ instead of cc. I'm already using c++.

Thanks!
Diomidis Spinellis
2007-04-22 23:13:47 UTC
Permalink
Post by Andrew Pantyukhin
7zip developers converted some code from C++ to C,
while leaving the main stand-alone lzma app in C++.
They use 'extern "C" { }' blocks around #include's
referencing C headers.
Everything compiles fine, but "undefined reference"
errors appear at linkage. The undefined references
are to the C functions included from withing those
'extern "C"' wrappers. I tried to remove the
wrappers from some files and the amount of errors
decreased a bit. Is there a better workaround?
Google came up with two results: remove the wrappers
or use c++ instead of cc. I'm already using c++.
Many of the C headers are available in a different form for C++
programs. For example, in a C++ program you can #include <cstdio>
instead of <stdio.h>.

You can use nm(1) on the .o files to see where the problem comes from.

Diomidis
Andrew Pantyukhin
2007-04-23 00:04:59 UTC
Permalink
Post by Diomidis Spinellis
You can use nm(1) on the .o files to see where the problem comes from.
Thanks! Using nm(1) I saw that the object file generated
from pure C code had its symbols mangled - only then did
I notice that it was being compiled with c++, not cc. A
simple sed trick fixed that in a jiffy. How blind could
I be...

Thanks!

Loading...