On Fri, 4 Jul 2003, Muthumeena M wrote:
> can u just tell me what is this extern "C" about and
> what if i use g++ ?
> basically what should i use?
C++ does funky things with function names when it stores them in symbol
table (in *.o files). (It does this because you can have multiple
functions with the same names in C++, but we digress.) So if you combine
a C program and a C++ program, you're gonna have function name mismatches.
To avoid this, people can instruct C++ programs to treat certain portions
of the code to be a C-only code. They do this by using `extern "C"` in
the header files. For example:
/* blah.h */
#ifndef BLAH_H_
#define BLAH_H_
#ifdef __cplusplus /* true if C++ compiler, false for C compiler */
extern "C" {
#endif
void blah();
#ifdef __cplusplus
}
#endif
#endif /* BLAH_H_ */
/* blah.c */
#include <stdio.h>
#include "blah.h"
void blah() {
printf("Blah!\n");
}
If you compile the above program with a C compiler, it'll compile it as a
normal C program. If you compile it with a C++ compiler, however, it'll
see:
extern "C" {
void blah();
}
in the header file, which tells it to not to do anything funky with the
blah() function name. So whether you use a C compiler or a C++ compiler,
you'll have a C-compatible "blah.o" file generated.
Later, if you link "blah.o" with another C-compiled object file, it'll
link clean since they're both C-compatible. If you link "blah.o" with
another C++-compiled object file, it'll link clean also, because during
compile time it notices `extern "C"` in the header file.
The common practice these days is to put `extern "C"` in all header files
intended to be compiled with C programs. It's safer that way.
I hope that helps.
-Mark
--
Mark K. Kim
http://www.cbreak.org/
PGP key available upon request.
_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech