2

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

Neoheurist
  • 3,183
  • 6
  • 37
  • 55
Andy
  • 2,248
  • 7
  • 34
  • 57
  • 1
    You want to call a method written in C++ from a C# project? Or vice versa? Or code from one C++ project in a second C++ project? – Cody Gray - on strike Apr 15 '11 at 09:39
  • @Cody Gray No, this time both are in C++. I asked the the mixed language yesterday and that works but strangly i fail to do it in 2 c++ projects. i edited the question to avoid the confusion on it. – Andy Apr 15 '11 at 09:44

2 Answers2

3

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • if i do this, will i be able to 'step into' the other project which is now a dll? – Andy Apr 15 '11 at 10:02
  • @Andy: Yes, all that's required for that are the symbol files. The ones with the extension `.pdb`, same as those found in C#. I'm pretty sure this works right out-of-the-box, though my memory's a little foggy as I haven't done this in a while. – Cody Gray - on strike Apr 15 '11 at 10:27
  • Yeah, I debug across multiple DLLs on a daily basis, as long as they're all built with debug information (i.e. the .pdb file) and by the same compiler+linker, debugging across multiple DLLs should work out of the box. – Mephane Apr 15 '11 at 10:57
2

You have to tell the compiler where to look for includes. In Visual Studio, open the properties page for the project, then add the necessary directories by going to:

Configuration Properties -> C/C++ -> General -> Additional Include Directories

If the other project is in the same solution, use a relative path. I think the dialog box that pops up when you click on the button on the right does this automatically.

You might have to go through a similar process for linking:

Configuration Properties -> Linker -> General -> Additional Library Directories

Note: this may not be necessary if you're putting the DLLs and executables in the same directory.

Neoheurist
  • 3,183
  • 6
  • 37
  • 55
James Kanze
  • 150,581
  • 18
  • 184
  • 329