views:

62

answers:

1

Hi all,

i have a static library which (among other things) implements a tiny function that only returns some string from a table of const strings. This function is not called anywhere inside the library, but still it's declared as inline. For clarity, it looks like this:

namespace flow
{
    inline const char* GetName( BYTE methodType );
}

and the implementation:

const char* flow::GetName( BYTE methodType )
{
    if ( methodType < 5 )
        return cszNameTable[ methodType ];

    return NULL;
}

In another project, i'm linking against this library. I have the correct .h files included and I have the using namespace flow; in my code. The problem is, I'm getting linker errors:

error LNK2001: unresolved external symbol "char const * __cdecl flow::GetName(unsigned char)" (?GetName@flow@@YAPBDE@Z)

Now i can easily fix this by removing the "inline" keyword from the function declaration in the static library. So here are my questions:

1) Why does this error appear? How can i fix it without modifying the static library source code (without removing the inline keyword)?

2) What is the benefit of using the inline keyword in a static library function that is not called inside the library itself? Does the inline keyword have any effect when linking against the library from another project (i guess it does, but i'm not sure)?

+3  A: 

1) Why does this error appear? How can i fix it without modifying the static library source code (without removing the inline keyword)?

There's no point in declaring functions as inline. You have to define them in the header anyway:

namespace flow
{
    inline const char* GetName( BYTE methodType )
    {
        if ( methodType < 5 )
            return cszNameTable[ methodType ];

        return NULL;
    }
}

2) What is the benefit of using the inline keyword in a static library function that is not called inside the library itself? Does the inline keyword have any effect when linking against the library from another project (i guess it does, but i'm not sure)?

The effect of inline is that you can, and have to, define the function within the header, because the implementation of an inline function has to be visible where that function is called.

sbi
Indeed. http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.6
Stephen
So the reason for the linker error is because the implementation is not in the header file? (as i said in the comments to my question).
PeterK
Actually, you don't have to define it "right away". You can use a separate declaration followed by a separate implementation. Both still have to be in the header though.
Staffan
@PeterK: Yes, that's the problem all right.
Staffan
@Staffan: Yes, you are allowed to do _declare_ a function as `inline`. But there is no point in doing so. In order to be able to call an `inline` function, its _definition_ has to be available. And usually this means the definition has to be in the header. However, I changed my statement a little bit, hoping that it is less confusing this way.
sbi