Visual Studio Link Error When Including Headers from Another Directory

Short question: How can I use Visual Studio to create / compile / run source projects in different directories.

Here are the specific details:

I have a class definition file (.hpp) and an implementation file (.cpp) in one directory, and I have a main.cpp file in another directory. In Visual Studio 2008, I created a new project and placed main.cpp in this project. I added the class file directory to the additional Include directories (right-click on the project โ†’ project properties โ†’ Configuration properties - C / C ++ - General โ†’ Additional directories Include = C: \ Test \ cpp).

When I do this, intellisense works fine when editing main.cpp. When I create a project, it compiles fine, but I get link errors, such as:

error LNK2019: unresolved external symbol "public: int __thiscall Test :: add (int)" (? add @Test @@ QAEHH @Z) referenced by the _main function.

As far as I can tell, Visual Studio does not actually compile Test.cpp (I do not see any .obj files for it). I tried to compile it before compiling / building main.cpp, but that didn't make any difference. Is there any way around this? When I search the Internet for answers, I see that many people forget to include libraries for the linker, but I don't deal with any library.

, , Test.hpp Test.cpp , main.cpp, - . , , .

3 :

  • C:\Test\CPP\Test.hpp

    #ifndef TEST_H
    #define TEST_H
    
    class Test
    {
    private:
      int mynum;
    
    public:
      Test();
      int add(int num);
    
    };
    #endif //TEST_H
    
  • C:\Test\CPP\test.cpp

    #include "Test.hpp"
    
    Test::Test()
    {
      mynum = 0;
    }
    
    int Test::add(int num)
    {
      return mynum += num;
    }
    
  • C:\ Visual Studio\MyProject\main.cpp

    #include <iostream>
    #include <Test\Test\Test.hpp>
    
    int main(int argc, char *argv[])
    {
      Test test;
      std::cout << "Add 5 = " << test.add(5) << std::endl;
      return 0;
    }
    
+3
2

, Test.cpp - . :

  • Test.cpp
  • , TestLib. Test.cpp . TestLib.

VisualStudio. , Debug/Release/etc. lib . . . .

+5

Test.h Test.cpp ( ).

include, , .

0

All Articles