Search code examples
c++cvisual-studio-2008windows-ce

LNK2019: unresolved external symbol in function


I'm using VS2008 WinCE7. I'm facing linking error during build.

The file system structure is

menu.c - ./menu.c
eboot.h - ./eboot.h
file_1.cpp - ./dir1/file_1.cpp

where . represents current directory

menu.c

#include <eboot.h>

static VOID OALWriteToEMMC(OAL_BLMENU_ITEM *pMenu);

VOID OALWriteToEMMC(OAL_BLMENU_ITEM *pMenu)
{
     OALTestEMMC();
}

file_1.cpp

#include <eboot.h>

VOID OALTestEMMC();

VOID OALTestEMMC()
{
//some code
}

eboot.h

 VOID OALTestEMMC();

I'm getting the error

menu.obj : error LNK2019: unresolved external symbol OALTestEMMC referenced in function OALWriteToEMMC

Please guide me how to solve it.

EDIT1:

menu.c

#ifdef __cplusplus
extern "C" VOID OALTestEMMC();
#endif

and removed the declaration in eboot.h and added it in file_1.cpp But, the error persists.


Solution

  • It seems that you are compiling the function definition in C++ but the use of that function in C. Without making the function extern "C" in C++, the compiler will mangle the name, and generate a symbol that won't match the declaration used in the C code.

    If you want to mix C and C++, make sure that the declarations in C++ are marked extern "C" so that the compiler won't mangle the names and will use the C calling conventions. Alternatively, compile everything in C++ (or in C)