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:
October 20: code_swarm
Next Installfest:
Sat. Nov. 8th [TENTATIVE]
Latest News:
Sep. 25: Installfest this Saturday
Page last updated:
2003 Jul 04 13:51
Events
 Meetings
 Installfests
 Demos
 Photos
Services
 Library
 LERT
 Jobs
 Documents
Interact
 Mailing Lists
 - Search
 - Archives
 Chat
About Us
 Members
 Projects
 Testimonials
 Call for Speakers
 Why Not MS?
 Finances
 Sponsors

^Home
?Search
?News & RSS
?Calendar
@Contact Us
$Buy Stuff
=Printable


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



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

LUGOD: Linux Users' Group of Davis
1105 Kennedy Place, Suite 1, Davis, CA 95616
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:
O'Reilly and Associates
For numerous book donations.