Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The C++ language allows intermixing of both C++ and C in one source file. For example,

extern "C" {
    struct bar { /* ... */ }
}

Does C++ or has it ever supported any other "linkage languages" other than C? e.g. extern "Pascal" or extern "Haskell"

share|improve this question
2  
Awesome question! –  Jörg W Mittag Aug 23 '13 at 0:43
    
most of the time both languages will do their extern "C" equivalent to interact with each other –  ratchet freak Aug 23 '13 at 0:45
    
You should post your question in stackoverflow.com to gain more feedback on this question. –  huahsin68 Aug 23 '13 at 1:01
    
C++ supports other linkage by allowing imlementations to add their linkage types (§7.5), the question should be "Do implementations exist or have implementtions existed" ;-) –  johannes Aug 23 '13 at 2:25
2  
extern "C" only affects linkage, not the syntax and semantics of the text inside the block. In other words, it's still C++ inside that extern "C" block; the symbol names just aren't mangled. See stackoverflow.com/q/1041866/712605 –  Richard Hansen Aug 23 '13 at 19:10
show 1 more comment

4 Answers

extern "C" may be the only required one:

Every implementation shall provide for linkage to functions written in the C programming language, "C", and linkage to C++ functions, "C++".

However others can exist as per this quote:

The string-literal indicates the required language linkage. This International Standard specifies the semantics for the string-literals "C" and "C++". Use of a string-literal other than "C" or "C++" is conditionally- supported, with implementation-defined semantics. [ Note: Therefore, a linkage-specification with a string- literal that is unknown to the implementation requires a diagnostic. — end note ] [ Note: It is recommended that the spelling of the string-literal be taken from the document defining that language. For example, Ada (not ADA) and Fortran or FORTRAN, depending on the vintage. — end note ]

share|improve this answer
1  
Where is this quoted from? –  paul Aug 28 '13 at 13:42
    
@paul c++11 standard draft –  aaronman Aug 28 '13 at 14:29
add comment

In many (most) environments "extern "C" is the default linking convention for any language on the system.

So depending on the operating system and the implementation of the "foreign" language "extern C" is all you need to call a program in another compiled language.

share|improve this answer
    
Even environments not using C linkage (Pascal i.e. often uses other argument order on stack and the callee has to free the value) they have a C-compatible mode –  johannes Aug 23 '13 at 2:28
    
@johannes: Actually Windows use Pascal convention for many things, but since it needs that in C too and C does not have the "extern "Language"" syntax, they do it via platform-specific C extension, so for C++ it's still just extern "C" and the the special keyword specifies the different calling convention. –  Jan Hudec Aug 23 '13 at 11:59
1  
Still there might be cases where a compiler vendor adds special linkage types. Besides interoperability with other languages this might also be interesting to interoperability with different C++ name mangling. Question is whether anybody did :) –  johannes Aug 23 '13 at 12:27
add comment

G++ at some point supported extern "Java" to interact with GCJ-compiled Java code.

http://stackoverflow.com/questions/15167867/extern-java-block-in-gcc

share|improve this answer
add comment
  • No! C++ only support C as a "linkage languages".

  • extern "C" { struct bar { /* ... */ } }

  • The syntax as above means that do not change the name of variable,struct or function. Because C++ maybe change the name of variable,struct or function to implement Class ,Namespace and function overloading.

  • If you do not do this,the linker maybe can not find out the object of variable,struct of function.

  • C++ is designed to be compatible with C. The extern "C" is the way to implement compatible with C.

share|improve this answer
9  
Please check §7.5 of the C++ standard. extern "C" and extern "C++" are the only mandatory ones, but implementations are allowed to provide others. –  johannes Aug 23 '13 at 2:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.