extern C main and C++

Apr 6, 2013 at 11:15am

extern C main and C++


In the Graham Wakefield’s repository ( https://github.com/grrrwaaa/maxcpp ) the main function of a C++ external is marked extern “C”.

But with or without, nothing is changed to the resulted bundle.

C74_EXPORT extern "C" int main(void)
C74_EXPORT int main(void)

In both cases i get :

000011b8 T _main
00009e88 S _main.eh

So my question is : Is it a good practice ? And if yes, why the “collect.cpp” example doesn’t use it.

Apr 6, 2013 at 11:42am

Given that (a) extern “C” tells the C++-compiler to suppress function name mangling (necessary for function overloading) and (b) main() can’t be overloaded, it’s probably a matter of taste.

Graham’s usage is more explicit, but I suspect the majority of of C++ programmers would argue that the explicitness is unnecessary and/or distracting.

I tend to feel this is like bracketing single statements inside if clauses. Some people always use brackets inside if-statements for consistency; others feel it makes simple code overly fussy. I don’t think you’ll find a broad consensus that either is “best practice” (although you’ll get some heated argument…)

Apr 6, 2013 at 1:59pm


Thanks Peter. Ok ; matter of taste ; I will use “extern C”. We are not safe about a maleficent compiler in the future that could decide to mangle and to overload ;-)

Apr 7, 2013 at 6:16pm

On a slight aside: does anyone know why Graham’s library fails to link (can’t find commonsyms) unless I wheel in MaxAudioAPI.framework?


You must be logged in to reply to this topic.