-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2010-08-24 16:12, Thomas Hruska wrote:
> On 8/24/2010 4:50 AM, Stanisław Findeisen wrote:
>> ======================================================================
>> If you are including a C header file that isn't provided by the system,
>> you may need to wrap the #include line in an extern "C" { /*...*/ }
>> construct. This tells the C++ compiler that the functions declared in
>> the header file are C functions.
>>
>> http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.3
>> ======================================================================
>>
>> 1. What if I don't wrap them? Is it possible to have runtime errors
>> because of that? I understand linker errors are possible.
> 
> This is just addressing name mangling issues.  Look up "name mangling" 
> on Google.

What about calling conventions?

>> 2. When do you think this wrapping is not needed, i.e. what do you think
>> "provided by the system" was meant to mean? Parashift.com author gives
>> <cstdio>  as an example, but what about POSIX headers like<pthread.h>  or
>> <sys/socket.h>? Do you need to wrap them in extern "C" in portable code?
> 
> Those are provided by the system.  The author is referring to 
> third-party code not supplied by the vendor directly.

What's the difference??

>> ======================================================================
>> [32.6] How can I create a C++ function f(int,char,float) that is
>> callable by my C code?
>>
>> The C++ compiler must know that f(int,char,float) is to be called by a C
>> compiler using the extern "C" construct [...]
>>
>> http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.6
>> ======================================================================
>>
>> ======================================================================
>> [...] static member functions do not require an actual object to be
>> invoked, so pointers-to-static-member-functions are usually
>> type-compatible with regular pointers-to-functions. However, although it
>> probably works on most compilers, it actually would have to be an extern
>> "C" non-member function to be correct, since "C linkage" doesn't only
>> cover things like name mangling, but also calling conventions, which
>> might be different between C and C++.
>>
>> http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2
>> ======================================================================
>>
>> 3. Is it possible to have runtime errors if you don't use extern "C" in
>> this case? I think YES?
> 
> Only if name mangling gets screwed up really badly somehow.

What about calling conventions?

> The linker 
> is usually pretty good about preventing such things.  The runtime error 
> would almost always be the inability to load a module (.dll/.so) because 
> one or more of the exports won't resolve dynamically and then the whole 
> program would fail to load as a result.  That is, the OS won't load the 
> program in the first place due to dependency issues.  However, as I 
> said, the linker is pretty good about preventing such problems.
> 
> I've never had compilation problems like that where the same compiler 
> suite is used for all the components being built.

Maybe, but I am asking about the standard.

>> 4. What effect does extern "C" have on C++ function in this case? It's
>> about generating machine code of the function itself, right? No wrapper?
>> So when you call this C++ function from within C++ code (from the same
>> or another translation unit), the C calling convention should be used?
> 
> Not sure what you are asking here.  I think you are just confused at 
> this point.  I know the feeling.

I am asking about the function calling conventions. For from
http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2
I've learned that in C and C++ they might different. So if you want a C
code to call your C++ function, you have to make that function
compatible with C calling convention. Otherwise you can get a runtime
BANG. Right?

However the question is: given an architecture and a language (C or C++)
does there exist exactly 1 standard calling convention??

STF

http://eisenbits.homelinux.net/~stf/
OpenPGP: DFD9 0146 3794 9CF6 17EA  D63F DBF5 8AA8 3B31 FE8A
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkxz3VUACgkQ2/WKqDsx/orFFgCcC9xm+k6XeT3FbvtgQO8DTQO4
F5IAn1vmn+TFqv8FgtSzzdQRj57HR2xg
=7TDM
-----END PGP SIGNATURE-----

Reply via email to