Gomu
Gomu

Reputation: 1064

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.

Upvotes: 0

Views: 381

Answers (2)

David LaPorte
David LaPorte

Reputation: 231

David Rodríguez's original answer is correct. You are invoking a C++ function (OALTestEMMC) from a C source file. As David mentioned, the C++ compiler will mangle the function name. In order to suppress the name mangling so that the code in menu.c can invoke it, place the 'extern "C"' qualifier on the OALTestEMMC function (in file_1.cpp):

extern "C" VOID OALTestEMMC();

extern "C" VOID OALTestEMMC() {
    // stuff
}

Upvotes: 1

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)

Upvotes: 2

Related Questions