Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to specify extern "C" when exporting symbols?

I am wondering if extern "C" is a must or not?

like image 750
user496949 Avatar asked Dec 27 '22 23:12

user496949


2 Answers

Only if you want to call your code from C (or a different C++ compiler, which you should treat like C).

It is to disable name-mangling.

See this article on the C++ FAQ: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

like image 111
Merlyn Morgan-Graham Avatar answered Jan 13 '23 13:01

Merlyn Morgan-Graham


No, you use extern "C" to provide a C-linkage to your C++ functions, so they won't be 'decorated' like normal C++ functions and to allow them to be called from C (or Objective-C).

Function decoration is used to implement the C++ function overloading feature and gives each variation of the function a different signature while allowing the developer to use the name he assigned.

Your C++ functions will be exported automatically by simply not using the static keyword. However if you have implemented your C++ functions within a Windows DLL it's necessary to use the declspec dllexport/dllimport keywords to access them externally.

like image 42
trojanfoe Avatar answered Jan 13 '23 11:01

trojanfoe