This question already has an answer here:
-
Is there a way to suppress c++ name mangling?
3 answers
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.
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!
}