Visual Studio link error when including headers fr

2019-05-11 14:14发布

问题:

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;
    }
    

回答1:

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.



回答2:

Add Test.h and Test.cpp to the project (you should be able to just drag the files from explorer).

You need the additional include directory specified as well but it sounds like you've already got that sorted.