How do I run a class from another project?
I am fairly new to c++ and I am trying to figure out the best way to run a class from another project in my solution.
Lets say I have 2 projects in my solution: proj_A and proj_B.
Now I would like to create an instance of a class in proj_B from inside proj_A. I have tried including the headers and source files from proj_B to proj_A. The problem is that the source files in proj_B have a lot of dependencies and I would like to keep those out of proj_A. Mainly because I feel that that would be like merging the two projects together. Maybe I am going about things completely the wrong way. (I have a c# background, I am looking for something that is equiv开发者_开发问答alent to adding a reference to another project)
Please help me out here. =)
You can build a DLL and use it. Here's a link at MSDN about how to do so.
In C++, you have two options for splitting your application into modules. You can you use Libraries, which are linked directly into your executable, or you can use DLLs, which you are familiar with from C#.
It sounds like you are using DLLs. If that is the case, you need to export your classes from the DLL (proj_B) that you want to use in your main executable (proj_A).
http://www.codeproject.com/KB/cpp/howto_export_cpp_classes.aspx
Since you are worried about exposing dependencies, I suggest you consider option 3 from this code project article. I've used all three approaches described in the article, and option 3 has become my preferred method.
精彩评论