4

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

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

As I mentioned - I could add Test.hpp and Test.cpp to the project for something this simple, but I have other projects that are not so simple are require separate directories. For example, I will be creating projects on my local machine that make use of code that is stored on a server/central source control repository.
You also stated 'I'm not dealing with any libraries'. If you want to re-use code across several projects, compiling the common code as a .dll or a library as dpiskyulev suggests is the way to go.
There's nothing to stop you importing from multiple directories to a single project if that's what you meant by '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'. Just stick a ; between the directories when you enter them in the project settings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.