提问人:solaremperor 提问时间:10/27/2021 更新时间:10/28/2021 访问量:242
仅当在 Visual Studio 2019 中以 C++ 模式编译静态库时,代码链接
Code links only when static library compiled in C++ mode in Visual studio 2019
问:
我创建了一个链表管理器的 WIN32 静态库,我一直将其用于 C/C++ 开发。
我在 Visual Studio 2019 上编译了这个库,强制它编译为 C 代码(VS2019 首选项中的选项)。但是,我确实使用了/TC
#ifdef __cplusplus
extern "C" {
#endif
以防万一我的静态库会被 C++ 程序使用。这是标题的链接。
但是,当我在 C++ 项目中使用此静态库时,我收到链接器错误
1>quickDAQ.obj : error LNK2019: unresolved external symbol "unsigned long __cdecl cListLength(struct _cLinkedList *)" (?cListLength@@YAKPAU_cLinkedList@@@Z) referenced in function "void __cdecl quickDAQstart(void)" (?quickDAQstart@@YAXXZ)
1>quickDAQ.obj : error LNK2019: unresolved external symbol "int __cdecl cListEmpty(struct _cLinkedList *)" (?cListEmpty@@YAHPAU_cLinkedList@@@Z) referenced in function "void __cdecl setSampleClockTiming(enum _sampling_modes,double,char *,enum _trigger_modes,unsigned __int64,bool)" (?setSampleClockTiming@@YAXW4_sampling_modes@@NPADW4_trigger_modes@@_K_N@Z)
1>quickDAQ.obj : error LNK2019: unresolved external symbol "struct _cListElem * __cdecl cListFirstElem(struct _cLinkedList *)" (?cListFirstElem@@YAPAU_cListElem@@PAU_cLinkedList@@@Z) referenced in function "void __cdecl enumerateNIDevices(void)" (?enumerateNIDevices@@YAXXZ)
1>quickDAQ.obj : error LNK2019: unresolved external symbol "struct _cListElem * __cdecl cListNextElem(struct _cLinkedList *,struct _cListElem *)" (?cListNextElem@@YAPAU_cListElem@@PAU_cLinkedList@@PAU1@@Z) referenced in function "void __cdecl enumerateNIDevices(void)" (?enumerateNIDevices@@YAXXZ)
1>quickDAQ.obj : error LNK2019: unresolved external symbol "void * __cdecl cListFirstData(struct _cLinkedList *)" (?cListFirstData@@YAPAXPAU_cLinkedList@@@Z) referenced in function "void __cdecl syncSampling(void)" (?syncSampling@@YAXXZ)
1>quickDAQ.obj : error LNK2019: unresolved external symbol "int __cdecl cListInit(struct _cLinkedList *)" (?cListInit@@YAHPAU_cLinkedList@@@Z) referenced in function "void __cdecl enumerateNIDevices(void)" (?enumerateNIDevices@@YAXXZ)
1>quickDAQ.obj : error LNK2019: unresolved external symbol "int __cdecl cListAppend(struct _cLinkedList *,void *)" (?cListAppend@@YAHPAU_cLinkedList@@PAX@Z) referenced in function "void __cdecl enumerateNIDevices(void)" (?enumerateNIDevices@@YAXXZ)
1>quickDAQ.obj : error LNK2019: unresolved external symbol "int __cdecl cListPrepend(struct _cLinkedList *,void *)" (?cListPrepend@@YAHPAU_cLinkedList@@PAX@Z) referenced in function "void __cdecl pinMode(unsigned int,enum _IOmodes,unsigned int)" (?pinMode@@YAXIW4_IOmodes@@I@Z)
1>quickDAQ.obj : error LNK2019: unresolved external symbol "void __cdecl cListUnlinkElem(struct _cLinkedList *,struct _cListElem *)" (?cListUnlinkElem@@YAXPAU_cLinkedList@@PAU_cListElem@@@Z) referenced in function "void __cdecl enumerateNIDevices(void)" (?enumerateNIDevices@@YAXXZ)
1>C:\Users\tyros\codebase\testingLinkedList\Debug\testingLinkedList.exe : fatal error LNK1120: 9 unresolved externals
1>Done building project "testingLinkedList.vcxproj" -- FAILED.
显然,链接器无法链接到静态库。
但是,如果我使用设置为 C++ () 的编译为标志编译静态库,则我在其中使用此库的项目编译时不会出现上述错误。/TP
为什么会这样?
答:
0赞
solaremperor
10/28/2021
#1
问题是PEBKAC。我忘了包括新的头文件(具有 C++ 的外部“C”定义)以及新版本的静态库。这导致使用该库的 C++ 项目修改符号。
故事的寓意 - 更新 C++ 项目使用的库时,始终更改标头和静态库。
评论