首页 文章

gcc使用newlib而不是glibc?

提问于
浏览
7

我想使用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 ???

3 回答

  • 6

    你不应该为此重建gcc;你只需要将你现有的gcc指向正确的东西(使用 -I-L 等)并告诉它不要拉入常用的系统内容(使用 -nostdlib ) .

    newlib README file中 Headers 为"Shared newlib"的部分具有用于在本机编译时构建和链接共享或静态newlib的符文 .

  • 1

    我强烈建议使用crosstool-ng构建您的GCC工具链 . 如果选择x86后跟"bare metal"作为操作系统,则可以安全地使用 newlib 作为libc .

    请注意,newlib不适用于操作系统 - 这是标准的东西,比如IO,不会开箱即用 .

  • 3

    您必须告诉编译器它可以在哪里找到包含文件:

    gcc -IyourDirectoryHere -IanotherDirectoryHere

    -I(这是一个减号,后面是意大利的首都)

    更多细节:http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Directory-Options.html

    心连心

    马里奥

相关问题