Clang+mingw注意事項

Clang:fatal error:'stdio.h' file not found的解決方法

轉載 2015-03-06 09:41:46
標籤:clang編譯器

    在我下載安裝了 LLVM-3.6.0-win32.exe之後,我將LLVM的bin目錄加入到了系統PATH中,然而在Windows CommandLine上執行命令編譯一個簡單的hello.c程序時,出現瞭如下錯誤:


錯誤信息

   第一次安裝使用Clang,多虧神奇的google,我在這裏找到了解決方案:

   http://lists.cs.uiuc.edu/pipermail/cfe-dev/2013-November/033724.html  

   Clang doesn't shipwith its own complete toolchain.  It alwaysintegrates with whatever standard C/C++ libs and headers the nativeplatform uses.

   On Windows, there are twotoolchains worth mentioning: the Visual C++ toolchain, and theMingw toolchain.

     If youwant to build using make, etc, then you probably want to installmingw.Then you can build with something like "clang--target=i686-pc-mingw32" or "clang--target=x86_64-pc-mingw32"and it should pick the rightheaders for you.

   就是說Clang自身沒有配備自己完整的工具鏈,它通常使用本地平臺使用的任意的標準的C/C++庫和頭文件。在Windows平臺上,VisualC++和Mingw是兩個比較常用的工具鏈。那麼解決方案就來了:

  第一種方法,使用MinGW的頭文件

  安裝mingw32,將它的bin目錄加入到系統PATH中,我的MingGW的bin目錄爲:D:\MinGW\bin;然後在使用Clang編譯時,指定--target=i686-pc-mingw32就可以進行正常編譯了。

使用MinGW

  第二種方法,使用MSVC

   我的電腦上安裝了MicrosoftVisual Studio2013,那麼我就可以在Clang編譯時,指定--target=i686-pc-vs2013就可以正常編譯了。

使用VisualStudio 2013

--------------------------------------------------------------------------------------------------------------

windows下clang的安裝與使用

我本意是想在windows下學習下C++11,而結果是我的Visual Studio 2012不完全支持,而我又懶得去安裝2013/2015,太大了。公司運維也不允許我去下載- -,然後就想能不能在windows環境下搞個gcc玩,然後我又知乎了一把,大意的意見是clang把gcc甩了好遠,所以我就決定安裝clang環境來學習一下,過程中還是遇了幾個坑…

 

-----------------------------------------------------------------------------------------------

下載最新的clang版本,地址:http://www.llvm.org/releases/download.html#3.7.0

然後編寫測試用的c代碼,保存爲demo1.c

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("Hello World!");    

    return 0;
}

使用Win + R,切換到demo1.c的目錄下,然後執行clang --verbose demo1.c會遇到錯誤

找不到stdio.h文件,之後我在網上搜索了好久,比如這一篇文章

http://zanedp.blogspot.com/2014/01/clang-cant-find-stdioh-etc.html

我按照文章提示的步驟進行安裝,最後發現遇到這樣的錯誤:

ld.exe: unrecognised emulation mode: i386pep

Supported emulations: i386pe

 

這個問題很頭疼,google出來的結果很多,卻幾乎沒什麼頭緒,最後我在一個郵件列表中找到了答案

http://comments.gmane.org/gmane.comp.lib.boost.devel/262947

缺少stdio.h,下載mingw沒有問題,問題是我使用的不是64位的!

然後我搜索關鍵字“mingw 64”,總算讓我找到了答案,下載地址:http://mingw-w64.org/doku.php/download

注意CPU架構選擇x86_64,原因就是clang也使用的是該架構編譯的

安裝成功後,查看gcc的相關信息(需要把gcc安裝目錄的bin加入到環境變量)

 

如果還編譯不通過(我遇到了),關閉當前的dos窗口,然後重新來一遍就可以了

main.cpp的源碼:

#include <iostream>
#include <vector>
 
int main()
{
  std::vector<int> vect {1, 2, 3, 4, 5};
  for(auto& el : vect)
    std::cout << " - " << el << std::endl;
  
  return 0;
}

剩下的就請同學們自己愉快的玩耍吧…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章