简体   繁体   中英

C++ static and shared libraries

I would like to create a static library in C++ to store my functions. I'm aware this question has been asked on the Cplusplus forums but I could really use a more accurate description of what to do.

As far as I'm aware you create a new Win32 program and then add the header file (.h) and the code file (.cpp).

So in fact I have a few questions.

1 - How do I put my code into these files? Do I use the .cpp?

2 - I did manage to make a simple library with an add function alone, but after compiling and building it I was unable to #include it in a program. Why is this?

Could someone please write out a step-by-step approach to making this so I can finally do it. I am aware MSDN has a tutorial for this, and I have looked at it. The thing is it uses a OOP approach to making the static library, and the calls to the functions within the library use the :: operator (think its an operator), too often which is what I want to avoid. Would like to start simple, basically.

Thanks for any help given.

The idea of a static library, is that you write your code as usual, but you compile it as a static library. The users of a static library still need your header files, but they don't need your .CPP files anymore, because the actual implementation is contained in your static library file.

To use a library, you include the header files you need, and then link the library file with your program.

Here is a link to the microsoft walkthrough. http://msdn.microsoft.com/en-us/library/vstudio/ms235627.aspx

How to create and use a static library using Visual Studio

Here is excactly how you do it in Visual Studio 2012.

  • To create a library, create a new C++ project. In the wizard, at Application Settings, choose Static library. Uncheck Precompiled header.
  • Create your library as you want. Don't forget to declare everything in header files.
  • Compile the project as you usually would. This creates a .lib file in the debug folder of your solution
  • To use the library, create an application as you usually would.
  • To link the library with your project, drag the .lib file to your project in visual studio.
  • To let visual studio find your header files, right click your project. Choose Properties->Configuration properties->C/C++. There is a input box called Additional Include Directories. Here, you have to write the path to the header files of you library.

You can now use the header files as if they where made by your project directly. The implementation of your library is taken from the .lib file, and everything should compile and run well.

Another option, is referencing the whole libary project in your application. To do this, you must get the library project in your solution. Right click your solution in Visual Studio->Add->Existing Project. Then you have to reference the project. Right click your project->References->Common Properties->Framework and References->Add New Reference->Choose your project. Now you can edit your library in this solution and use it directly in your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM