1

I heard from a book (Professional C++ Edition 2) that C++ can be combined with many languages, close to all, by using extern. For example:

extern "Lua" {
     //Some code in Lua
};

extern "Python" {
     //Some code in Python
};

extern "C" {
     //Some code in C
};

int main(int argc, char* argv[]){
     //Some C++ code
     return 0;
}

Now I know that you can combine C and ASM with C++, but like I said before, can we use a lot more other languages like Lua, Python, PHP, etc? Or does it only work with languages that are dependent on C/C++?

Nfagie Yansaneh
  • 215
  • 1
  • 10
  • 7
    Any string literal can appear after `extern`, but a compiler doesn’t have to be able to handle values other than `"C"` and `"C++"`. See http://en.cppreference.com/w/cpp/language/language_linkage. Actual implementations are more “close to none” than “close to all”. – Ry- Jul 29 '17 at 23:43
  • Are you asking what's possible? What's typical? What's guaranteed? Is there any particular platform you're asking about? – David Schwartz Jul 29 '17 at 23:44
  • It is possible to integrate C++ code with code written in other languages, but it's more complicated than writing `extern "Magic"`. See e.g. [SWIG](https://en.wikipedia.org/wiki/SWIG) – Igor Tandetnik Jul 29 '17 at 23:44
  • 4
    You can certainly call C or C++ code from other languages, but not via the `extern language` mechanism, which basically only supports `extern "C"`. –  Jul 29 '17 at 23:44
  • 1
    "I read this in a book " - what, you read that C++ supports "extern "Python""? And what book? –  Jul 29 '17 at 23:45
  • the book did a example saying **extern "Language name here"** – Nfagie Yansaneh Jul 29 '17 at 23:46
  • 2
    Stop reading that book. – DimChtz Jul 29 '17 at 23:47
  • But it was a pretty good book, it talked all about **algorithm** **atmoic** **import dll's** and **metaprogramming**, i think the book just made a mistake – Nfagie Yansaneh Jul 29 '17 at 23:49
  • Nobody is "insulting you". And what was the title of the book? –  Jul 29 '17 at 23:49
  • Down-votes are not a judgement of you but rather of the quality of the question. For instance, does the question show that the user researched the question extensively before asking? – Hovercraft Full Of Eels Jul 29 '17 at 23:51
  • The book is called Professinal C++ edition 2 – Nfagie Yansaneh Jul 29 '17 at 23:51
  • It has a guitar on the front – Nfagie Yansaneh Jul 29 '17 at 23:52
  • Oh Hover dude, i couldn't find anyone else asking this question so i decided to ask to see if the book was correct soz for being wrong – Nfagie Yansaneh Jul 29 '17 at 23:52
  • https://www.amazon.com/Professional-C-Marc-Gregoire/dp/0470932449 – Nfagie Yansaneh Jul 29 '17 at 23:53
  • 1
    Again you misunderstand, this has nothing to do with being wrong, or being naive, but rather showing your research. Did you research the key `extern` word in several references, for example? Did you show the fruits of this research in your question? And don't assume that I'm one of the down-voters. I don't vote on subjects that I'm not expert in. – Hovercraft Full Of Eels Jul 29 '17 at 23:54
  • Yea i know that u can use extern with C and extern can be used for extern vars and all that – Nfagie Yansaneh Jul 29 '17 at 23:55
  • but when i got that example when the book did a example saying extern "Language name here" I thought i could – Nfagie Yansaneh Jul 29 '17 at 23:55
  • Well i learnt something today, before i ask, ill do a test on a program. Go on wiki and check other sites before asking – Nfagie Yansaneh Jul 29 '17 at 23:56
  • 2
    "Takes you on a technical tour of C++ and the STL, and explores the unusual and quirky aspects of this language" - does not inspire confidence. –  Jul 29 '17 at 23:57
  • what do you mean inspire confidence? – Nfagie Yansaneh Jul 29 '17 at 23:57
  • This question seems to be attracting a significant amount of comments that aren't related to improving the question. Let's save discussions for [chat]. – 4castle Jul 29 '17 at 23:59
  • Oh i think i know what you mean, yea the book teaching like all the basics and some intermeditate stuff but not like. Advanced topics that give confidence, like Servers & Clients and like using C++ with HTML or like Cryptography, yea i get what u mean now – Nfagie Yansaneh Jul 29 '17 at 23:59
  • @4castle I dont have enough reputation so we will just end it here – Nfagie Yansaneh Jul 30 '17 at 00:02
  • 3
    @DimChtz, when you say stop reading that book, do you know which book the OP is referring to??! Don't blame the book because someone may misunderstand what the authors are trying to convey. – CroCo Jul 30 '17 at 00:15

1 Answers1

4

I'm really not qualified to answer this question, but I'll do the best I can.

According to the documentation for extern, it provides for linkage between modules written in different programming languages.

Language linkage encapsulates the set of requirements necessary to link with a module written in another programming language: calling convention, name mangling algorithm, etc.

That may lead one to believe that, as allegedly claimed by the book, one could combine C++ with many other languages by using extern. That is simply not the case. Compilers only really support "C", and "C++". "C++" being the default and "C" being used to

link with functions written in the C programming language, and to define, in a C++ program, functions that can be called from the modules written in C.

This is due to the fact that C++ is a close relative of C, and most C++ compilers know how to compile C code. (It's actually more complicated than that, see this, and this) You can also embed assembly in C++, albeit not in a portable way.

Now that doesn't mean that other languages can't be combined with C++. You can, for instance, extend python, where you can load modules written in C++, and you can embedd python into C++, where a C++ program invokes the Python interpreter as a soubroutine. See Boost Python. Similarly you can embedd Lua in C++, and I'm sure many other scripting languages can be embedded. The point is that it's more complicated than just using extern, you need libraries for that specific purpose, and embedding each different language has its own unique complications.

As for why the book made such a claim, maybe you misunderstood it, as one could have misunderstood the documentation snippet above? In any case, any C++ book that would leave any level of ambiguity in that matter is probably not a very good book. I would hardly imagine that you should read any other C++ book until you have read all of the ones in The Definitive C++ Book Guide and List.

Ramon
  • 1,169
  • 11
  • 25