0

I have two class that I want to import from a dynamic library. One off then work just fine. I can use the first one without any linking error, but the second one create an linking 2019 code error. I have no idea about what I'm doing wrong with the second one.

The problem happen only when I try to use the class.

There is the header file with and the definition off the class that create linking error :

Vertice.h //the class with linking error

#pragma once

#ifdef CGENGINE_EXPORTS
#define ENGINE_API __declspec(dllexport)
#else
#define ENGINE_API __declspec(dllimport)
#endif

constexpr char EMPTYCHAR = char(32);

class ENGINE_API Vertice
{
public:
    Vertice(int color, char caracter);
    ~Vertice(void);
    const bool operator != (const Vertice vertice);

public:
    int color;
    char caracter;
};

Vertice.cpp

#include "Vertice.h"

Vertice::Vertice(int color, char caracter) :
    color(color), caracter(caracter)
{
}

Vertice::~Vertice(void)
{
}

const bool Vertice::operator!=(const Vertice vertice)
{
    const unsigned int size = sizeof(Vertice);
    for (int index = 0; index < size; index++)
    {
        char* self_byte = ((char*)this) + index;
        char* vertice_byte = ((char*)&vertice + index);

        if (*self_byte != *vertice_byte) { return false; }

    } return true;
}

There is the error output :

Build started... 1>------ Build started: Project: Flappy Bird, Configuration: Debug Win32 ------ 1>Application.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall Vertice::Vertice(int,char)" (__imp_??0Vertice@@QAE@HD@Z) referenced in function _main 1>Application.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall Vertice::~Vertice(void)" (__imp_??1Vertice@@QAE@XZ) referenced in function _main 1>C:\Users\Jehud\source\repos\CGEngine\Debug\Flappy Bird.exe : fatal error LNK1120: 2 unresolved externals 1>Done building project "Flappy Bird.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== ========== Build started at 9:22 PM and took 02,122 seconds ==========

I try to put both class declaration in the same header file and/or same cpp file. It give me the same error output.

J3hud
  • 13
  • 3
  • Please indicate how you are compiling and linking your files - the dup is for command line issues, but perhaps you are using an IDE? – Ken Y-N Jun 05 '23 at 02:10
  • I use visual code 2022, I already check for the project property on both side and the are fine. – J3hud Jun 05 '23 at 02:14
  • Having an IDE doesn't magically make your work error-free. Show the actual compilation and link commands. – n. m. could be an AI Jun 05 '23 at 03:51

1 Answers1

-1

According this document Using dllimport and dllexport in C++ Classes you must use the next class declaration:

#define DllExport   __declspec( dllexport )

class DllExport C {
   int i;
   virtual int func( void ) { return 1; }
};
daniyar
  • 1
  • 1