Unresolved external

Hey guys. I have a compilation error I am trying to understand, manifesting in the form of unresolved externals.

Basically I have three files:


FileA.h which contains...
1
2
3
4
5
6
7
namespace foo
{
    namespace bar
    {
        extern int myFunction( int min, int max );
    }
}



FileA.cpp which contains...
1
2
3
4
int foo::bar::myFunction( int min, int max )
{
    return 0;
}



FileB.h which contains...
1
2
3
4
5
6
7
8
9
10
11
12
13
namespace foo
{
    namespace bleep
    {
        std::string myInlineFunction( unsigned int input )
        {
            // do something with input
            int value = foo::bar::myFunction( 0, 10 );
            // do some work           
            return std::string( "asd" );
        }
    }
}



In practice this very simply application builds for me and works fine using MSVC 2015. However I have an identically structured snippet of code in a much larger project producing the following error:

 
error LNK2019: unresolved external symbol "int __cdecl foo::bar::myFunction(int,int)" (?myFunction@bar@foo@@YAHHH@Z) referenced in function "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl foo::bleep::myInlineFunction(unsigned int)" (?myInlineFunction@bleep@foo@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@I@Z)


If I make 'foo::bar::myFunction()' inline and define it in the header the issue goes away. The error is only present when it is defined in the cpp file. Does anyone have any ideas what might be causing this?
Unless I'm mistaken, FileA.h doesn't require the extern keyword for declaring functions.

The extern keyword is used for variables that are defined in another file.

Try removing that, see what you get.

Joe.
Concord Spark Tutoring
closed account (48T7M4Gy)
Linker errors are just that. The linker hasn't got a clue what myFunction is because it can't find it. So you have to put in sufficient #includes to tell it where the relevant info is.
closed account (48T7M4Gy)
PS
main.cpp -> FileA.h (ie main has #include FileA.h)
and
FileA.cpp -> File.h (ie FileA.cpp has #include FileA.h)


Thanks for the replies. I tried removing the extern keyword but the same error occurs. I believe all the includes are correct.

Now I apologise for not sharing this info before hand as I genuinely did not think it was relevant but in its actual setting FileA.h, FileA.cpp and FileB.h are all part of a DLL. This DLL also contains FileC.cpp with an exported class inside it. This exported class contains a function which makes a call to FileB.h.

Now FileB.h contains an inline function so no issues there, but when the function in FileB.h calls a function in FileA.h, defined in FileA.cpp there are unresolved externals.

If I export the function in FileA.h (using __declspec(dllexport)) then the error dissapears and the build is fixed.

Is this normal behaviour? I assumed this function would not need to be exported because it is not called directly by anything outside the DLL.
Last edited on
> The linker hasn't got a clue what myFunction is because it can't find it.
> So you have to put in sufficient #includes to tell it where the relevant info is.
Unless the function is `inline' or `template', includes don't help.
You put functions declaration in header files, if some include is missing, you'll get a `not declared' error at compilation time.
Topic archived. No new replies allowed.