Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is "extern C " necessary in c++ in windows?

Tags:

c++

c

windows

As we know that we can use c functions directly in c++, when is extern "C" necessary then?

like image 240
COMer Avatar asked Sep 16 '10 17:09

COMer


2 Answers

If your function is implemented in a .c file, the .cpp files will need the extern "C" reference, or else they'd reference a mangled C++-style function name, and the link would fail.

It's also handy for exporting functions from DLLs so that they are exported with a non-mangled name.

like image 174
Graham Perks Avatar answered Sep 29 '22 22:09

Graham Perks


It's necessary when a C++ function must be called by C code rather than C++ code.

Basically, when you want your C++ library to be backwards compatible.

like image 21
Randolpho Avatar answered Sep 29 '22 23:09

Randolpho