我的工作是使用Linux作为操作系统的嵌入式系统。这些系统不包含动态链接器,所以所有的库都必须静态包含。为了确保小的二进制大小,我需要一个小的C库。我知道有很多选项,但我的代码只使用系统调用,没有标准的C库调用、POSIX或任何其他扩展。使用一个完整的通用库是一种夸张的做法。
我可以用汇编语言编写这个库,但是我的目标硬件不是同构的。我需要在架构(ARM、i386和x86_64)和Linux内核版本之间有一定程度的可移植性。
有没有只包含Linux系统调用的小型C库?
发布于 2015-01-26 00:49:17
纯系统调用是您在ASM中所做的,最终的产品本质上是特定于体系结构的。它是C/POSIX部分,通过定义的接口对它们进行抽象
除非你的意思是你不需要一个完整的POSIX用户空间?POSIX本身很大,较低的部分涵盖了您所要求的类型。例如,sys/*.h中指定的标头。
您可以为嵌入式系统获得一个专门用于此特定目的的迷你库。你看过musl吗?或者,如果您想要更低的价格,请查看klibc。
请记住,这些标准函数要么是自举的,要么可以用C本身编写(没有包含stdlib),或者是系统特定的包装器。您也许能够通过查看这些项目的代码来找到您想要的东西。
发布于 2015-01-26 17:58:51
只需使用预先存在的标准库。因为你是静态链接,所以你只能得到你需要的东西。下面是一个在x86_64和ARM上使用多路复用的示例:
[~/ellcc/examples/write] dev% cat main.c
#include <unistd.h>
int main()
{
write(1, "hello world\n", sizeof("hello world\n"));
}
[~/ellcc/examples/write] dev% make x86_64-linux-eng
make[1]: Entering directory `/home/rich/ellcc/examples/write'
Compiling main.c
Linking write
make[1]: Leaving directory `/home/rich/ellcc/examples/write'
[~/ellcc/examples/write] dev% ./write
hello world
[~/ellcc/examples/write] dev% size write
text data bss dec hex filename
1617 32 568 2217 8a9 write
[~/ellcc/examples/write] dev% make arm-linux-engeabihf
make[1]: Entering directory `/home/rich/ellcc/examples/write'
rm -f *.o write write.bin write.log elkconfig.ld
Compiling main.c
Linking write
make[1]: Leaving directory `/home/rich/ellcc/examples/write'
[~/ellcc/examples/write] dev% ./write
hello world
[~/ellcc/examples/write] dev% size write
text data bss dec hex filename
3282 16 376 3674 e5a write
[~/ellcc/examples/write] dev% https://stackoverflow.com/questions/28138720
复制相似问题