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