1

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.

15
  • 1
    @HennoBrandsma: My guess: TU = "translation unit". Commented May 29, 2016 at 18:27
  • 1
    Is there anything special about inline extern "C" functions defined in a header? In a word, no. Why would this happen? What exactly? Unspecified unwanted side effects? Commented May 29, 2016 at 18:41
  • 1
    @n.m.I am trying to understand why references to that function show up in the compiled object file when the CPP file that includes this header has no references to anything in that header. I am missing something. Commented May 29, 2016 at 18:43
  • 3
    Look hard at Is inline without static or extern ever useful in C99. It covers the issue from the viewpoint of the C system. See also extern inline, which may be more apposite still. Basically, in C, one source file can contain extern inline …function definition… and this file will contain a non-inline function for the inline-able function. Commented May 29, 2016 at 18:53
  • 1
    I would strongly recommend NOT using inline in a header to be shared between C and C++, because it has different semantics in each of C++, ISO C, and GNU C. (no idea what MSVC's C compiler does with it) Commented May 29, 2016 at 22:40

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

To avoid modifying the SDK header, what happens if you pre-declare this function the way you want it before you #include the header? Like extern "C" inline void SomeFunc(void* user_data); or even inline void SomeFunc(void* user_data);. Perhaps this will convince the compiler to use the inline version of the function and ignore all the misleading extern versions that come after it in the #include file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.