我想使用 newlib 而不是 glibc 来编译小型静态二进制文件。 (我不打算交叉编译,因为二进制文件将由同一台计算机使用。)我相信我需要为此编译一个单独的 gcc ?
我编译了 gcc:
./configure --prefix=/home/myuser/Desktop/gcc-4.4.5 --libexecdir=/home/myuser/Desktop/gcc-4.4.5 --libdir=/home/myuser/Desktop/gcc-4.4.5 --with-gxx-include-dir=/home/myuser/Desktop/gcc-4.4.5 --enable-languages=c --enable-libmudflap --disable-multilib --disable-libssp --disable-nls --with-newlib --with-gnu-as --with-gnu-ld --with-system-zlib
make
它编译没有错误,但现在当我尝试编译一个简单的 Hello World!它想要使用 /usr 中的标头而不是我上面指定的路径。以下是一些错误:
In file included from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/stdio.h:34:21: error: stddef.h: No such file or directory
In file included from /usr/include/stdio.h:75,
from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/libio.h:53:21: error: stdarg.h: No such file or directory
In file included from /usr/include/stdio.h:75,
from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/libio.h:332: error: expected specifier-qualifier-list before 'size_t'
/usr/include/libio.h:364: error: expected declaration specifiers or '...' before 'size_t'
/usr/include/libio.h:373: error: expected declaration specifiers or '...' before 'size_t'
我做错了什么?是否需要编译新的 gcc,或者我可以使用现有的 gcc 并使用 newlib 而不是 glibc ???
I want to use newlib instead of glibc in order to compile small static binaries. (I do not intend to cross-compile as the binaries are to be used by the same computer.) I believe that I need to compile a separate gcc for this ?
I compiled gcc:
./configure --prefix=/home/myuser/Desktop/gcc-4.4.5 --libexecdir=/home/myuser/Desktop/gcc-4.4.5 --libdir=/home/myuser/Desktop/gcc-4.4.5 --with-gxx-include-dir=/home/myuser/Desktop/gcc-4.4.5 --enable-languages=c --enable-libmudflap --disable-multilib --disable-libssp --disable-nls --with-newlib --with-gnu-as --with-gnu-ld --with-system-zlib
make
It compiled without errors but now when I try to compile a simple Hello World! program it wants to use headers from /usr instead of the path I specified above. These are some of the errors:
In file included from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/stdio.h:34:21: error: stddef.h: No such file or directory
In file included from /usr/include/stdio.h:75,
from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/libio.h:53:21: error: stdarg.h: No such file or directory
In file included from /usr/include/stdio.h:75,
from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/libio.h:332: error: expected specifier-qualifier-list before 'size_t'
/usr/include/libio.h:364: error: expected declaration specifiers or '...' before 'size_t'
/usr/include/libio.h:373: error: expected declaration specifiers or '...' before 'size_t'
What am I doing wrong ? Is compiling a new gcc necessary or can I use my existing gcc and use newlib instead of glibc ???
发布评论
评论(3)
您不需要为此重建 gcc;您只需将现有的 gcc 指向正确的位置(使用
-I
、-L
等)并告诉它不要拉入常见的系统内容(使用-nostdlib
)。newlib 自述文件 具有用于在本机编译时构建和链接共享或静态 newlib 的符文。
You shouldn't need to rebuild gcc for this; you just need to point your existing gcc at the right things (using
-I
,-L
etc.) and tell it not pull in the usual system stuff (using-nostdlib
).The section entitled "Shared newlib" in the newlib README file has runes for building and linking against either shared or static newlib when compiled natively.
我强烈建议使用
crosstool-ng
构建您的 GCC 工具链。如果您选择 x86 和“裸机”作为操作系统,则可以安全地使用newlib
作为 libc。请注意,newlib 不能与操作系统一起工作 - 这是标准的东西,比如 IO,不能开箱即用。
I would strongly recommend using
crosstool-ng
to build your GCC toolchain. If you choose x86 followed by "bare metal" as the operating system, you can safely usenewlib
as a libc.Note that newlib does not work with an operating system - that is standard things, like IO, won't work out of the box.
您必须告诉编译器在哪里可以找到包含文件:
-I
(在意大利,减号后跟大写 i)
更多详细信息:http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Directory-Options.html
You have to tell the compiler where it can find the include files:
-I
(that is a minus followed by a capital i as in Italy)
More details: http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Directory-Options.html