2012-08-07 82 views
3

我想在OSX山獅編譯一個c文件。在Xcode 4.4中,首選項 - >下載我可以安裝命令行工具來做到這一點。然而在該窗格其通知我,我可以改用xcrun:xcrun gcc找不到頭文件

Before installing, note that from within Terminal you can use the XCRUN tool to launch compilers and other tools embedded within the Xcode application. Use the XCODE-SELECT tool to define which version of Xcode is active. Type "man xcrun" from within Terminal to find out more.

我很樂意這樣做的,但我無法得到它找到stdio.h中。

$ xcrun gcc hello.c 
hello.c:1:19: error: stdio.h: No such file or directory 

我絕對有這個文件我的系統上後,通過標準的應用程序商店安裝的Xcode: /Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10 .8.sdk/usr/include目錄/文件stdio.h

我試圖指定SDK,但得到了同樣的錯誤:

$ xcrun -sdk /Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk gcc hello.c 

一些谷歌搜索導致我嘗試上述命令之前運行的Xcode選,但沒有運氣。無論如何,我只有一個版本的Xcode。

$ sudo xcode-select -switch /Applications/Xcode.app 

我怎樣才能得到它找到的頭?

最終我放棄了並安裝了命令行工具,但很高興知道如何使用內置的Xcode工具來完成它。

注:這裏是我試圖編譯文件:

#include <stdio.h> 

int main() { 
    printf("hello world!\n"); 
    return 0; 
} 

回答

4

您必須指定編譯非標準SYSROOT。

使用鐺/ LLVM(這是新的標準),這會做的伎倆:

xcrun clang --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/ -o hello hello.c 

如果你想堅持用gcc只需更換「鐺」與「海灣合作委員會」。

2

大概無法與此有直接的幫助,但是你可以設置別名常用xcrun命令,這樣,當其他程序調用GCC,製作,GDB,混帳等的Xcode的版本習慣:

alias gcc='xcrun gcc' 

如果您願意,可以將別名放入.bashrc文件中,以便它可以保留,並根據需要從.bash_profile提供。

所有這一切的優點是,您不需要安裝Xcode命令行工具,也可以避免使用軟件包管理器,節省空間,降低複雜性並利用Xcode自動更新這些工具。

+0

是的,這是一個非常有用的點,即使它不是這個問題的答案。 – 2012-09-06 02:23:10

1

使用xcrun --sysroot不適合我使用10.8。 尋找叮噹文件,我發現-isysroot是在這種情況下使用的選項。

使用:

xcrun gcc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk 

或:

xcrun clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk 

對我來說是非常有用創建下列別名:

alias xcrungcc='xcrun gcc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk' 
alias xcrunclang='xcrun clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk'