1. 程式人生 > >Clang+llvm windows執行環境配置

Clang+llvm windows執行環境配置

下了官網Pre-built Binaries: llvm.org/releases/3.5.0/LLVM-3.5.0-win32.exe 03 Sep 2014 3.5.0

由於剛剛安裝了 TDM GCC 4.9.2 tdm64-gcc-4.9.2-3.exe 2014 December 12th(tdm-gcc.tdragon.net)

mklinkC:\mingw指向tdm64-gcc-4.9.2-3安裝目錄。

/* hello.c */
#include <stdio.h>

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

>clang hello.c

hello.c:2:10:fatal error: 'stdio.h' file not found

#include <stdio.h>

         ^

1 error generated.

難道又要下載 LLVM + Clang 原始碼,修改 llvm-3.5.0.src\tools\clang\lib\Frontend\下的 InitHeaderSearch.cpp,新增tdm64-gcc-4.9.2-3 C/C++ include search path標頭檔案搜尋路徑,重新編譯一次(具體參看舊文)。看了3.5.0原始碼,官網Pre-builtBinaries

支援到MiniGW 4.8.2難道每次使用新版 MinGW,都要重複這惱人的編譯。

現在,告訴你,只要新增幾個環境變數即可。

PATH新增 編譯器和LLVM+Clang Command Line Path 命令列執行路徑。

至於C/C++ include search path 標頭檔案搜尋路徑:

使用 MinGW 的,要設定:

C_INCLUDE_PATH

CPLUS_INCLUDE_PATH

使用 Visual C++ 的,要設定:

INCLUDE

當然,如果有附加的連結庫

MinGW 設定LIBRARY_PATH

VisualC++ 設定LIB

下面以使用 tdm64-gcc-4.9.2 為例:

Run-Clang.bat

@remset run llvm+clang with mingw path env
@echooff
setMINGW_HOME=c:\mingw
setMINGW_VERSION=4.9.2
set"PATH=%MINGW_HOME%\bin;%PATH%"
set"C_INCLUDE_PATH=%MINGW_HOME%\include;%MINGW_HOME%\x86_64-w64-mingw32\include"
set"CPLUS_INCLUDE_PATH=%C_INCLUDE_PATH%;%MINGW_HOME%\lib\gcc\x86_64-w64-mingw32\%MINGW_VERSION%\include;%MINGW_HOME%\lib\gcc\x86_64-w64-mingw32\%MINGW_VERSION%\include\c++;%MINGW_HOME%\lib\gcc\x86_64-w64-mingw32\%MINGW_VERSION%\include\c++\x86_64-w64-mingw32;%MINGW_HOME%\lib\gcc\x86_64-w64-mingw32\%MINGW_VERSION%\include\c++\backward"
setLLVM_HOME=c:\llvm
set"PATH=%LLVM_HOME%\bin;%PATH%"
%comspec%

轉載請註明引用地址 :http://blog.csdn.net/gocad/article/details/42297829 謝謝

如果使用 tdm32-gcc-4.9.2 則

C_INCLUDE_PATH=%MINGW_HOME%\include
CPLUS_INCLUDE_PATH=%MINGW_HOME%\include;%MINGW_HOME%\lib\gcc\mingw32\%MINGW_VERSION%\include;%MINGW_HOME%\lib\gcc\mingw32\%MINGW_VERSION%\include\c++;%MINGW_HOME%\lib\gcc\mingw32\%MINGW_VERSION%\include\c++\mingw32;%MINGW_HOME%\lib\gcc\mingw32\%MINGW_VERSION%\include\c++\backward
使用其它版本 MinGW 的,請根據各版本實際路徑設定。當然也可以自己編輯成像vcvars32.bat帶引數呼叫。

VisualC++ 使用者呼叫現成的Visual Studio Native Tools Command Prompt,或自己呼叫 vcvars32.bat 設定 Visual C++ 命令列編譯環境。 或Installing MSVC integration

下面是有些需要注意的地方

關於 m32/-m64, clang, clang++,clang-cl

預設 ( Target: i686-pc-windows-gnu/i686-pc-windows-msvc,Thread model: posix )

m32( Target:i386-pc-windows-gnu/i386-pc-windows-msvc,Thread model: posix )

