Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ library
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();
}
 
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.
Posted 25-Dec-11 1:41am
Edited 25-Dec-11 1:50am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
arielbeck at 8-Feb-12 1:30am
   
not what i've asked.
i know how to do this, so u guess it needs to be recompiled as a lib.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
arielbeck at 25-Dec-11 7:00am
   
ye i know its not a common practice, but i just wondered.
so you say that the console project must be touched to change the output to lib, right?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Wouldn't creating a library imply that you need access to the code?
If so you might just as well include the class in a the new application.
  Permalink  
Comments
JackDingler at 27-Dec-11 10:59am
   
If you have an assortment of projects accessing a shared code base, making copies of the files with every change becomes unwieldy and error prone.
 
Putting the common code in libraries gives you one central place to make changes to the class.
 
This way you don't have to search your projects, and make the same changes over and over, every time you update your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 26 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100