|
-
September 6th, 2010, 08:50 AM
#1
Informations for a noob
I'm confused about the following words:
__declspec(dllexport) / __declspec(dllimport)
extern "C"
__stdcall
I only realized that extern "C" preceding a function create a decoration-free symbol in the export table (assuming this is correct)
Can you explain me what purposes are these conventions for?
-
September 6th, 2010, 04:46 PM
#2
Re: Informations for a noob
 Originally Posted by pm44xl23
__declspec(dllexport) / __declspec(dllimport)
These two declare a function to be exported or imported from a DLL, respectively. __declspec(dllexport) is, obviously, used inside a module that is part of a (or the the entire) DLL. There is at least one other way to achieve this, i.e. declaring the function to be exported in the .def file. A function declared __declspec(dllimport) is to be linked at load time, as opposed to run-time linking using LoadLibrary() and GetProcAddress().
This declares a certain calling convention to be used for a function. This calling convention is used by Windows API functions and callbacks. It is also common for functions exported from a DLL, though it is not mandatory here. It has several #define aliases like WINAPI and APICALL.
You are right about extern "C". But it also implies that code that is comiled under extern "C" is treated as real C by the compiler, i.e. it can't use any C++ features.
See also http://www.codeguru.com/forum/showthread.php?t=500568 regarding this topic.
HTH
-
September 7th, 2010, 04:39 AM
#3
Re: Informations for a noob
Thank you for your time answering me
-
September 7th, 2010, 07:12 AM
#4
Re: Informations for a noob
 Originally Posted by Eri523
You are right about extern "C". But it also implies that code that is comiled under extern "C" is treated as real C by the compiler, i.e. it can't use any C++ features.
Not entirely true. Only the function signature has to be compatible with C (and it can't be a class method, etc, of course). The code within the function may do anything it likes; this is how you can call C++ code from C.
-
September 7th, 2010, 12:32 PM
#5
Re: Informations for a noob
 Originally Posted by Lindley
Not entirely true. Only the function signature has to be compatible with C (and it can't be a class method, etc, of course). The code within the function may do anything it likes; this is how you can call C++ code from C.
Oh, really... I thought I would remember a recent thread over there in the non-VC++ section, where extern "C" was used to integrate C code into a C++ project, allowing it to do some of the rare C things that are not allowed in C++. Looks like I didn't remember it correctly... So I think I'll have to dig that thread out again and re-read it.
And what about the situation when I tell the compiler to treat the entire source file as C with the respective option in the file properties of the project? I.e. does that have the same meaning as using extern "C"?
Thank you for putting things right, and let me apologize for spreading misinformation.
-
September 7th, 2010, 12:41 PM
#6
Re: Informations for a noob
The effect of an "extern "C"" statement is to tell the compiler to use C-compatible name mangling. In practice, this means you can use it to let C call C++ code, or to let C++ call C code.
The easiest way to make an entire file be compiled as C rather than C++ is simply to make the filename end in .c. This does the trick for most build environments.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|