Language Linkage

Jan 22, 2014 at 6:06pm
Hi,

I learned that C compiler converts a function's prototype/definition to a binary format by the function name Only,

while C++ compiler converts a function's prototype/ definition to a binary format by its function name + arguments types/quantity. (that's why functions overloading is feasible in C++)

Then, it was said that to enable calling C compiled function from C++ code, one should use the extern "C" directive.

What does it mean?

why can't C++ compiler just compile all these function in both CPP and C source files?

Or is the above talking on a case where C++ Compiler is given C Object files, and not C Source files.


for example, when I write this function, without using extern "C" on printf(..), I don't get linkage error.

How come?

(I use Visual Studio C++ Compiler)


1
2
3
4
5
6
7
#include <stdio.h>

int main()
{
printf("hello, is it me you're looking for");
return 0;
}

Last edited on Jan 22, 2014 at 6:08pm
Jan 22, 2014 at 6:32pm
How do you think that function is declared?
I don't have VC++ at hand, but for example,

in Linux, stdio.h in the very beginning says:
1
2
# include <features.h>
__BEGIN_DECLS

The include file features.h, includes sys/cdefs.h, which defines that macro as follows:
1
2
3
#ifdef  __cplusplus
# define __BEGIN_DECLS  extern "C" {
# define __END_DECLS    } 


so printf() is declared inside extern "C" {...}
Jan 22, 2014 at 6:57pm
closed account (Dy7SLyTq)
isnt there also an std:: version of each c function?
Jan 22, 2014 at 7:19pm
@Cubbi

Thank you very much!

you are right.

inside the stdio.h file, there's

1
2
3
#ifdef __cplusplus
extern "C" {
#endif  /* __cplusplus */ 


However,
I don't understand,
What is wrong with having the C++ compiler, compiling the printf prototype and definition?

If it mangles both the prototype and definition, why should there be a problem?



@DTSCode

Yes, there's a C++ lib called iostream, which has namespace called std, which include two global objects - cout and cin - and they constitute interface to the output console.
Last edited on Jan 22, 2014 at 7:21pm
Jan 22, 2014 at 7:26pm
closed account (10X9216C)
h
Jan 22, 2014 at 7:31pm
What is wrong with having the C++ compiler, compiling the printf prototype and definition?
If it mangles both the prototype and definition, why should there be a problem?


Nothing is wrong with that, and if you copy the definition of printf from some C library source code into your own project, you could compile it and call without any linkage specification.
Jan 22, 2014 at 7:38pm
On a particular implementation, it is (at least theoretically) possible that printf() from the C standard library has C++ linkage.
Whether a name from the C standard library declared with external linkage has extern "C" or extern "C++" linkage is implementation-defined. It is recommended that an implementation use extern "C++"
linkage for this purpose.

Footnote: The only reliable way to declare an object or function signature from the Standard C library is by including the header that declares it, notwithstanding the latitude granted in 7.1.7 of the C Standard. - IS
Last edited on Jan 22, 2014 at 7:41pm
Jan 22, 2014 at 7:43pm
closed account (Dy7SLyTq)
no thats not what i meant. im aware of iostream. what i mean is in the <c*> libraries i believe there are also things like std::printf and std::atoi
Jan 22, 2014 at 7:46pm
@ Cubbi and JLBorges

Thank you very much!


So if there's no real need for extern "C" statement (that is what you said right?), what is it for?

Why should one use extern "C" if he can work without it in the same way?
Jan 22, 2014 at 7:54pm
> Why should one use extern "C" if he can work without it in the same way?

extern "C" is used when interoperability with C code is required.

If that is not a requirement, extern "C" should not be used.
Jan 22, 2014 at 8:17pm
@ JLBorges

Thank you friend.


could you please give an example when extern "C" must/should be used?
Jan 22, 2014 at 8:33pm
Your printf from the first post was a good example.

Perhaps http://en.cppreference.com/w/cpp/language/language_linkage can help?
Topic archived. No new replies allowed.