LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-02-2005, 11:04 PM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
about extern "C"


Hello everyone,


I have learned about the concept of extern "C". It is used to compile a C++ file into C binary format. Am I correct?

But I can not quite understand the concept and its special benefits until learn from a simple proof of concept sample and run the sample by myself. However, I can not find out such a simple proof of concept sample.

Could anyone help to find one a simple proof of concept sample or write one for me?


thanks in advance,
George
 
Old 09-02-2005, 11:53 PM   #2
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
The scripting language Lua requires you to extern "C" its header files. I did a quick look up on why that is and here's what I found:

Quote:
Why isn't there an extern "C" block, required for C++, in the API header?
Lua is implemented in ANSI C. No special treatment is given for the import of its interface into other languages, including C++. To use the Lua header in C++, wrap it externally:

extern "C" {
#include "lua.h"
}

Another reason is that Lua is also correct C++ code. You can compile Lua with a C++ compiler without any changes. If it had the extern "C" declaration, Lua would generate a C interface even when compiled as a C++ library. See BuildingLua.
Quote:
Note on embedding Lua in C++ applications

Please note that Lua is ANSI C. In order to embed Lua in a C++ application (i.e. link C to C++) you will have to place extern "C" around the inclusion of the Lua headers in your C++ application, e.g.,

extern "C" {#include "lua.h"

}

If you do not do this you may get link errors because of C++ name mangling. Lua does not place: {#ifdef __cplusplus extern "C" {#endif ... Lua header ...#ifdef __cplusplus}#endif } in the code because it is clean ANSI C. Adding the above code would pollute the namespace with another language, i.e. C++.

Hope that clears things up a little.
 
Old 09-03-2005, 12:03 AM   #3
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
Name mangling means that the compiler adds some prefixes and/or suffixes to function names so you can have multiple functions with the same name but different parameters and return type. The 'extern "C"' disables this.

You can test it yourself this way: write some C++ code and compile it to assembler (-S switch in gcc). Then add extern "C" and compile again. Then compare the output files. They should be identical except function names.
 
Old 09-03-2005, 12:29 AM   #4
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks Harmaa,


Quote:
Originally posted by Harmaa Kettu
Name mangling means that the compiler adds some prefixes and/or suffixes to function names so you can have multiple functions with the same name but different parameters and return type. The 'extern "C"' disables this.

You can test it yourself this way: write some C++ code and compile it to assembler (-S switch in gcc). Then add extern "C" and compile again. Then compare the output files. They should be identical except function names.
So, extern "C" is used in C++ file only, is that correct? And it is used to provide C++ functions to C modules? Am I correct?


regards,
George
 
Old 09-03-2005, 12:47 AM   #5
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks R00ts,


Quote:
Originally posted by R00ts
Lua is implemented in ANSI C. No special treatment is given for the import of its interface into other languages, including C++. To use the Lua header in C++, wrap it externally:

extern "C" {
#include "lua.h"
}

Another reason is that Lua is also correct C++ code. You can compile Lua with a C++ compiler without any changes. If it had the extern "C" declaration, Lua would generate a C interface even when compiled as a C++ library. See BuildingLua.
It is mentioned that Lua is in ANSI C, but it is also mentioned that it is correct C++ code. I can not imagine why it could be both C code and C++ code. Could you help please?

Quote:
Originally posted by R00ts
If you do not do this you may get link errors because of C++ name mangling. Lua does not place: {#ifdef __cplusplus extern "C" {#endif ... Lua header ...#ifdef __cplusplus}#endif } in the code because it is clean ANSI C. Adding the above code would pollute the namespace with another language, i.e. C++.
What namespace pollution will be if Lua place extern "C" directly into its file?

In my previous understanding, I think extern "C" is used in C++ to make C++ functions be able to be called in C. But from your reply, it seems that things are adverse, i.e. your points are that it is used to make C function to be able to be called in C++. How strange it is!


have a good weekend,
George

Last edited by George2; 09-03-2005 at 01:07 AM.
 
Old 09-03-2005, 02:58 AM   #6
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
Quote:
In my previous understanding, I think extern "C" is used in C++ to make C++ functions be able to be called in C. But from your reply, it seems that things are adverse, i.e. your points are that it is used to make C function to be able to be called in C++. How strange it is!
It is needed in both cases. Assume we have a function prototype from a library written in C:
Code:
void foo(char *s);
Without extern "C" gcc seems to name that as "_Z3fooPc", and uses that name in the resulting binary code. But in the library, the function is named simply "foo", so the linker doesn't find it.

Quote:
What namespace pollution will be if Lua place extern "C" directly into its file?
Think what happens if you have the "foo" above written in C++ and a different function named "_Z3fooPc" written in C.

Quote:
It is mentioned that Lua is in ANSI C, but it is also mentioned that it is correct C++ code. I can not imagine why it could be both C code and C++ code. Could you help please?
C++ was designed that way. In principle, all valid C code in also valid C++.
 
Old 09-03-2005, 05:21 AM   #7
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks Harmaa,


Quote:
Originally posted by Harmaa Kettu
It is needed in both cases. Assume we have a function prototype from a library written in C:
Code:
void foo(char *s);
Without extern "C" gcc seems to name that as "_Z3fooPc", and uses that name in the resulting binary code. But in the library, the function is named simply "foo", so the linker doesn't find it.
I do not understand what do you mean "But in the library, the function is named simply "foo", so the linker doesn't find it.". I think you mean some other modules ask the linker to find the function foo, when we perform link operation. But what modules? Could you provide more information please?

Let us suppose a simple case. Suppose I am using the same compiler and compile two C modules (foo.c and goo.c) into different object files (foo.o and goo.o). Suppose in module foo.c, I define foo function and in module goo.c, I define goo function. We will invoke foo function in goo function.

As you mentioned above, in foo.o file, the method name may change (to "_Z3fooPc", and not simple foo). But when writing source codes, I do not know what name foo will be "_Z3fooPc" in foo.o, so in source codes of goo function, I invoke "foo", since the function name in foo.o is not the same as foo, will there be any troubles when linking foo.o and goo.o together? Are there any issues for goo function to find out foo function?


have a good weekend,
George

Last edited by George2; 09-03-2005 at 05:22 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
extern "C" and static data type problem with g++ vtluu Red Hat 1 05-21-2004 10:45 AM
extern "C" and static data type problem with g++ vtluu Programming 2 04-28-2004 05:10 AM
extern "C" lackluster Programming 4 07-25-2002 12:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration