2011-04-21 77 views
7

我想使用newlib而不是glibc來編譯小的靜態二進制文件。 (我不打算交叉編譯,因爲二進制文件將被同一臺計算機使用。)我相信我需要爲此編譯一個單獨的gcc?gcc使用newlib而不是glibc?

我的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?

+0

「我相信我需要爲此編譯一個單獨的gcc?」爲什麼?你不能只做自己單獨的鏈接步驟,鏈接到newlib而不是常規的libc嗎? – 2011-04-21 21:08:28

+1

@Chris:你還需要確保你沒有使用系統包含文件(你需要使用newlib頭文件)等等。實際上,有一個獨特的交叉編譯器比確保glibc中沒有任何內容泄漏更容易。 – 2011-04-21 23:49:55

回答

3

我強烈建議使用crosstool-ng來構建您的GCC工具鏈。如果您選擇x86,然後選擇「裸機」作爲操作系統,則可以安全地使用newlib作爲libc。

請注意,newlib確實不是而是與操作系統一起工作 - 這就像標準的東西,像IO一樣,不會開箱即用。

5

你不需要爲此重建gcc;你只需要把你現有的gcc指向正確的東西(使用-I,-L等)告訴它不要使用通常的系統東西(使用-nostdlib)。

newlib README file中標題爲「Shared newlib」的部分具有用於在本機編譯時針對共享或靜態newlib構建和鏈接的符號。