1

I normally work with C#. In C# I can have a hierarchy, and they reference each other.

I am trying to do the same in VC++, but I dont know how. I don't have much experience with C++ in general.

I have:

  • TestProject1
  • TestProject2: I need to use classes that are in TestProject1 here.

I tried adding TestProject1 as reference of TestProject2:

Right Clikc on the TestProject1-> Properties -> Common Properties -> Framework and References -> Add New Reference -> Selected TestProject2

But this results in an error, because TestProject2 is not built as a lib.

From what I can see only a list of obj files are generated.

What is the correct way to reference TestProject2 in TestProject1 so I can use its classes?


Update: How I solved it

I solved the issues by following SOReader instructions, but I added the lib in a different way:

  1. First I changed the TestProject1 project type as SOReader indicated (Right Click on TestProject1 project -> Properties-> Configuration Properties -> General-> Set Configuration Type to Static library (lib)
  2. Add reference to TestProject1 in TestProject2 -> Right Click on TestProject2 project -> Properties-> Common Properties -> Framework and References -> Add New Reference -> Select TestProject1
Dzyann
  • 5,062
  • 11
  • 63
  • 95

1 Answers1

1

It's not so easy as it is in C#.

You must have TestProject1 build as static library if you want to simply include it to another project. After this you go to dependent project properties and add lib file to linker and headers folder for headers lookup.

enter image description here

enter image description here

enter image description here

Assuming Dll has its include .h file in its root folder (which actually should not have) you simply add an entry to Additional Include Directories to point the place where Dll root folder is.

Now you can just write #include <theheaderfile.h> in you cpp file in Main application to reference exported functionality.

Here are few others locations in msdn that might help: import/export, static libraries, hpp vs h

Community
  • 1
  • 1
SOReader
  • 5,697
  • 5
  • 31
  • 53
  • So, you can not simply use the obj files generated when the Test Project is generated? That is inconvenient, and is there a simple way to tell the Test project to build as a Static Library? Or do I need to create a new Static Library Project? – Dzyann Jan 08 '14 at 12:17
  • @Dzyann AFAIR you simply change output type in project's properties as it is shown on the first screenshot. – SOReader Jan 08 '14 at 12:20
  • @Dzynn More convenient in terms of components updating would be to use DLLs but this is really pain in the a** to do it in C++. import/export ms-compiler specific keyword come with help here but it is still not so nice and easy as it is in C# or Delphi – SOReader Jan 08 '14 at 12:23
  • Sorry, In this browser the screenshots are not showing for some reason. Now I can see them in my phone. Thanks! – Dzyann Jan 08 '14 at 12:23
  • yes it's a shame, I thought Microsoft had implemented something similar than with C++. Maybe in the future. – Dzyann Jan 08 '14 at 12:25
  • @Dzyann I'm a huge fan of MSVS but I would strongly suggest you use different IDE if you want to develop anything in C++. I found Eclipse really convenient. Code::Blocks saved me several times. Good luck! – SOReader Jan 08 '14 at 12:50
  • I changed the project type as you indicated, and I added a reference (Right Click on Project -> Properties -> Common Properties -> Framework and References -> Add New Reference -> Selected the project with the static lib) and it is working. I didn't reference the lib like in your images. Do you think is ok to do it like this too? It is the way I am referencing another Static Library I have, and test are running fine. – Dzyann Jan 08 '14 at 14:30
  • @Dzyann If it works then leave it as it is. It might be possible something was changed in msvs for c++ through these couple of years when I didn't use c++. The description in my answer is the way I always added references. – SOReader Jan 08 '14 at 17:22