Visual Studio 2013 DLL Export Communication Error (LNK2019 / LNK1120)

I know that a similar question was asked before, but it looks like there is something different about exporting classes than a simple function ... I checked all these solutions, checked all the sentences, but it still looks like I something is missing ...

What's happening:

  • I have a main C ++ project written in Visual Studio 2013, and I want to add a dll library with various utilities. I created a dummy, mostly without a function, but it will not compile:
    2> TestSvc_i.c
    2> TestSvc.obj: error LNK2019: unresolved external symbol "__declspec (dllimport) public: __thiscall CUtils :: CUtils (void)" ( __imp _ ?? 0CUtils @ @ QAE @ XZ ) referenced in function _wWinMain @ 16
    2> C: \ Work \ TestSvc_root \ Debug \ TestSvc.exe: fatal error LNK1120: 1 unresolved externals
    ========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========

And the code in the main project is as follows:

extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
    CUtils *a = new CUtils();
    delete a;

    return 1;
}

I have the following setup:

  • The main project written in C ++ with Visual Studio 2013;
  • I use Unicode encoding, and the runtime is used in a common dll;
  • _UNICODE:
  • SubSystem: Windows ( /SUBSYSTEM:WINDOWS);
  • I want to create a utility library that should be used in a shared DLL. The code is as follows:

Utils.h:

#ifdef UTILS_EXPORTS
#define UTILS_API __declspec(dllexport)
#else
#define UTILS_API __declspec(dllimport)
#endif

// This class is exported from the Utils.dll
class UTILS_API CUtils {
public:
    CUtils(void);
};

Utils.cpp:

#include "stdafx.h"
#include "Utils.h"

// This is the constructor of a class that has been exported.
// see Utils.h for the class definition
CUtils::CUtils()
{
    return;
}
  • I checked the dll-project parameters and basically, they are all the same as in the main project;
  • UTILS_EXPORTSdefined in a dll project so, as a rule, all definitions should have __declspec(dllexport)(as expected, not defined in UTILS_EXPORTS);
  • : WIN32; _DEBUG; _WINDOWS; _USRDLL; UTILS_EXPORTS;
  • dll-project -;
  • dll $(SolutionDir)\Debug, $(SolutionDir)\$(MasterProject)\Debug - . , :
  • DUMPBIN Utils.dll :
    C:\Work\TestSvc_root\Debug>DUMPBIN /EXPORTS /SYMBOLS Utils.dll
    Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.


    Dump of file Utils.dll

    File Type: DLL

      Section contains the following exports for Utils.dll

    00000000 characteristics
    53C632A8 time date stamp Wed Jul 16 10:07:04 2014
        0.00 version
           1 ordinal base
           2 number of functions
           2 number of names

    ordinal hint RVA      name

          1    0 00011154 ??0CUtils@@QAE@XZ = @ILT+335(??0CUtils@@QAE@XZ)
          2    1 000110C8 ??4CUtils@@QAEAAV0@ABV0@@Z = @ILT+195(??4CUtils@@QAEAAV0@ABV0@@Z)

    Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text
       10000 .textbss
  • "" UTILS_API:
    #define DO_QUOTE(X)        #X
    #define QUOTE(X)           DO_QUOTE(X)
    #define MY_QUOTED_VAR      QUOTE(MYVARIABLE)

    #pragma message(QUOTE(UTILS_API))

: "" dll, __declspec(dllexport), __declspec(dllimport)

, - ? , -, / ... Visual ++ "def", ... , .

!

+4
1

, .lib ( .dll) .

+2

Source: https://habr.com/ru/post/1548611/


All Articles