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

陌路散爱 提交于 2019-12-10 17:26:42

问题


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.


回答1:


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!
}


来源:https://stackoverflow.com/questions/24060046/assuming-i-dont-use-any-overloaded-functions-is-there-a-way-i-can-stop-all-nam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!