After I had finished an assigment, in which one had to implement a Win32 window, I wanted to export my functions to a dll.
Surprisingly the linker complains about unreferenced links, altough
The linker always complains (in german):
error LNK2019: Verweis auf nicht aufgel÷stes externes Symbol ""__declspec(dllimport) struct cw_Window * __cdecl createWindow(char *,unsigned short,unsigned short)" (__imp_?createWindow@@YAPEAUcw_Window@@PEADGG@Z)" in Funktion "main". C:\\Users\\jkirs\\Desktop\\Workspace\\MSVC2015_x86_64-release\\Unit.MSVC2015-x86-64.88a6cdd3\\intermediate.Unit.exe : fatal error LNK1120: 1 nicht aufgel÷ste Externe
My functions prototypes are defined as following:
typedef struct cw_Window cw_Window_t;
typedef struct cw_Event cw_Event_t;
API cw_Window_t* createWindow(char* pTitle, uint16_t nWidth, uint16_t nHeight);
API void destroyWindow(cw_Window_t* pWindow);
API void pollEvent(cw_Window_t* pWindow, cw_Event_t* pEvent);
where as "API" is defined as following:
#ifdef _MSC_VER
#ifdef CW_EXPORT
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif
#else
#define API // TODO
#endif
I tried to add 'extern "C" to the 'API' definition as well, without a result, but complaining about the string literal 'C'.
Has anyone come across this issue himself yet and can point me towards the right direction?
If it matters: I am uinsg Visual Studio C++ 2015 (MSVC_x86_64); my header files end with '.h' and source files end with '.c'.
EDIT: The lib is supposed to be used in C code again.
So it seems you knew the basic problem and approximate solution with extern "C"
. But your mistake is: don't put extern "C"
in the API define. Wrap the definitions and declarations with it:
typedef struct cw_Window cw_Window_t;
typedef struct cw_Event cw_Event_t;
extern "C" {
API cw_Window_t* createWindow(char* pTitle, uint16_t nWidth, uint16_t nHeight);
API void destroyWindow(cw_Window_t* pWindow);
API void pollEvent(cw_Window_t* pWindow, cw_Event_t* pEvent);
}
I know it looks like extern
and what you were trying seems like a sensible place to put extern, but extern "C"
is a different beast with different rules; you can read more than you want to about it here In C++ source, what is the effect of extern "C"?
After all, everything I made was just fine but the build system. Turns out the buildsystem treaded the file as an C++ Source File instead of plain C.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.