简体   繁体   English

C ++静态和共享库

[英]C++ static and shared libraries

I would like to create a static library in C++ to store my functions. 我想在C ++中创建一个静态库来存储我的函数。 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. 我知道这个问题已在Cplusplus论坛上提出,但我确实可以使用更准确的描述来做。

As far as I'm aware you create a new Win32 program and then add the header file (.h) and the code file (.cpp). 据我所知,您创建了一个新的Win32程序,然后添加了头文件(.h)和代码文件(.cpp)。

So in fact I have a few questions. 所以实际上我有几个问题。

1 - How do I put my code into these files? 1-如何将代码放入这些文件中? Do I use the .cpp? 我是否使用.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. 2-我确实设法制作了一个单独的带有add函数的简单库,但是在编译和构建它之后,我无法将其包含在程序中。 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. 我知道MSDN为此提供了一个教程,并且已经研究了它。 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. 关键是它使用OOP方法制作静态库,并且对库中函数的调用使用::运算符(认为是一个运算符),这是我经常要避免的事情。 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. 静态库的用户仍然需要您的头文件,但他们不再需要.CPP文件,因为实际的实现包含在您的静态库文件中。

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. 这是Microsoft演练的链接。 http://msdn.microsoft.com/en-us/library/vstudio/ms235627.aspx http://msdn.microsoft.com/en-us/library/vstudio/ms235627.aspx

How to create and use a static library using Visual Studio 如何使用Visual Studio创建和使用静态库

Here is excactly how you do it in Visual Studio 2012. 确切地说,这是在Visual Studio 2012中执行的方法。

  • To create a library, create a new C++ project. 要创建一个库,请创建一个新的C ++项目。 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 这将在解决方案的调试文件夹中创建一个.lib文件。
  • 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. 要将库与您的项目链接,请将.lib文件拖到Visual Studio中的项目中。
  • To let visual studio find your header files, right click your project. 要让Visual Studio找到您的头文件,请右键单击您的项目。 Choose Properties->Configuration properties->C/C++. 选择“属性”->“配置属性”->“ 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. 库的实现来自.lib文件,所有内容都应编译并运行良好。

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. 在Visual Studio->添加->现有项目中右键单击您的解决方案。 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. 现在,您可以在此解决方案中编辑库,然后直接在您的应用程序中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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