C++ : Extern C Functions inside a Namespace

烈酒焚心 提交于 2019-12-01 14:40:10

问题


I have to link two libraries, say A and B. Some of the files are common in both libraries. So, I declare functions in library A inside a namespace, say abc. So, in A and B, a function func looks like below:

[ in A]

    namespace abc {
    extern "C" void func();
    }


[in B]

    extern "C" void func();

While building the project, compiler throws linking errors saying multiple definitions of function func. Isn't the function func in A inside the namespace or is there some problem with extern "C" functions. If there is, then how can I differentiate them both?


回答1:


When you use Extern "C" you are turning off name mangling so you lose the namespace information as C has no such concept. This causes a duplicate definition.



来源:https://stackoverflow.com/questions/9685994/c-extern-c-functions-inside-a-namespace

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