简体   繁体   English

如何使用系统库创建静态库?

[英]How create static library using system libraries?

I wrote a simple C++ program using ntl libraries.我使用ntl库编写了一个简单的C++程序。 I try to create a static library from my program.我尝试从我的程序创建一个静态库。 I used these commands :我使用了这些命令:

g++ -Wall -g -c base.cpp -o base.o
ar rcs libMyStaticLib.a *.o

libMyStaticLib.a was created successfully. libMyStaticLib.a已成功创建。 But when I used libMyStaticLib.a in another project I get these error:但是当我在另一个项目中使用libMyStaticLib.a ,我收到以下错误:

g++ -o main.out main.cpp -lMyStaticLib
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libMyStaticLib.a(base.o): In function `NTL::Vec<NTL::GF2>::~Vec()':
/usr/local/include/NTL/vec_GF2.h:43: undefined reference to `NTL::WordVector::~WordVector()'

my main.cpp我的 main.cpp

#include <iostream>
#include </home/Qwer/test/base.h>

int main()
{
    baseInit();
    return 0;
}

I try to link ntl library while creating static library我在创建静态库时尝试链接ntl

ar rcs libMyStaticLib.a *.o -lntl 

But I get this error :但我收到此错误:

ar: two different operation options specified 

I want to try static library and use it in anoother project.我想尝试静态库并在另一个项目中使用它。 How should I so this ?我该怎么这样呢?

Static libraries are nothing more that archives (that's what the ar program creates, and the .a suffix stands for) of object files.静态库只不过是目标文件的存档(这是ar程序创建的, .a后缀代表的)。 Linking with a static library is like linking with the objects files inside the archive.链接静态库就像链接存档中的对象文件。

That's why all other libraries that your static library depends on also must be linked:这就是为什么您的静态库所依赖的所有其他库必须链接的原因:

$ g++ -o main.out main.cpp -lMyStaticLib -lntl

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

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