简体   繁体   English

简单的图书馆问题(C ++)

[英]Simple Library Questions (C++)

1. Are shared libraries and dynamic libraries the same exact thing? 1. 共享库和动态库是否完全相同?
windows just labels them as .dll , and linux labels them as .so ? windows只是将它们标记为.dll ,linux将它们标记为.so

2. If a shared libarary has a ton of functions that a program uses, when are those functions loaded into memory? 2.如果共享库具有程序使用的大量函数,那么这些函数何时加载到内存中?
At the start of the program? 在计划开始时? when the particular function is actually called? 什么时候实际调用特定的函数?

3. If I make a library like this: 3.如果我创建这样的库:

#ifndef SHARED_H
#define SHARED_H

#include <iostream>
#include <string>

namespace shared
{
  void Function1(void);
  void Function2(void);
  void Function3(void);
  void Function4(void);
  void Function5(void);
  void Function6(void);
  ...
  void Function99(void);
  void Function100(void);
  ...
}
//assume none of these functions call each other

#endif

and my client program only calls one of those functions, will their be performance decrease because of all the other extra functions not used? 并且我的客户端程序只调用其中一个函数,由于未使用的所有其他额外函数,它们的性能会降低吗?
Not worried about compilation time.. just the actual running time 不担心编译时间..只是实际的运行时间

4. Is question 3's scenario different if I use a class: 4.如果我使用课程,问题3的情况会有所不同:

#ifndef SHARED_H
#define SHARED_H

#include <iostream>
#include <string>

class Shared
{
  public:
  void Function1(void);
  void Function2(void);
  void Function3(void);
  void Function4(void);
  void Function5(void);
  void Function6(void);
  ...
  void Function99(void);
  void Function100(void);
  ...
  private:
  protected:
};
//assume none of these functions call each other

#endif

5. I'm use to making a lot of objects(.o files), and then linking them together to make my executable. 5.我使用,以使大量的对象(.o文件),然后将它们连接在一起,使我的可执行文件。 would it be better to turn all of my objects(which are usually classes) into a .so file AND THEN link them together? 将所有对象(通常是类)转换为.so文件然后将它们链接在一起会更好吗?
I understand that the executable will rely on the .so file, unlike the first approach where the objects can just be deleted after compilation, but what is the recommendation on this? 我知道可执行文件将依赖于.so文件,不像第一种方法可以在编译后删除对象,但对此有何推荐?

6. I'm a bit in the dark about the difference between -fPIC and -fpic 6.关于-fPIC和-fpic之间的区别,我有点不知所措
I've heard that -fPIC will always work and -fpic is target-dependent. 我听说-fPIC将始终有效,-fpic依赖于目标。
What does target dependent mean? 目标依赖意味着什么? If the library is always going to be compiled and used on the same machine, am I safe to use -fpic? 如果库总是要在同一台机器上编译和使用,我可以安全地使用-fpic吗?

some of these questions may be trivial, but I want to be certain about the things I've read so far. 其中一些问题可能是微不足道的,但我想确定我到目前为止所阅读的内容。 I appreciate any and all responses 我感谢任何和所有的回应
*If relevant: using gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) *如果相关:使用gcc版本4.6.1(Ubuntu / Linaro 4.6.1-9ubuntu3)

For 1st and 2nd question : In Windows Xp threads have dll's(Dynamic link libraries) as component.As a thread can be considered as light process. 对于第一和第二个问题:在Windows Xp线程中有dll(动态链接库)作为组件。作为一个线程可以被视为轻量级进程。 A process is nothing but program in Execution.(run time).I guess dll and .so are similar (there might exist variation ,not sure) 一个过程只不过是执行中的程序。(运行时)。我猜dll和.so是相似的(可能存在变化,不确定)

External libraries are usually provided in two forms: static libraries and shared libraries. 外部库通常以两种形式提供:静态库和共享库。 Static libraries are the '.a' files . 静态库是'.a'文件。 When a program is linked against a static library, the machine code from the object files for any external functions used by the program is copied from the library into the final executable. 当程序链接到静态库时,程序使用的任何外部函数的目标文件中的机器代码将从库中复制到最终的可执行文件中。

An executable file linked against a shared library contains only a small table of the functions it requires, instead of the complete machine code from the object files for the external functions. 链接到共享库的可执行文件仅包含它所需的函数的小表,而不是来自外部函数的目标文件的完整机器代码。 Before the executable file starts running, the machine code for the external functions is copied into memory from the shared library file on disk by the operating system—a process referred to as dynamic linking.Dynamic linking makes executable files smaller and saves disk space,because one copy of a library can be shared between multiple programs. 在可执行文件开始运行之前,外部函数的机器代码由操作系统从磁盘上的共享库文件复制到内存中 - 这个过程称为动态链接。动态链接使可执行文件更小并节省磁盘空间,因为可以在多个程序之间共享库的一个副本。

For 3rd question : since in shared library only table of functions gets loaded not the actual mnemonics of code so we are saving data unlike static library where mnemonics are loaded at compile time.Which can be inferred from above explanation. 对于第三个问题:因为在共享库中只有函数表被加载而不是代码的实际助记符所以我们保存数据不像静态库,其中助记符是在编译时加载的。这可以从上面的解释中推断出来。

For 5th question : I think it will be better to make them as '.so' only when you know that you are going to those functions rarely.if you are going to use those function often then make those functions containing lib as static.As fetching them at run time increases responsive time . 对于第5个问题:我认为只有当你知道你很少去那些函数时才会把它们变成'.so'更好。如果你打算经常使用这些函数,那么把那些包含lib的函数作为static.As在运行时获取它们会增加响应时间。

  1. Under Win, dll's are dynamic linked libraries, meaning they get loaded separately in memory at run-time, unlike static linked libraries ( lib's ) that are embedded in your module during compilation. 在Win下, dll's是动态链接库,这意味着它们在运行时被单独加载到内存中,这与编译期间嵌入模块中的静态链接库( lib's )不同。

  2. Under Win, before the program starts. 在Win下,程序开始之前。 If it can't find a dll it needs, it will report an error message and exit. 如果找不到它需要的dll,它将报告错误消息并退出。 That's unless you dynamically try to call functions and not actually link against the library, via LoadLibrary and GetProcAddress . 除非你通过LoadLibraryGetProcAddress动态尝试调用函数而不实际链接到库,否则这是不可能的。

  3. No. The functions have a well-known place in memory when the library is loaded. 不可以。加载库时,函数在内存中有一个众所周知的位置。 There is only one jmp or call instruction per function call regardless of how many functions there are. 无论函数有多少,每个函数调用只有一个jmpcall指令。

  4. Also no. 也没有。 Most probably those functions will be represented as non-member functions that take this as an extra parameter. 很可能这些函数将表示为非成员函数,将this作为额外参数。

  5. The main reason would be re-usability. 主要原因是可重用性。 If you have a functionally independent module that contains multiple object files, you might as well group them together. 如果您有一个包含多个目标文件的功能独立模块,您也可以将它们组合在一起。 That way you can re-use it more easily because you'd be linking to just one library instead of multiple object files. 这样您就可以更轻松地重复使用它,因为您只链接到一个库而不是多个目标文件。

  6. ??? ???

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

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