ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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++.
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.
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?
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!
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++.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.