错误LNK2005和错误LNK2019

error LNK2005 and error LNK2019 in C/C++ mix program

本文关键字:错误 LNK2019 LNK2005      更新时间:2023-10-16

程序有:raw_mouse.h、raw_mous.c RawInputRegistry.h RawInputRegister.cpp和main.cpp

在raw_mouse.h中,

我定义:

typedef WINUSERAPI INT (WINAPI *pGetRawInputDeviceList)(OUT PRAWINPUTDEVICELIST pRawInputDeviceList, IN OUT PINT puiNumDevices, IN UINT cbSize);
void testme();

在raw_mouse.c中,我有:

_RRID = (pRegisterRawInputDevices)GetProcAddress(user32,"RegisterRawInputDevices");
void testme()
{
    int a =10;
}

我在raw_mouse.c中包含了raw_mouses.h,在RawInputRegistry.h中也包含了raw_mouse.h,最后在main.cpp 中包含了RawInputRegistry.h

然而,我得到了这些错误:

RawInputRegistry.obj : error LNK2005: "int (__stdcall* _GRID)(struct HRAWINPUT__ *,unsigned int,void *,int *,unsigned int)" (?_GRID@@3P6GHPAUHRAWINPUT__@@IPAXPAHI@ZA) already defined in main.obj
RawInputRegistry.obj : error LNK2019: unresolved external symbol "void __cdecl testme(void)" (?testme@@YAXXZ) referenced in function "protected: __thiscall RawInputEventRegistry::RawInputEventRegistry(void)" (??0RawInputEventRegistry@@IAE@XZ)

不确定我是否应该使用extern"C"来包含raw_mouse.C中的所有代码?

不确定我是否应该使用extern"C"来包含raw_mouse.C中的所有代码?

必须raw_mouse.h文件中放入类似以下内容的s.th.,以使其同时符合c++和c#include

#ifndef RAW_MOUSE_H__
#define RAW_MOUSE_H__
#ifdef  __cplusplus
extern "C" {
#endif
/* Your C function declarations go here ... */
#ifdef  __cplusplus
}
#endif
#endif /* RAW_MOUSE_H__ */