Search code examples
c++headerlnk2019

C++ API implementation in heade file LNK2019


I've 3 projects in my solution: Project_I, Project_A, Project_B

Project_I contains header files only (no cpp), this projects defines interfaces in the header files that Project_A and Projects_B derive from and implement.

Since I want default behavior in Project_I interfaces methods to throw NOT_IMPLEMENTED_EXCEPTION (my std::exception subtype), for each method declared in any of Project_I interfaces (header files) I wrote the this implementation in the header files, Also I implemented the C'TORs in the header files.

Now, when I try to compile the solution (after defining the project dependencies and etc...) I get the following error

Error   1   error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl Project_I::ClassA::ClassA()" referenced in function "public: __cdecl Project_B::ClassA_1::ClassA_1 K:\src\Project_B\ClassA_1.obj

Why does the VC++ compiler looks for the .obj file of ClassA in project_I (which needs to be built afterward) if there is no cpp file at all (the whole implementation is in the header file)?


Solution

  • If your Project_I::ClassA is implemented into a header file, it will not be compiled at all into the dll (unless the header is included by some cpp file of Project_I, of course).

    So, you have two options: create a cpp file and include the header containing ClassA definition or remove any __declspec declaration from it.