m64(Target: x86_64-pc-windows-gnu/x86_64-pc-windows-msvc,Thread model: posix )

這與tdm64-gcc-4.9.2預設-m64不同所以要編譯 x64 執行程式碼請顯式呼叫-m64

還有 tdm64-gcc-4.9.2 預設 m64支援 __int128 is supported on thishost. ( 見c++config.h )

如果同時使用 STL algorithm 與 m32,請將 CPLUS_INCLUDE_PATH 中的%MINGW_HOME%\lib\gcc\x86_64-w64-mingw32\%MINGW_VERSION%\include\c++\x86_64-w64-mingw32更改為%MINGW_HOME%\lib\gcc\x86_64-w64-mingw32\%MINGW_VERSION%\include\c++\x86_64-w64-mingw32\32以免編譯錯誤

如果要用Clang+llvm+MinGW編譯 32位執行程式碼,最好還是使用 MinGW(32),避免因使用 MinGW-w64 設定不恰當,而生成的編譯錯誤。

Clang具體呼叫模式與使用的命令引數,請用v命令選項進行追蹤

-v    Show commands to run and use verbose output

例如:

D:\>clang++ -v hello.c
clang version 3.5.0 (217039)
Target: i686-pc-windows-gnu
Thread model: posix
clang++.exe: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
 "c:\llvm\bin\clang++.exe" -cc1 -triple i686-pc-windows-gnu -emit-obj -mrelax-al
l -disable-free -main-file-name hello.c -mrelocation-model static -mdisable-fp-e
lim -fmath-errno -masm-verbose -mconstructor-aliases -target-cpu pentium4 -v -dw
arf-column-info -resource-dir "c:\\llvm\\bin\\..\\lib\\clang\\3.5.0" -c-isystem
"c:\\mingw\\include" -c-isystem "c:\\mingw\\x86_64-w64-mingw32\\include" -cxx-is
ystem "c:\\mingw\\include" -cxx-isystem "c:\\mingw\\x86_64-w64-mingw32\\include"
 -cxx-isystem "c:\\mingw\\lib\\gcc\\x86_64-w64-mingw32\\4.9.2\\include" -cxx-isy
stem "c:\\mingw\\lib\\gcc\\x86_64-w64-mingw32\\4.9.2\\include\\c++" -cxx-isystem
 "c:\\mingw\\lib\\gcc\\x86_64-w64-mingw32\\4.9.2\\include\\c++\\x86_64-w64-mingw
32" -cxx-isystem "c:\\mingw\\lib\\gcc\\x86_64-w64-mingw32\\4.9.2\\include\\c++\\
backward" -fdeprecated-macro -fdebug-compilation-dir "D:\\" -ferror-limit 19 -fm
essage-length 80 -mstackrealign -fno-use-cxa-atexit -fobjc-runtime=gcc -fcxx-exc
eptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o "C:\\Temp\
\hello-0a5974.o" -x c++ hello.c
clang -cc1 version 3.5.0 based upon LLVM 3.5.0 default target i686-pc-windows-gnu
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.0"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.0/x86_64-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.0/i686-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.0/backward"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.1"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.1/x86_64-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.1/i686-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.1/backward"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.2"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.2/x86_64-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.2/i686-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.2/backward"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.3"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.3/x86_64-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.3/i686-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.7.3/backward"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.0"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.0/x86_64-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.0/i686-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.0/backward"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.1"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.1/x86_64-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.1/i686-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.1/backward"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.2"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.2/x86_64-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.2/i686-w64-mingw32"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0/../../../include/
c++/4.8.2/backward"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.0/include/c++"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.0/include/c++/mingw32"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.0/include/c++/backward"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.1/include/c++"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.1/include/c++/mingw32"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.1/include/c++/backward"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.2/include/c++"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.2/include/c++/mingw32"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.2/include/c++/backward"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.3/include/c++"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.3/include/c++/mingw32"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.7.3/include/c++/backward"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.8.0/include/c++"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.8.0/include/c++/mingw32"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.8.0/include/c++/backward"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.8.1/include/c++"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.8.1/include/c++/mingw32"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.8.1/include/c++/backward"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.8.2/include/c++"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.8.2/include/c++/mingw32"
ignoring nonexistent directory "c:/MinGW/lib/gcc/mingw32/4.8.2/include/c++/backward"
ignoring nonexistent directory "/usr/include/c++/4.4"
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0\../../../i686-w64
-mingw32/include"
ignoring nonexistent directory "c:\llvm\bin\..\lib\clang\3.5.0\../../../x86_64-w
64-mingw32/include"
ignoring nonexistent directory "/mingw/include"
ignoring nonexistent directory "/usr/include"
ignoring duplicate directory "c:\mingw\include"
#include "..." search starts here:
#include <...> search starts here:
 c:\mingw\include
 c:\mingw\x86_64-w64-mingw32\include
 c:\mingw\lib\gcc\x86_64-w64-mingw32\4.9.2\include
 c:\mingw\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++
 c:\mingw\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\x86_64-w64-mingw32
 c:\mingw\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\backward
 c:\llvm\bin\..\lib\clang\3.5.0\include
 c:\llvm\bin\..\lib\clang\3.5.0\../../../include
