0

I Created a Test.h and Test.cpp in Visual Studio. Test.h

#ifndef TEST_H
#define TEST_H

class Test {
public:
    Test(int num);

    int getMyNum();

private:
    int myNum = 0;

};

#endif // !TEST_H

Test.cpp

#include "Test.h"

Test::Test(int num) {
    myNum = num;
}

int Test::getMyNum()
{
    return myNum;
}

So when I want to use the Test class in other project in Visual Studio I can just go to project properties >> VC++ Directories >> Include Directories, and then paste the path to the files location. And in this project I create a main.cpp

#include <iostream>
#include <Test.h>

int main(){
    Test test(4);
    std::cout << test.getMyNum();
}

I thought it should work just fine, and it do not give me any errors, but when I try to compile it I get some unresolved external symbol errors: unresolved external symbol "public: __cdecl Test::Test(int)" (??0Test@@QEAA@H@Z) referenced in function main

unresolved external symbol "public: int __cdecl Test::getMyNum(void)" (?getMyNum@Test@@QEAAHXZ) referenced in function main

It do not look like there is any symbol without definition.

What I wanted to know is why this is happening, and how to solve it.

BOOM
  • 11
  • 2
  • How do you build the library project? How do you ***link*** with it? – Some programmer dude Jan 02 '23 at 13:07
  • The library is just header based, and I just included in the project, should I link it some way? – BOOM Jan 02 '23 at 13:09
  • The headers usually just contain some available symbols, but the actual implementation is contained in the .cpp file. You need to get access to this implementation one way or another: either add the cpp file to the new target or link a static library that contains this cpp file as source... – fabian Jan 02 '23 at 13:11
  • @fabian I already read this question, but sadly it do not helped me. – BOOM Jan 02 '23 at 13:11
  • @fabian Ok, I will try it, but I think I saw this being done before, without any static library, just headers. – BOOM Jan 02 '23 at 13:14
  • 1
    Your library is NOT header based (as in a header only library). For header only libraries all the logic gets included via the header, even the implementation. You'd need to implement the function+constructor as inline in the header itself. – fabian Jan 02 '23 at 13:14
  • You need to create a library project in your VS solution, then make your application use it. Or you can create the library as a separate solution and projects, build it separately, and then in your application you must link with it. – Some programmer dude Jan 02 '23 at 13:18
  • @fabian If I put the definition and declaration in just one file, will it work fine? – BOOM Jan 02 '23 at 13:18
  • As long as there are no non-inline symbols, you won't have any problems; Just copy&pasting the function implementations to the `.cpp` file without adding the `inline` modifier will result in a linker error though, if the header is included into multiple translation units. Btw: If you implement the functions directly inside the class definition, they are considered `inline`, so this may be a way to save some code... – fabian Jan 02 '23 at 13:23

0 Answers0