C++ LNK2019:从CPP代码调用C函数
我创建了一个MSVS2015项目(MFC/C++),该项目 与静态C库的链接。当我建立 我发现以下错误:C++ LNK2019:从CPP代码调用C函数,c++,c,visual-studio,mfc,visual-studio-2015,C++,C,Visual Studio,Mfc,Visual Studio 2015,我创建了一个MSVS2015项目(MFC/C++),该项目 与静态C库的链接。当我建立 我发现以下错误: error LNK2019: unresolved external symbol "void __cdecl testLinkerError(void)" (?testLinkerError@@YAXXZ) referenced in function "void __cdecl test1(void)" (?test1@@YAXXZ) [PATH_REMOVED]\test.obj
error LNK2019:
unresolved external symbol "void __cdecl testLinkerError(void)" (?testLinkerError@@YAXXZ) referenced
in function "void __cdecl test1(void)" (?test1@@YAXXZ) [PATH_REMOVED]\test.obj [NAME_OF_MFC_PROJECT]
出于调试目的,我创建了3个文件:
test.cpp
linkertest.c
linkertest.h
两个项目中的调用约定都被设置为“y-cDECL < p>,基于编译器错误,编译器试图查找<代码> .TestLink KError @ @ YAXXZ < /Cord>这是一个C++函数名。这意味着在导入部分(而不是DLL导出部分)上,您没有使用
extern“C”
<>和另外一件事,在编译C++翻译单元时,将名称
\u cplusplus
(一个下划线)。您确定编译器提供了\u cplusplus
?我只见过它有两个前导下划线,据我所知,这是最接近任何“标准”的。您确定已将.lib文件包含在项目设置的“附加库”部分中,并且该文件是为编译所依据的生成配置设置的吗?刚才看到了@paddy的评论,我相信这是正确的答案。链接器错误指示C++编译忽略了#include "linkertest.h"
void test1(void)
{
testLinkerError();
}
#pragma once
#ifdef _cplusplus
extern "C" {
#endif
void testLinkerError(void);
#ifdef _cplusplus
}
#endif
#include "linkertest.h"
void testLinkerError(void) {
int x = 5;
int y = 7;
int z = x + y;
}