End of search list.
 "c:\mingw\bin\g++.exe" -v -m32 -o a.out "C:\\Temp\\hello-0a5974.o"
Using built-in specs.
COLLECT_GCC=c:\mingw\bin\g++.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.9.2/lto-wra
pper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-4.9.2/configure --build=x86_64-w64-mingw32 --e
nable-targets=all --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable
-libgomp --enable-lto --enable-graphite --enable-cxx-flags=-DWINPTHREAD_STATIC -
-disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-libstdcxx-d
ebug --enable-threads=posix --enable-version-specific-runtime-libs --enable-full
y-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-l
d --disable-werror --disable-nls --disable-win32-registry --prefix=/mingw64tdm -
-with-local-prefix=/mingw64tdm --with-pkgversion=tdm64-1 --with-bugurl=http://td
m-gcc.tdragon.net/bugs
Thread model: posix
gcc version 4.9.2 (tdm64-1)
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.9.2/;c:/mingw/bin
/../libexec/gcc/;c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x8
6_64-w64-mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/32/;c:/mingw/bin/.
./lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/lib/../lib32/;
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/;c:/mingw/bin/../lib/gcc/;C:/Mi
nGW/lib/;D:/dev/msys64/usr/lib/;c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2
/../../../../x86_64-w64-mingw32/lib/;c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/
4.9.2/../../../
COLLECT_GCC_OPTIONS='-v' '-m32' '-o' 'a.out' '-mtune=generic' '-march=x86-64'
 c:/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.9.2/collect2.exe -plugin c:/mi
ngw/bin/../libexec/gcc/x86_64-w64-mingw32/4.9.2/liblto_plugin-0.dll -plugin-opt=
c:/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.9.2/lto-wrapper.exe -plugin-opt
=-fresolution=C:\Temp\cc83PMcQ.res -plugin-opt=-pass-through=-lmingw32 -plugin-o
pt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-th
rough=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lp
thread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32
-plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-
opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-th
rough=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-
lmsvcrt -m i386pe --exclude-libs=libpthread.a -Bdynamic -o a.out c:/mingw/bin/..
/lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/lib/../lib32/cr
t2.o c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-min
gw32/lib/../lib32/crtbegin.o -Lc:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/
32 -Lc:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-min
gw32/lib/../lib32 -Lc:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2 -Lc:/mingw/
bin/../lib/gcc -LC:/MinGW/lib -LD:/dev/msys64/usr/lib -Lc:/mingw/bin/../lib/gcc/
x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/lib -Lc:/mingw/bin/../li
b/gcc/x86_64-w64-mingw32/4.9.2/../../.. C:\Temp\hello-0a5974.o -Bstatic -lstdc++
 -Bdynamic -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -l
shell32 -luser32 -lkernel32 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt c:/min
gw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/lib/..
/lib32/crtend.o

D:\>

另外就是,關於 --driver-mode 引數:

clang++等效於 clang--driver-mode=g++

clang-cl等效於 clang--driver-mode=cl

--driver-mode可以覆蓋內建或預設值:clang-cl --driver-mode=g++ 等效於 clang++

目前,clang-cl 編譯 VC x64/amd64 STL algorithm 時,有時會編譯不通過,如:

