Search code examples
c++visual-c++reference

How can you Call a method from a diffrent Project, both in C++?


When using Visual Studio I normally work in C# so certain things in C++ confuse me (the concepts seem so different yet the names are almost the same)

I created a Console project in which I want to run a different project for testing purposes. I added the project as a reference to the Console App, and then got stuck

There are no namespaces in the projects, so I can't do a using and if I try to include the other file, it cannot find it (and I want to be able to debug through the code in all projects)

The code for the class can be found here (ignore the C# part), it is just a standard console module with nothing in it yet


Solution

  • Yeah, C++ doesn't have the notion of assemblies that exists in C# and .NET. It makes tasks like this slightly more difficult, a virtue of the fact that C++ compiles directly to native code.

    Instead, you'll generally #include the necessary header files (*.h) at the top of your code file, and instruct the linker to link to the appropriate .lib file(s). Do that by going to your project's Properties, selecting Linker -> Input, and adding the file to the "Additional Dependencies" section.

    As an alternative to linking to the .lib file, you can use Visual Studio to add a reference to the other project, if it's part of the same solution. Microsoft has a walk-through on creating and using a dynamic link library in C++ that might be worth a read.