简体   繁体   English

带类的库如何工作?

[英]How do libraries with classes work?

When learning C++ in school we never really talked about how to build libraries, so sorry for my rudimentary understanding. 在学校学习C ++时,我们从未真正谈论过如何构建库,因此,对我的基本理解感到抱歉。 From what I've read online, it seems like a library is just a collection of code that is already compiled, and then there is a .h file that lists what functions are accessible in that library. 从我在网上阅读的内容来看,似乎一个库只是一个已编译代码的集合,然后有一个.h文件列出该库中可访问的功能。

For example when I #include <cmath> I can now call sin(x) without having access to the cmath code to compile it. 例如,当我#include <cmath>我现在可以调用sin(x)而无需访问cmath代码进行编译。 My question is if this works with classes that have data in them. 我的问题是,这是否适用于其中包含数据的类。

So can I create a library 我可以建立一个图书馆吗

//AccumulatorLibrary.h
class Accumulator
{
public:
    int num;
    int increment() {num++};
    void otherFunctions(); //otherFunctions defined in the .lib file
}

And then call it 然后叫它

//Main
#include "AccumulatorLibrary.h"
#include <stdio>
int main()
{
    Accumulator A(0); //initalize num to 0
    Accumulator B(7); //initalize num to 7
    cout<<A.increment;
    cout<<B.increment;
    cout<<A.increment;
}

and get an output of 1 8 2 ? 并得到1 8 2的输出?

In summary, if I figure out how to put a bunch of classes into a library file can I access any data I want to, as long as that data has an access function in the .h file? 总之,如果我想出了如何将一堆类放入库文件中,只要该数据在.h文件中具有访问功能,我是否可以访问我想要的任何数据?

Or a more basic question, do a .h and .lib file work exactly the same as regular c++ code except that it doesn't have to be compiled when you use it, and you don't have access to the code in the .lib file? 或更基本的问题是, .h.lib文件的工作原理与常规c ++代码完全相同,除了使用时不必编译该文件以及您无权访问.c文件中的代码.lib文件?

From what I've read online, it seems like a library is just a collection of code that is already compiled, and then there is a .h file that lists what functions are accessible in that library. 从我在网上阅读的内容来看,似乎一个库只是一个已编译代码的集合,然后有一个.h文件列出该库中可访问的功能。

Correct. 正确。

My question is if this works with classes that have data in them. 我的问题是,这是否适用于其中包含数据的类。

It does. 是的 A lot of C++ libraries expose classes and have their code precompiled in a library. 许多C ++库公开类,并在库中预编译其代码。

Or a more basic question, do a .h and .lib file work exactly the same as regular c++ code except that it doesn't have to be compiled when you use it... 或更基本的问题是,.h和.lib文件的工作原理与常规c ++代码完全相同,只是使用时不必编译它。

Wait, wait. 等等。 .h files still contain C++ code (declarations and sometimes even inline implementations). .h文件仍然包含C ++代码(声明,有时甚至是内联实现)。 .lib files are dynamically linked libraries. .lib文件是动态链接的库。 They're the result of the compilation (and linkage) of the C++ source files. 它们是C ++源文件的编译(和链接)结果。

...and you don't have access to the code in the .lib file? ...并且您无权访问.lib文件中的代码?

You do have access to it: open it using a disassembler. 您确实具有访问权限:使用反汇编程序将其打开。 It just won't be C++ anymore. 它将不再是C ++。

From what I've read online, it seems like a library is just a collection of code that is already compiled 根据我在网上阅读的内容,图书馆似乎只是已编译的代码的集合

Yes and no. 是的,没有。

To me, a "library" is a body of code (one or more header ( .h) files and zero, one, or more source ( .cpp ) files), without the main() function, that can be independently compiled and linked (except for main() ). 对我来说,“库”是一个代码主体(一个或多.h)文件( .h)和零个,一个或多个源文件( .cpp )),而没有main()函数,可以独立地编译和链接( main()除外)。

A library may be available in various mechanisms: 库可以通过各种机制使用:

  • Available as source: Here you have to compile the library along with your application. 可作为源使用:在这里,您必须与应用程序一起编译库。 Examples are C++ standard template library, Boost C++ libraries, a library that you or your colleague wrote, a library that you downloaded from sourceforge etc. (Caveat: Sometimes source-based libraries can be pre-compiled in a system as a compilation optimization.) 例如C ++标准模板库,Boost C ++库,您或您的同事编写的库,从sourceforge下载的库等。(注意:有时,基于源的库可以作为编译优化而预先编译在系统中。 )

    Note that, being available as source means you can read it, but not necessarily modify it. 请注意,作为源可用意味着您可以阅读它,但不必修改它。

  • Available as binary: Here, the library is already compiled and perhaps available in your system. 以二进制形式提供:在这里,该库已经编译,并且可能在您的系统中可用。 Examples are C standard library, C++ standard library, C math library etc. From the question, it sounds like that this is the one you are referring to. 示例包括C标准库,C ++标准库,C数学库等。从问题上看,这听起来像是您所指的那个。

then there is a .h file that lists what functions are accessible in that library. 然后有一个.h文件,列出了该库中可访问的功能。

True, that is the case for a C library. 没错,C库就是这种情况。 For C++, the concept is naturally extended to include the classes and (public) member functions. 对于C ++,该概念自然扩展为包括类和(公共)成员函数。

In summary, if I figure out how to put a bunch of classes into a library file can I access any data I want to, as long as that data has an access function in the .h file? 总之,如果我想出了如何将一堆类放入库文件中,只要该数据在.h文件中具有访问功能,我是否可以访问我想要的任何数据?

Yes! 是!

Or a more basic question, do a .h and .lib file work exactly the same as regular c++ code except that it doesn't have to be compiled when you use it, and you don't have access to the code in the .lib file? 或更基本的问题是,.h和.lib文件的工作原理与常规c ++代码完全相同,除了使用时不必编译该文件以及您无权访问.h和.lib文件。 lib文件?

A library follows the same rules of C++, the only difference as mentioned above, is that it does not have a main() function. 库遵循C ++的相同规则,但如上所述,唯一的区别是它没有main()函数。

Whether you need to compile or not depends on the way it is available to you (see above). 是否需要编译取决于可用的方式(请参见上文)。

For many libraries, you have access to the source code (see above). 对于许多库,您可以访问源代码(请参见上文)。

Following is the full code for your example: 以下是示例的完整代码:

// AccumulatorLibrary.h
class Accumulator {
  public:
    Accumulator( int x ) : num( x ) {}    // ctor with initializer
    int increment() {num++};
    int get() const;
    void set( int x );
  private:
    int num;
};
// AccumulatorLibrary.cpp
int Accumulator::get() const { return num; }
int Accumulator::set( int x ) { num = x; }

// Usercode.cpp
#include "AccumulatorLibrary.h"
#include <iostream>
using namespace std;
int main() {
    Accumulator A(0); //initalize num to 0
    Accumulator B(7); //initalize num to 7

    A.increment();
    cout << A.get() << endl;        // print 1

    B.increment();
    cout << B.get() << endl;        // print 8

    A.increment();
    cout << A.get() << endl;        // print 2
}

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

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