error LNK2019: unresolved external
 symbol __cxa_call_unexpected referenced in function "public: __int64 __cdecl std::_Temp_iterator<long>::_Maxlen(void)" ([email protected][email protected]@[email protected]@QEAA_JXZ)
error LNK2001: unresolved external
 symbol __gxx_personality_v0

下面引自(c.tieba.baidu.com/p/3290080396

關於 LLVM Clang。目前 Clang 在 Windows 下都是基於 MingW 的庫,所以編譯/連結的時候需要把相應的路徑設定好。在我的綠色便攜版中都已經設定好了。
llvm-3.5.0.src\tools\clang\tools\driver\driver.cpp
Clang在Windows上呈現有三種實現,一種是使用VisualStudio,一種是Mingw,還有一種是Cygwin,clang用一個引數是驅動模式--driver-mode
driver.cpp
staticconst struct {
const char*Suffix;
const char*ModeFlag;
} suffixes[] = {
{"clang", nullptr },
{"clang++", "--driver-mode=g++" },
{"clang-c++", "--driver-mode=g++" },
{"clang-cc", nullptr },
{"clang-cpp", "--driver-mode=cpp" },
{"clang-g++", "--driver-mode=g++" },
{"clang-gcc", nullptr },
{"clang-cl", "--driver-mode=cl" },
{"cc", nullptr },
{"cpp", "--driver-mode=cpp" },
{"cl" , "--driver-mode=cl" },
{"++", "--driver-mode=g++" },
};

下面引自Clang Compiler User’s Manual(clang.llvm.org/docs/UsersManual.html)

X86

The support for X86 (both 32-bit and 64-bit) is considered stable onDarwin (Mac OS X), Linux, FreeBSD, and Dragonfly BSD: it has been tested tocorrectly compile many large C, C++, Objective-C, and Objective-C++ codebases.

Onx86_64-mingw32, passing i128(by value)is incompatible with the Microsoft x64 calling convention. You might need totweakWinX86_64ABIInfo::classify()in lib/CodeGen/TargetInfo.cpp.

For the X86 target, clang supports the -m16 command line argument which enables16-bit code output. This is broadly similar to usingasm(".code16gcc")with the GNU toolchain.The generated code and the ABI remains 32-bit but the assembler emitsinstructions appropriate for a CPU running in 16-bit mode, with address-sizeand operand-size prefixes to enable 32-bit addressing and operations.

Clang works on some mingw32 distributions. Clang assumes directories asbelow;

  • C:/mingw/include
  • C:/mingw/lib
  • C:/mingw/lib/gcc/mingw32/4.[3-5].0/include/c++

On MSYS, a few tests might fail.

For 32-bit (i686-w64-mingw32), and 64-bit (x86_64-w64-mingw32), Clangassumes as below;

  • GCC versions 4.5.0 to 4.5.3, 4.6.0 to 4.6.2, or 4.7.0 (for the C++ header search path)
  • some_directory/bin/gcc.exe
  • some_directory/bin/clang.exe
  • some_directory/bin/clang++.exe
  • some_directory/bin/../include/c++/GCC_version
  • some_directory/bin/../include/c++/GCC_version/x86_64-w64-mingw32
  • some_directory/bin/../include/c++/GCC_version/i686-w64-mingw32
  • some_directory/bin/../include/c++/GCC_version/backward
  • some_directory/bin/../x86_64-w64-mingw32/include
  • some_directory/bin/../i686-w64-mingw32/include
  • some_directory/bin/../include

This directory layout is standard for any toolchain you will find on theofficialMinGW-w64 website.

Clang expects the GCC executable “gcc.exe” compiled fori686-w64-mingw32(orx86_64-w64-mingw32) to be present on PATH.

clang-cl is an alternative command-line interface to Clang driver,designed for compatibility with the Visual C++ compiler, cl.exe.

To enable clang-cl to find system headers, libraries, and the linker whenrun from the command-line, it should be executed inside a Visual Studio NativeTools Command Prompt or a regular Command Prompt where the environment has beenset up using e.g.vcvars32.bat.

clang-cl can also be used from inside Visual Studio by using an LLVMPlatform Toolset.