2

I'm normally working in c# so certain things in c++ keep confusing me alot (they seem so diffrent yet the names almost the same)

I created a Console project in which i want to run a diffrent project for testing purposes. i added the project as a reference to the console app, and then got kinda stuck.

there is no namespace 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 avoid being unable to debug through it all).

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

Community
  • 1
  • 1
Andy
  • 2,168
  • 7
  • 32
  • 56
  • 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 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
  • 230,875
  • 49
  • 477
  • 553
  • 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 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

I'll assume you're using Visual Studios:-). You have to tell the compiler where to look for its includes. Under Visual Studios, open the properties page for the project, then go to Configuration Properties->C/C++->General, and add the necessary directories in the entry Additional Include Directories. (If the other project is in the same solution, use a relative path. But I think the dialog box that pops up when you click on the button on the right does this automatically. I'm not a great fan of all this GUI stuff in general, but Microsoft seems to have done this particular part quite well.)

Once you've done this, you might have to go through a similar process for linking: this time it's under Configuration Properties->Linker->General, and the entry is called Additional Library Directories, but the principle is the same. (This may not be necessary, if you're putting all of the dll's and executables in the project in the same directory.)

James Kanze
  • 146,674
  • 16
  • 175
  • 326