我在.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
知道怎么解决吗?
答案 0 :(得分:1)
When you fully define this function in the .h
file, it is compiled in to any client code which #include
s 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