我只是試着寫一個簡單的C程序上OSX獅子我該如何模仿OSX Lion上的GNU/linux C開發環境?
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
用gcc
$ gcc hello.c
test.c:1:19: error: stdio.h: No such file or directory
test.c: In function ‘main’:
test.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
好編譯......細
$ gcc -I /Developer/SDKs/MacOSX10.6.sdk/usr/include
ld: library not found for -lcrt1.10.6.o
collect2: ld returned 1 exit status
什麼?讓我們來看看...
$ gcc -I /Developer/SDKs/MacOSX10.6.sdk/usr/include -L/Developer/SDKs/MacOSX10.6.sdk/usr/lib test.c
$ ./a.out
hello world
終於!
這似乎是很多努力,只是爲了得到一個hello世界的工作,我如何使默認的gcc在MacOSX10.6.sdk目錄中找到庫和頭文件,就像它在Linux上一樣?在Linux上我發現ldd和ldconfig非常有用,它們似乎不存在於OSX上......是否有相當的?還有什麼其他有用的工具可用於在OSX上開發C?
是的,我知道xcode使這更容易,但假設我想使用vim和命令行工作在opensource c項目。
據我所知,你的第一次嘗試是應該工作。至少,它在我的機器上工作。 – zneak
我會嘗試檢查xcode的功能,比如它設置了哪些環境變量以及它傳遞給gcc的哪些選項,並將其複製到makefile中。這樣你就可以做'做'和瞧。 –
@zneak我還以爲它會,以及我隱約記得它工作之前,但這是我第一次嘗試編譯任何簡單的C程序,因爲升級到OSX獅子(10.7) –