Stefan Meller
Stefan Meller

Reputation: 11

Access to cpp file in another project in Visual Studio 2019

I have two C++projects(Visual Studio Community Edition 2019) called Game(e) and Tester in a solution map. Tester is a subdirectory of Game. Both projects creates exe files. The projects Game hast two cpp and two header files(Foo.cpp, Foo.h, Foo2.cpp and Foo2.h).

Foo has two methods add1 and add2. Foo2 has the method plus_one. The method add2 from Foo uses plus_one. In the project Tester there is a file called Main.cpp. I want to use all the files from Gamer in the project Tester. I have added ".." to "Additional Include Directories" so that I can include Foo.h in Tester\Main.cpp. I have added a reference to Games. The problem is that the linker doesn't like it:

Error   LNK2019 unresolved external symbol "public: int __thiscall Foo::add(int,int)" (?add@Foo@@QAEHHH@Z) referenced in function _main
Error   LNK2019 unresolved external symbol "public: int __thiscall Foo::add2(int,int)" (?add@Foo@@QAEHHH@Z) referenced in function _main

Does anybody know how to fix it? One solution is to import both cpp-files into the Tester project. But this solution is not acceptable for me. I am interested in a solution without importing all cpp files. I have uploaded it on github: Github Link

Upvotes: 0

Views: 759

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148965

A possible way is to directly add the required object files as linker input for the Tester project.

  1. Open the properties page with a right click on Tester project then Properties

  2. Select Linker then Input

  3. In Additional dependencies add (through Edit...)*:

     $(SolutionDir)$(Platform)\$(Configuration)\Foo.obj
     $(SolutionDir)$(Platform)\$(Configuration)\Foo2.obj
    

And everything should work as expected. As the main project is a dependency of the Tester one, VS will build it (if required) before trying to build Tester, and this is enough to ensure that the objects will be up to date.


* The path is the path of the intermediate directory of the main project. As you have chosen to put the main project and the solution in the same folder, the path is directly the solution path. If the main project was in its own folder, you would have to add that folder to the path.

Upvotes: 0

Related Questions