错误LNK2019:将代码放入cpp文件时未解析的外部符号

时间:2015-10-06 08:09:04

标签: c++ visual-c++ static-members

我在.h文件中定义了一个静态成员函数,该函数运行正常。

当我尝试将实现移动到.cpp文件时,项目构建因另一个调用此函数的类而导致LNK2019错误而失败。

错误信息是:

8>------ Build started: Project: COrders, Configuration: Debug x64 ------
8>     Creating library D:\devel\Server\COrders\Debugx64\COrders.lib and object D:\devel\Server\COrders\Debugx64\COrders.exp
8>OrderProcessor.obj : error LNK2019: unresolved external symbol "public: static bool __cdecl COAValidator::ValidatePercentage(class CSDO const &,class CExecInstHelper const &,class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > > const &)" (?ValidatePercentageXI@COAValidator@CPlus@@SA_NAEBVCSDO@AEBVCExecInstHelper@@AEBV?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@@Z) referenced in function "private: int __cdecl CPlus::COrderProcessor::DoCreate(int)" (?DoCreate@COrderProcessor@CPlus@@AEAAHH@Z)
8>D:\devel\Server\Server\COrders\Debugx64\COrdersd.dll : fatal error LNK1120: 1 unresolved externals

此处COrderProcessor::DoCreate()正试图通过static bool COAValidator::ValidatePercentage()

致电.dll

知道怎么解决吗?

1 个答案:

答案 0 :(得分:1)

When you fully define this function in the .h file, it is compiled in to any client code which #includes that .h file, and no linking is required to locate the function definition.

However, if you move the function definition to the .cpp file, the client code can no longer see that definition at compile time, and relies on the linker to locate it. It's then imperative that the function is marked for dllexport, so that it is included in the dll's function table.

This page describes the syntax for doing this: https://msdn.microsoft.com/en-us/library/a90k134d.aspx