-1

I have a static member function defined in .h file, which is working fine.

When I was trying to move the implementation to .cpp file, the project build failed with LNK2019 error by another class that is calling this function.

Error message is:

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

Here COrderProcessor::DoCreate() is trying to call static bool COAValidator::ValidatePercentage(), which provided via .dll

Any idea how to fix it?

Deqing
  • 14,098
  • 15
  • 84
  • 131
  • Can you show the code at least for this method please? – πάντα ῥεῖ Oct 06 '15 at 08:11
  • Did you create the `.dll` containing `COAValidator::ValidatePercentage()` yourself? Did you mark this method for export using `__declspec`? https://msdn.microsoft.com/en-us/library/a90k134d.aspx – atkins Oct 06 '15 at 08:12

1 Answers1

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

atkins
  • 1,963
  • 14
  • 27
  • Thanks for explaining. Our old code is still using `.def` to export symbols, which I had tried to add this function's signature in it but with no luck. – Deqing Oct 06 '15 at 12:22
  • Update: just tried again by copying signature from the one in error message to `def` and it works. – Deqing Oct 06 '15 at 22:56