我可以在其他c ++项目中使用c ++控制台应用程序的类吗?

问题描述:

嘿,我有一个简单的问题,
假设我有一个c ++控制台应用程序项目.
它有一些类:

Hey, just a quick question i have,
suppose i have a c++ console app project.
it has some class:

class A
{
public:
 void printHello();
}



然后在同一个控制台项目中,我得到了:



then in the same console project, i got:

void main()
{
  A a;
  a.printHello();
}



该项目已关闭,无法触摸.

现在,我正在创建一个新项目,并且我只想在其中使用A类.
我可以做吗?我应该以某种方式获取.Lib文件吗?



this project is closed and cant be touched.

now, i''m creating a new project, and i want to use only the A class in there.
can i do it? shouldnt i get a .Lib file somehow?

thanks.

假设您正在使用Visual Studio,请阅读这篇有关在Visual Studio中使用C ++创建和使用DLL的基本文章,从以下内容开始就足够了:
http://msdn.microsoft.com/en-us/library/ms235636%28v = vs.80%29.aspx [ ^ ].

这是2005年,但对于任何更高版本的Visual Studio都完全有效.如果您不使用Visual Studio,则还是有帮助的.您可以找到有关此基本主题的更多文章.

—SA
Assuming you are using Visual Studio, read this elementary article on creating and using DLLs in C++ with Visual Studio, this will be quite enough to start with:
http://msdn.microsoft.com/en-us/library/ms235636%28v=vs.80%29.aspx[^].

This is 2005, but totally valid for any later version of Visual Studio. If you are not using Visual Studio, it will be helpful anyway. You can find a lot more articles on this elementary topic.

—SA


您必须将代码链接到DLL或直接链接到您的应用程序.通常的做法是将通用代码放入DLL(这就是它们的作用),然后您可以访问DLL中的类.
You have to link the code to a DLL or directly into your application. General practice is to put common code into a DLL (that''s what they''re for), and then you can access the classes in the DLL.




为了生成Lib,必须创建一个Win32控制台应用程序,其应用程序类型为静态库".编译项目后,您将在"Debug"或"Release"文件夹中获得相关的lib.
为了重用"Lib"文件,应在新项目的链接器"选项中指定它,并添加相关的头文件.如果还需要重用该资源,则可以方便地将其制成DLL.


谢谢
Hi,

In order to generate Lib, you have to create a Win32 console application with application type as "Static Library".After compiling the project, you will get the concerned lib in "Debug" or "Release" folder.
In order to reuse the "Lib" file you should specify it in the Linker options of the new project, and add the concerned header file also.If you need to reuse the resource also, convenient to make it a DLL.


Thanks