Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio link error when including headers from another directory

The short question is: How can I use Visual Studio to create/compile/run projects that have source code in different directories.

Here are the specific details:

I have an class definition file (.hpp) and 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 put main.cpp in that project. I added the class file directory to Additional Include Directories (right click on the project -> project properties -> Configuration Properties - C/C++ - General -> Additional Include Directories = C:\Test\cpp).

When I do this, intellisense works fine when editing main.cpp. When I Build the 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 in function _main.

As far as I can tell, Visual Studio isn't actually compiling Test.cpp (I don't see any .obj files being made for it). I have tried compiling it separating, before Compiling/Building main.cpp, but that didn't make any difference. Is there a way around this? When I search the web for answers, I see a lot of people forgetting to include libraries for the linker, but I'm not dealing with any libraries.

I have also verified this code compiles and runs correctly by moving Test.hpp and Test.cpp to the same directory as main.cpp - that worked without any problems. I would just stick with that setup, but I need to be able to use source from different directories for a project I'm working on.

Here are the 3 files:

  1. 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
    
  2. C:\Test\cpp\Test.cpp

    #include "Test.hpp"
    
    Test::Test()
    {
      mynum = 0;
    }
    
    int Test::add(int num)
    {
      return mynum += num;
    }
    
  3. C:\Visual Studio Projects\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;
    }
    
like image 970
nexus-bytes Avatar asked Dec 28 '22 02:12

nexus-bytes


1 Answers

The bottom line is that you have to compile Test.cpp and link with it somehow. Two options:

  1. Add Test.cpp to your project
  2. Create a library project, say TestLib. Add Test.cpp to that project and compile it. Then link your main project with the TestLib.

In second case you can have dependent projects in VisualStudio. This way it will properly pull proper Debug/Release/etc. version of the lib when you build your main project. It will also get it from the right folder. E.g. no need to mess with linker settings manually.

like image 79
dpiskyulev Avatar answered Feb 04 '23 01:02

dpiskyulev