Assuming I don't use ANY overloaded functions, is there a way I can stop ALL name mangling? [duplicate]

StackOverflow https://stackoverflow.com//questions/24060046

  •  26-12-2019
  •  | 
  •  

Question

The title pretty much says it all. I know I can use and extern "C" block to stop mangling (although I'm not entirely sure where I should put said block) but is there a way that I can disable it for the whole program? And if I do, will that make the libraries that are compiled from the code easier to use with something like luajit's FFI?

EDIT: The question that this is supposedly a duplicate of is specific to DLLs and the Visual C++ compiler. I'm just asking a general C++ question.

Was it helpful?

Solution

As you have mentioned to disable name mangling using the extern "C" { } syntax to surround the function declarations you don't wan't to have mangled names for

extern "C" {
    int foo(int x, int y);
    void bar(const char* cstr); 
}

The easier way, if you are sure you're not using any c++ specific features, is to use the c-compiler to compile your code. For e.g. GCC toolchain call gcc instead of g++.

UPDATE:
The advantage of the extern method is that you can still use c++ features for the implementation (in a separate .cpp compilation unit), which is of course not possible when compiling your code as pure c-code. E.g.

#include "MyExportAPI.h"
#include <string>

void bar(const char* cstr) {
    std::string s(cstr); // <<< Note!
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top