如何使用库 [英] How to use Libraries

查看:177
本文介绍了如何使用库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我从来不能使用任何语言的外部库。我正在寻找如何使用外部库的说明/解释,以及它们如何工作。当我在线搜索时,我得到的片段,似乎不适用于任何库下载,尝试和使用。我工作在一个mac和一个pc,和C ++的例子是好的。我使用eclipse IDE与C + +插件。如果有指令适用于所有的库,将是巨大的。

For some reason I'm never able to use external libraries in any language. I'm looking for instructions/explanations of how to use external libraries, as well as how they work. When I search online, I get fragments that never seem to apply to whatever library I download and try and use. I work on both a mac and a pc, and C++ examples are fine. I use eclipse IDE with the C++ plug in. If there are instructions that apply to all libraries that would be great.

推荐答案

假设您有一个类 Unuseful ,定义如下:

Say you have a class Unuseful defined as follows:

档案 Unuseful.h

class Unuseful {
public:
    void printUnusefulStatement();
};

档案 Unuseful.cpp

#include "unuseful.h"
#include <iostream>

void Unuseful::printUnusefulStatement()
{
    std::cout << "Hello world!" << std::endl;
}



现在,您有另一个类需要打印无用的语句:

Now, you have another class that needs printing unuseful statements:

Unuseful u;
u.printUnusefulStatement();

这意味着你想使用包含特定实现的外部库(

This means that you want to use an external library containing the specific implementation (printUnusefulStatement) that you want to include in your code.

您可以通过两种方式使用此库:

You may use this library in two ways:


  1. 通过向编译器提供源代码

  2. 通过提供一个二进制文件链接器



案例1:在编译时使用库



是最简单的情况。
你有你必须使用的库的源代码,你只需要与你现有的代码(例如 main.cpp 文件)一起编译。
通常你是库的作者和用户(一个完成你需要的任务的类)。

Case 1: using a library at compile time

This is the simplest case. You have the source code of the library you have to use and you simply have to compile it together with your existing code (say main.cpp file). Typically you are the author and user of the library (a class that accomplishes a task you need).

使用这个命令编译:

g++ main.cpp unuseful.cpp

允许您在 main.cpp 文件中使用所需的实现。

allows you to use the implementation you need in your main.cpp file.

比较常见的情况1 ,您没有要使用的库的源代码。您只有头文件( Unuseful.h ,继续使用示例)和静态共享库(分别可能是[*] libunuseful.a libunuseful.so 文件)。

More often than Case 1, you don't have the source code of the library you want to use. You only have the header file (Unuseful.h, to continue with the example) and a static or shared library (probably[*] libunuseful.a and libunuseful.so files, respectively).

静态库是在最终可执行文件中链接的对象文件( *。o )的归档,共享库被动态加载 - 在运行时(请参阅此网页,以便更好地了解差异)。

The static library is an archive of object files (*.o) that are linked inside your final executables, the shared libraries instead are loaded dynamically - at run time (look at this page for a better understanding of the difference).

静态库是通过将 *。o 文件与 ar 程序:

Static libraries are created by simply archiving the *.o files with the ar program:

# Create the object files (only one here)
g++ -c unuseful.cpp
# Create the archive (insert the lib prefix)
ar rcs libunuseful.a unuseful.o

共享库使用 g ++ -shared 选项创建:

Shared libraries are created with the g++ -shared option:

# Create the object file with Position Independent Code[**]
g++ -fPIC -c unuseful.cpp
# Crate the shared library (insert the lib prefix)
g++ -shared -o libunuseful.so unuseful.o

让我们假设现在你有 Unuseful.h 文件和共享库( libunuseful.so 文件),你有一个文件,它实例化一个 Unuseful 对象并调用 printUnusefulStatement

Let's suppose now you have the Unuseful.h file and the shared library (libunuseful.so file) and you have a main.cpp file that instantiates a Unuseful object and calls the printUnusefulStatement method.

如果你试图编译这个文件( g ++ main.cpp ),链接器会报错,因为它不能找到 printUnusefulStatement 符号。

If you try to compile this file (g++ main.cpp) the linker will complain because it cannot find the printUnusefulStatement symbol.

是时候使用库:

g++ main.cpp -L. -lunuseful

选项说明了 -L 链接器在其中搜索库文件,并且 -l <​​/ code>标志告诉链接器要使用的库的名称(没有 lib 前缀)

The -L option tells the linker where to search for library files and the -l flag tells the linker the name of the libraries to be used (without the lib prefix).

现在可执行文件( a.out ,因为我没有指定不同的名称),并且您已使用库实现所需的功能( printUnusefulStatement )。

Now the executable (a.out, because I didn't specify a different name) is created, and you have used a library to implement a functionality you needed (printUnusefulStatement).

共享库在运行时加载,因为系统无法找到该库,所以执行 a.out 可能会失败。
通常可以通过适当地设置一个环境变量来指示使用哪个路径来搜索动态库来解决这个问题:

Since the shared library is loaded at run-time, the execution of the a.out executable may fail because the system is not able to find the library. Typically this can be solved by appropriately setting an environment variable indicating which paths to use to search for dynamic libraries:

# Set the LD_LIBRARY_PATH [*]
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.

完成后,现在您的可执行文件已经编译,它将能够运行和加载它需要的库。

Done, now your executable has been compiled and it will be able to run and load the library it needs.

这是一个关于库的快速概述,希望能帮助您了解如何使用它们

This is a rapid overview on libraries which I hope can help you understand how they are used and provided to others.

如果您有兴趣,还有许多方面需要详细调查: g ++ ar 选项,环境变量,共享库格式等。

There are many many aspects that should be investigated in more detail, if you are interested: g++ options when creating shared libraries, ar options, environment variables, the shared libraries format and so on.

[*]:在Unix环境中

[**]:如果支持目标机器, ,适合于动态链接并且避免对全局偏移表的大小的任何限制。此选项在m68k,PowerPC和SPARC上有所不同。位置无关代码需要特殊支持,因此只能在某些机器上工作。 [从g ++手册页]

这篇关于如何使用库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