question

jjs avatar image
0 Votes"
jjs asked ·

Unresolved external symbol error when using multiple directories with c++ project

Hello, I'm having an issue where VS is throwing an unresolved external symbol error and I'm not sure how to resolve this.

Error: LNK2019 unresolved external symbol "public: __thiscall coppercable::coppercable(void)" (??0coppercable@@QAE@XZ) referenced in function _main NetworkStackMain C:\Users\JoshS\OneDrive\NetworkStack\NetworkStack\NetworkStackMain\NetworkStackMain.obj 1

After a few searches this seems to commonly be due to the linker being unable to find this function declaration. I attempted to add the directories of the file containing the function the error is referring to, coppercable::coppercable(), but this did not help.

Here is the location of my VS project files, per the error

![75644-image.png][2]


And here is the location of the file containing the function declaration, note the definition is in a cpp file in /source instead of /header


75663-image.png


Code in main:

 #include <iostream>
 #include "cable.hpp"
 #include "coppercable.hpp"
    
 using namespace std;
    
 int main()
 {
     cable* cable1 = new coppercable();
     cable1->setCableDiameter(0.2893);
     cable1->setCableLength(5);
     cout << cable1->getAwgRating();
 }

Header file with function declaration:


 #include <iostream>
 using namespace std;
    
 class coppercable : public cable
 {
     public:
         coppercable();
         ~coppercable();
         double getPowerLoss() override;
 };

Source file with function definition:

 #include "../header/coppercable.hpp"
 #include "../header/copperwire.hpp"
 #include <iostream>
 #include <math.h>
 using namespace std;
    
 coppercable::coppercable()
 {
        
 }

And for project properties, I've tried adding multiple directories including the exact directory where the source and header files are which contain the functions declaration/definition

75589-image.png




Has anyone experienced this kind of issue based on this scenario? Any pointers or ideas is appreciated, thank you!

c++vs-debugging
image.png (30.9 KiB)
image.png (25.7 KiB)
image.png (67.4 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered ·

Probably you must add coppercable.cpp and maybe other .cpp files to your project. Right-click Source Files or project node in Solution Explorer, go to Add, Existing Item and select the file.

The longer way: create a Static Library or DLL, which can be used in many main projects.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DylanZhu-MSFT avatar image
0 Votes"
DylanZhu-MSFT answered ·

Hi jjs,

The LNK2019 often means that your declaration is not defined. You can try to add the directories of source files in VC++ Directories/Source Directories.
75678-image.png
Best Regards,
Dylan


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


image.png (32.1 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.