LLVM每日談之二十二 llvm-config工具的使用

做者:史寧寧(snsn1984)html

llvm-config做爲LLVM的一個工具,是很是有用的,官方文檔(http://llvm.org/docs/CommandGuide/llvm-config.html)關於它的介紹以下:git

llvm-config makes it easier to build applications that use LLVM. It can print the compiler flags, linker flags and object libraries needed to link against LLVM.
github

這裏介紹的很是清楚,llvm-config使得使用LLVM去構建本身的應用更加的簡單。之因此能夠更加簡單,是由於它能夠打印出編譯器flags、鏈接器flags以及須要鏈接的LLVM庫。簡單點的說,就是llvm-config能夠獲取系統中LLVM的全部相關信息,這些信息能夠方便的用於構建基於LLVM的項目。只是這麼說的話,看起來並不明顯,下面我就給出一個實例來。shell

LLVM_CONFIG = llvm-config
LLVM_CXXFLAGS += $(shell $(LLVM_CONFIG) --cxxflags)
LLVM_LDFLAGS := $(shell $(LLVM_CONFIG) --ldflags)
LLVM_LIBS = $(shell $(LLVM_CONFIG) --libs bitwriter core support)

llvm_model_src = ModuleMaker.cpp


test_model:
	g++ $(llvm_model_src) $(LLVM_CXXFLAGS) $(LLVM_LIBS) $(LLVM_LDFLAGS) -lpthread -ldl -o ModuleMaker
這是一個Makefile。它是一個例子ModuleMaker的編譯文件。這個ModuleMaker例子自己是LLVM源碼中llvm/examples/ModuleMaker/目錄下的一個例子,它演示的若是憑空構建一個LLVM IR的Module。我這裏寫了這個Makefile之後,能夠在已經安裝LLVM的系統(Linux)上單獨的編譯這個例子,而不須要依賴LLVM的源碼,也再也不須要在LLVM源碼中編譯這個例子。完整的包含Makefile的ModuleMaker例子的代碼: https://github.com/shining1984/llvm-examples
從這個Makefile中能夠看出,編譯所須要的環境變量,包括LLVM_CXXFLAGS、LLVM_LDFLAGS和LLVM_LIBS都是直接經過llvm-config直接獲取的,這就徹底不須要用戶在編譯項目的時候設置環境變量或者傳遞變量,項目能夠直接獲取系統裏的環境變量,大大方便了項目的構建。只有真正的構建過基於LLVM項目的人,才明白使用了llvm-config以後會多方便。

同時,llvm-config還能夠以`llvm-config --libs`這樣的形式在Makefile中使用,或者在命令行中使用,這樣的使用形式是獲取這個命令在shell執行後所輸出的信息。這裏須要指出的是「`」這個符號,並非「‘」。這二者是有區別的,前者是和「~」同鍵的符號,後者是和「"」 同鍵的符號。這一點是必定要區別的,否者系統沒法識別。例如:app

g++ `llvm-config --cxxflags` -o HowToUseJIT.o -c HowToUseJIT.cpp
g++ `llvm-config --ldflags` -o HowToUseJIT HowToUseJIT.o \
    `llvm-config --libs engine bcreader scalaropts`

llvm-config的主要參數以下:ide

–version工具

Print the version number of LLVM.

-helpui

Print a summary of  llvm-config arguments.

–prefixthis

Print the installation prefix for LLVM.

–src-rootspa

Print the source root from which LLVM was built.

–obj-root

Print the object root used to build LLVM.

–bindir

Print the installation directory for LLVM binaries.

–includedir

Print the installation directory for LLVM headers.

–libdir

Print the installation directory for LLVM libraries.

–cxxflags

Print the C++ compiler flags needed to use LLVM headers.

–ldflags

Print the flags needed to link against LLVM libraries.

–libs

Print all the libraries needed to link against the specified LLVM  components, including any dependencies.

–libnames

Similar to  –libs, but prints the bare filenames of the libraries without  -l or pathnames. Useful for linking against a not-yet-installed copy of LLVM.

–libfiles

Similar to  –libs, but print the full path to each library file. This is useful when creating makefile dependencies, to ensure that a tool is relinked if any library it uses changes.

–components

Print all valid component names.

–targets-built

Print the component names for all targets supported by this copy of LLVM.

–build-mode

Print the build mode used when LLVM was built (e.g. Debug or Release)
參考文檔: http://llvm.org/docs/CommandGuide/llvm-config.html
相關文章
相關標籤/搜索