【发布时间】:2016-09-23 09:40:45
【问题描述】:
我正在写诗的图书馆是为了好玩,但我在设计我的任务时遇到了困难。我有一个静态库,我想制作一个“静态插件”系统(请原谅这个名字)。
所以,我想创建一系列遵守以下规则的静态库:
- 每个库都包含一个对象(一个类或任何需要的对象)
- 类必须包含一个数组
- 每个数组的项都是
unsigned char的数组
本质上,我想创建一系列库,在此任务中提供诗歌列表。因此,每个图书馆都包含一位作者的诗歌。
现在我需要的部分是:库用户将只链接所需的静态插件和主库。有了这个,用户可以在控制台打印整个诗歌存储库,就像这个简单的例子一样:
#include "poems.hpp"
int main(int argc, const char * argv[])
{
poems p;
p.dump(">>> Dumping poems");
return 0;
}
所有过程都在链接时处理
$ clang++ a.cpp libpoems.a libplugin_coleridge.a
$ ./a.out
>>> Dumping poems
The Rime of the Ancient Mariner
It is an ancient Mariner,
And he stoppeth one of three.
[...]
Kubla Khan
In Xanadu did Kubla Khan
A stately pleasure-dome decree:
[...]
或替代
$ clang++ a.cpp libpoems.a libplugin_shelley.a
$ ./a.out
>>> Dumping poems
Ozymandias
O wild West Wind, thou breath of Autumn's being,
Thou, from whose unseen presence the leaves dead
[...]
Ode To The West Wind
O wild West Wind, thou breath of Autumn's being,
Thou, from whose unseen presence the leaves dead
[...]
我想我可以在poem.hpp 中创建一些包含对外部对象或类似对象的调用的类或类似内容。
欢迎任何提示。
【问题讨论】:
-
所以你可以在库中放置一个函数来进行转储,每个库的名称相同,并由链接器解析。那么这里的问题是什么?
-
不仅仅是一个函数,还包括转储所需的所有数据,或者任何其他函数(例如,搜索一首诗)所需的所有数据。
标签: c++ static-libraries static-linking