www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - extern (c)

reply Paul <phshaffer gmail.com> writes:
What does the extern (c) attribute(?) do?
Does it tell the compiler/linker to build the function like a C 
compiler would build a C function? If so what does that mean?
Does it tell the compiler/linker to let C functions know it 
exists? If so what does that mean?
Is it meant for the compiler or linker or both?

Thanks for any assistance.
Oct 11 2023
next sibling parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Wednesday, 11 October 2023 at 12:36:58 UTC, Paul wrote:
 What does the extern (c) attribute(?) do?
 Does it tell the compiler/linker to build the function like a C 
 compiler would build a C function? If so what does that mean?
 Does it tell the compiler/linker to let C functions know it 
 exists? If so what does that mean?
 Is it meant for the compiler or linker or both?

 Thanks for any assistance.
It is about name mangling. It prevents symbol names from using D's name mangling. https://dlang.org/blog/2017/12/20/ds-newfangled-name-mangling/
Oct 11 2023
prev sibling parent reply user1234 <user1234 12.de> writes:
On Wednesday, 11 October 2023 at 12:36:58 UTC, Paul wrote:
 What does the extern (c) attribute(?) do?
 Does it tell the compiler/linker to build the function like a C 
 compiler would build a C function? If so what does that mean?
 Does it tell the compiler/linker to let C functions know it 
 exists? If so what does that mean?
 Is it meant for the compiler or linker or both?

 Thanks for any assistance.
`extern(C)` on module level functions affect the mangling and the calling convention. - Mangling is used by the linker to link symbols between objects. - Calling convention affects the compiler backend in how code is generated for a CALL instruction.
Oct 11 2023
parent reply Paul <phshaffer gmail.com> writes:
On Wednesday, 11 October 2023 at 12:54:53 UTC, user1234 wrote:
 `extern(C)` on module level functions affect the mangling and 
 the calling convention.

 - Mangling is used by the linker to link symbols between 
 objects.
 - Calling convention affects the compiler backend in how code 
 is generated for a CALL instruction.
So C doesn't use name mangling and extern (C) in a D prog would turn off D mangling thereby allowing C and D object files to be linked together? I didn't know C and D had different calling conventions...i.e. different ABI's?
Oct 11 2023
next sibling parent user1234 <user1234 12.de> writes:
On Wednesday, 11 October 2023 at 13:36:16 UTC, Paul wrote:
 On Wednesday, 11 October 2023 at 12:54:53 UTC, user1234 wrote:
 `extern(C)` on module level functions affect the mangling and 
 the calling convention.

 - Mangling is used by the linker to link symbols between 
 objects.
 - Calling convention affects the compiler backend in how code 
 is generated for a CALL instruction.
So C doesn't use name mangling
I see what you mean. I just consider no mangling as a kind of mangling, that is "just the symbol name".
 and extern (C) in a D prog would turn off D mangling thereby 
 allowing C and D object files to be linked together?

 I didn't know C and D had different calling conventions...i.e. 
 different ABI's?
They are mostly similar but according to https://dlang.org/spec/abi.html#function_calling_conventions there are differences.
Oct 11 2023
prev sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Wednesday, 11 October 2023 at 13:36:16 UTC, Paul wrote:
 On Wednesday, 11 October 2023 at 12:54:53 UTC, user1234 wrote:
 `extern(C)` on module level functions affect the mangling and 
 the calling convention.

 - Mangling is used by the linker to link symbols between 
 objects.
 - Calling convention affects the compiler backend in how code 
 is generated for a CALL instruction.
So C doesn't use name mangling and extern (C) in a D prog would turn off D mangling thereby allowing C and D object files to be linked together? I didn't know C and D had different calling conventions...i.e. different ABI's?
https://dlang.org/spec/abi.html
Oct 11 2023