extern “C” functions in compiled object code

对着背影说爱祢 提交于 2019-12-13 20:07:06

问题


Environment is Microsoft Visual C++ 2015 and Windows 7.

Is there anything special about inline extern "C" functions defined in a header? I am consuming an SDK in which one of the headers contain such a beast. In my application I have a lone TU (translation unit) whose only job in life is to include the aforementioned header. That's all. Nothing else is in it. If I dig into the generated object file, I see the extern "C" function being pulled in. This is causing me some unwanted side effects (I will leave out what they are for now, as it might just distract from the main issue).

Why would this happen? There is nothing from the client code (remember my lone TU is empty except for main() entry point and that header) that is triggering this to happen.

UPDATE with a small snippet that might explain better what I am running into:

This is what is actually happening:

FooObj.h

FooObj::FooObj() { }

FooObj::~FooObj() { CallIntoAnotherC_API(); }

SDKHeader.h

#include <FooObj.h>

extern "C" inline void SomeFunc(void* user_data)
{   
    A* obj = static_cast<A*>(user_data);
    obj->CallAnotherFunc(FooObj(33));
}

MyFile.cpp

#include "SDKHeader.h"

int main() { return 0; }

Compiling MyFile.cpp into an executable fails with the linker complaining that CallIntoAnotherC_API is an unresolved external.


回答1:


Jonathan Leffler! Thank you very much for pointing me in the right direction. I found out what the problem is and its super weird to say the least. In the SDKHeader.h snippet I posted above, there is an extraneous declaration of SomeFunc like so:

#include <FooObj.h>

// I don't know why this declaration exists but its presence is
// causing the compiler to include SomeFunc and everything it references
// in the object file causing eventual linker errors! Also notice that
// this declaration even misses the "inline" keyword.
extern "C" void SomeFunc(void* user_data);

extern "C" inline void SomeFunc(void* user_data)
{   
    A* obj = static_cast<A*>(user_data);
    obj->CallAnotherFunc(FooObj(33));
}

Removing this extraneous declaration gets rid of the linker errors and also prevents the bogus symbol from showing up in the object file.



来源:https://stackoverflow.com/questions/37513384/extern-c-functions-in-compiled-object-code

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