l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
December 2: Social gathering
Next Installfest:
TBD
Latest News:
Nov. 18: Club officer elections
Page last updated:
2003 Jul 04 13:51

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] Reg lex and flex compatibilty
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] Reg lex and flex compatibilty



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



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.