LLVM 基础学习:LLVM 的编译、安装和基本使用
LLVM的编译安装和基本使用
时间:20220608,版本:V0.1
作者:robotech_erx
1.LLVM的组成
The LLVM Core libraries :LLVM核心库
Clang :多重含义,编译器前端,或者是llvm的同义词;
LLDB:调试器;
libc++:C++的高性能实现,支持C++11和C++14;
OpenMP:多核并行程序设计方案。;
Polly:cache-locality optimizations as well as auto-parallelism。
Libclc:The libclc project aims to implement the OpenCL standard library.
Klee:符号执行引擎,以后单独介绍。
还有其他很多。花点时间翻翻,能找到不要惊喜。
2.Apt 安装
On Ubuntu focal(20.04), you can install modern LLVM from the official repository:
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-13 main"
sudo apt-get update
sudo apt-get install -y llvm-13 llvm-13-dev libllvm13 llvm-13-tools clang-13 libclang-common-13-dev libclang-13-dev libmlir-13 libmlir-13-dev
This will install all the required header files, libraries and tools in /usr/lib/llvm-13/.
各个版本的服务器地址等参考:https://apt.llvm.org/
3.下载安装
到官网下载预先编译的文件,设置一下环境变量。
地址:https://releases.llvm.org/download.html
4.编译安装
(1)需要的工具
$ sudo apt-get install build-essential(安装Gcc g++ 这些,要求GCC >=7.1.0,也可以apt安装一个clang来编译llvm)
$ sudo apt install libncurses5 (这个也是需要的,llvm-config等用的到)
$ sudo apt install cmake #3.16.3,需要CMake >=3.13.4
$ sudo apt install ninja-build(是官网推荐的工具,取代make。Building with ninja significantly improves your build time, especially with incremental builds, and improves your memory usage。)
其他可选工具:
python >=3.6 Automated test suite
可选,LLVM自带的测试用例的执行需要python
zlib >=1.2.3.4 Compression library,可选
(2)Clone 代码
Checkout LLVM (including related subprojects like Clang):
$ git clone https://github.com/llvm/llvm-project.git
The above command is very slow. It can be made faster by creating a shallow clone. Shallow clone saves storage and speeds up the checkout time. This is done by using the command:
$ git clone --depth=1 https://github.com/llvm/llvm-project.git (using this only the latest version of llvm can be built)
For normal users looking to just compile, this command works fine. But if someone later becomes a contributor, since they can't push code from a shallow clone, it needs to be converted into a full clone:
$ cd llvm-project
$ git fetch --unshallow
If you want to get a specific release (as opposed to the most recent revision), you can check out a tag after cloning the repository. E.g., git checkout llvmorg-6.0.1 inside the llvm-project directory created by the above command. Use git tag -l to list all of them.需要完整克隆。
(3)Build
Build LLVM and Clang:
$ cd llvm-project
$ mkdir build (in-tree build is not supported)
$ cd build
$ cmake -DLLVM_ENABLE_PROJECTS=clang -G "Unix Makefiles" ../llvm
$ make # or cmake --build .
编译完成后,The object files and the binaries are placed in the ~/llvm/build directory.The binaries for LLVM tools are placed in ~/llvm/build/bin.
说明一下cmake的参数们:
../llvm
../llvm是源码目录下的子目录,包含CMakeList.txt文件。
-DCMAKE_BUILD_TYPE=type
Controls optimization level and debug information of the build. For more detailed information see CMAKE_BUILD_TYPE (https://llvm.org/docs/CMake.html)。Type的类型有:Release,Debug,RelWithDebInfo,MinSizeRel。Debug一般是为了开发,普通使用Release就行了。如果是Debug,请准备足够的内存和硬盘空间。
注意,不要轻易选择编译Debug版本,如果是选择Debug,由于llvm确实太大了,RAM和硬盘的消耗都十分恐怖,我在一个60G的虚拟机里编译,10分钟不到,硬盘就占满了。然后报告编译失败。(报告这个错误 collect2: fatal error: ld terminated with signal 9 [Killed])。如果要编译Debug版本,内存和硬盘空间务必留足了(32G ram,150G磁盘)。
-DLLVM_TARGETS_TO_BUILD
Set this equal to the target you wish to build. You may wish to set this to X86; however, you will find a full list of targets within the llvm-project/llvm/lib/Target directory.
-DLLVM_TARGETS_TO_BUILD=X86. (注意是大写,x86会报错)
-DLLVM_TARGETS_TO_BUILD=host. 这也是可以的,编译本机的平台。
-DLLVM_ENABLE_PROJECTS
semicolon-separated list of the LLVM subprojects you’d like to additionally build.Can include any of: clang, clang-tools-extra, lldb, compiler-rt, lld, polly, or cross-project-tests.
-DLLVM_ENABLE_RUNTIMES
Set this equal to the runtimes you wish to compile (e.g. libcxx, libcxxabi, etc.) If compiling more than one runtime, separate the items with a semicolon. Should you run into issues with the semicolon, try surrounding it with single quotes.
For example, to build LLVM, Clang, libcxx, and libcxxabi, use -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi".
-DCMAKE_INSTALL_PREFIX=directory
Specify for directory the full pathname of where you want the LLVM tools and libraries to be installed (default /usr/local).执行make install之类的安装才会执行复制。显然我们不会这么干的~。
注意路径里must not contain ~ (tilde) to refer to your home directory as it won't be expanded. Use $HOME or an absolute path instead.
除了直接调用make编译,开可以使用CMake调用相应工具:
$ cmake --build . #--build option is a portable why to tell cmake to invoke the underlying build tool (make, ninja, xcodebuild, msbuild, etc.)
$ cmake --build . --target install # build并安装
添加环境变量
add the following line to the ~/.bashrc file at the end of the file.
export PATH=$PATH:~/llvm/build/bin/
$ source ~/.bashrc
Restart the terminal for the change to take effect.
(4)使用ninja来编译
使用ninja能有效降低内存和硬盘的占用,特别是增量编译的速度很快。过程和使用make一样基本相同,只是生成工程文件的时候要使用 -G参数指名是Ninja,最后启动编译的时候使用ninja命令:
$ cd llvm-project
$ mkdir build
$ cd build
$ cmake -G Ninja -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=host ../llvm
$ ninja
$ ninja clang #This will build just clang.
$ ninja check-clang #This will run the clang tests.
(5)生成其他IDE (例如eclipse )的工程文件
CMake可以生成多种IDE的工程文件,甚至能够同时生成不同工具的。那我们可以同时生成Eclipse 和Ninja的,使用Ninja编译代码,使用IDE来浏览查看代码。
cd ~/llvm/
mkdir build
cd build
cmake -G “Eclipse CDT4 - Ninja” -LLVM_TARGETS_TO_BUILD=host ../llvm/
ninja
Since we asked CMake to create the Eclipse with Ninja build system, along with the ninja build files it also generated Eclipse project files that we can use to open the codebase in Eclipse. To import the LLVM project in Eclipse, do the following:
- Open Eclipse
- File -> Import -> General -> Existing Projects into Workspace
- Choose ~/llvm/buildas the root directory.
(6)Cmake 支持的 generators
(cmake -help就能看到)
The following generators are available on this platform (* marks default):
* Unix Makefiles = Generates standard UNIX makefiles.
Green Hills MULTI = Generates Green Hills MULTI files (experimental, work-in-progress).
Ninja = Generates build.ninja files.
Watcom WMake = Generates Watcom WMake makefiles.
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
CodeLite - Ninja = Generates CodeLite project files.
CodeLite - Unix Makefiles = Generates CodeLite project files.
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 - Unix Makefiles = Generates Sublime Text 2 project files.
Kate - Ninja = Generates Kate project files.
Kate - Unix Makefiles = Generates Kate project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
如果是VScode,安装常用的C++插件后,感觉已经很方便了。
(7)关于交叉编译
https://llvm.org/docs/GettingStarted.html#cross-compiling-llvm
https://llvm.org/docs/HowToCrossCompileLLVM.html
https://clang.llvm.org/docs/CrossCompilation.html
5.简单使用
https://llvm.org/docs/GettingStarted.html#an-example-using-the-llvm-tool-chain
6.参考
https://llvm.org/docs/tutorial/
https://llvm.org/docs/tutorial/index.html
https://llvm.org/docs/CommandGuide/index.html
https://llvm.org/docs/CMake.html
https://www.cs.utexas.edu/~pingali/CS380C/2019/assignments/llvm-guide.html
系列llvm教程
Series: Creating the Bolt Compiler
https://mukulrathi.com/create-your-own-programming-language/intro-to-compiler/
https://tomassetti.me/a-tutorial-on-how-to-write-a-compiler-using-llvm/
https://llvm-tutorial-cn.readthedocs.io/en/latest/chapter-1.html 官方教程的中文版
原文地址:https://www.cnblogs.com/robotech/p/16370415.html
上一篇: 逆向汤头条
下一篇: SDL 系列说明 (II) 环境设置
推荐阅读
-
0 基础即可了解 Visual Studio Code (VScode) 使用脚本配置一键安装 C/C++ 环境、编译和运行 Windows 版本的教程(附脚本、安装包下载链接)
-
[姿势估计] 实践记录:使用 Dlib 和 mediapipe 进行人脸姿势估计 - 本文重点介绍方法 2):方法 1:基于深度学习的方法:。 基于深度学习的方法:基于深度学习的方法利用深度学习模型,如卷积神经网络(CNN)或递归神经网络(RNN),直接从人脸图像中学习姿势估计。这些方法能够学习更复杂的特征表征,并在大规模数据集上取得优异的性能。方法二:基于二维校准信息估计三维姿态信息(计算机视觉 PnP 问题)。 特征点定位:人脸姿态估计的第一步是通过特征点定位来检测和定位人脸的关键点,如眼睛、鼻子和嘴巴。这些关键点提供了人脸的局部结构信息,可用于后续的姿势估计。 旋转表示:常见的旋转表示方法包括欧拉角和旋转矩阵。欧拉角通过三个旋转角度(通常是俯仰、偏航和滚动)描述头部的旋转姿态。旋转矩阵是一个 3x3 矩阵,表示头部从一个坐标系到另一个坐标系的变换。 三维模型重建:根据特征点的定位结果,三维人脸模型可用于姿势估计。通过将人脸的二维图像映射到三维模型上,可以估算出人脸的旋转和平移信息。这就需要建立人脸的三维模型,然后通过优化方法将模型与特征点对齐,从而获得姿势估计结果。 特征点定位 特征点定位是用于检测人脸关键部位的五官基础部分,还有其他更多的特征点表示方法,大家可以参考我上一篇文章中介绍的特征点检测方案实践:人脸校正二次定位操作来解决人脸校正的问题,客户在检测关键点的代码上略有修改,坐标转换部分客户见上图 def get_face_info(image). img_copy = image.copy image.flags.writeable = False image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = face_detection.process(image) # 在图像上绘制人脸检测注释。 image.flags.writeable = True image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) box_info, facial = None, None if results.detections: for detection in results. for detection in results.detections: mp_drawing.Drawing.detection = 无 mp_drawing.draw_detection(image, detection) 面部 = detection.location_data.relative_keypoints 返回面部 在上述代码中,返回的数据是五官(6 个关键点的坐标),这是用 mediapipe 库实现的,下面我们可以尝试用另一个库:dlib 来实现。 使用 dlib 使用 Dlib 库在 Python 中实现人脸关键点检测的步骤如下: 确保已安装 Dlib 库,可使用以下命令: pip install dlib 导入必要的库: 加载 Dlib 的人脸检测器和关键点检测器模型: 读取图像并将其灰度化: 使用人脸检测器检测图像中的人脸: 对检测到的人脸进行遍历,并使用关键点检测器检测人脸关键点: 显示绘制了关键点的图像: 以下代码将参数 landmarks_part 添加到要返回的关键点坐标中。
-
LLVM 基础学习:LLVM 的编译、安装和基本使用
-
学习JMeter的基本安装和使用指南
-
F#探险之旅(二):函数式编程(上)-函数式编程范式简介 F#主要支持三种编程范式:函数式编程(Functional Programming,FP)、命令式编程(Imperative Programming)和面向对象(Object-Oriented,OO)的编程。回顾它们的历史,FP是最早的一种范式,第一种FP语言是IPL,产生于1955年,大约在Fortran一年之前。第二种FP语言是Lisp,产生于1958,早于Cobol一年。Fortan和Cobol都是命令式编程语言,它们在科学和商业领域的迅速成功使得命令式编程在30多年的时间里独领风骚。而产生于1970年代的面向对象编程则不断成熟,至今已是最流行的编程范式。有道是“*代有语言出,各领风骚数十年”。 尽管强大的FP语言(SML,Ocaml,Haskell及Clean等)和类FP语言(APL和Lisp是现实世界中最成功的两个)在1950年代就不断发展,FP仍停留在学院派的“象牙塔”里;而命令式编程和面向对象编程则分别凭着在商业领域和企业级应用的需要占据领先。今天,FP的潜力终被认识——它是用来解决更复杂的问题的(当然更简单的问题也不在话下)。 纯粹的FP将程序看作是接受参数并返回值的函数的集合,它不允许有副作用(side effect,即改变了状态),使用递归而不是循环进行迭代。FP中的函数很像数学中的函数,它们都不改变程序的状态。举个简单的例子,一旦将一个值赋给一个标识符,它就不会改变了,函数不改变参数的值,返回值是全新的值。 FP的数学基础使得它很是优雅,FP的程序看起来往往简洁、漂亮。但它无状态和递归的天性使得它在处理很多通用的编程任务时没有其它的编程范式来得方便。但对F#来说这不是问题,它的优势之一就是融合了多种编程范式,允许开发人员按照需要采用最好的范式。 关于FP的更多内容建议阅读一下这篇文章:Why Functional Programming Matters(中文版)。F#中的函数式编程 从现在开始,我将对F#中FP相关的主要语言结构逐一进行介绍。标识符(Identifier) 在F#中,我们通过标识符给值(value)取名字,这样就可以在后面的程序中引用它。通过关键字let定义标识符,如: let x = 42 这看起来像命令式编程语言中的赋值语句,两者有着关键的不同。在纯粹的FP中,一旦值赋给了标识符就不能改变了,这也是把它称为标识符而非变量(variable)的原因。另外,在某些条件下,我们可以重定义标识符;在F#的命令式编程范式下,在某些条件下标识符的值是可以修改的。 标识符也可用于引用函数,在F#中函数本质上也是值。也就是说,F#中没有真正的函数名和参数名的概念,它们都是标识符。定义函数的方式与定义值是类似的,只是会有额外的标识符表示参数: let add x y = x + y 这里共有三个标识符,add表示函数名,x和y表示它的参数。关键字和保留字关键字是指语言中一些标记,它们被编译器保留作特殊之用。在F#中,不能用作标识符或类型的名称(后面会讨论“定义类型”)。它们是: abstract and as asr assert begin class default delegate do donedowncast downto elif else end exception extern false finally forfun function if in inherit inline interface internal land lazy letlor lsr lxor match member mod module mutable namespace new nullof open or override private public rec return sig static structthen to true try type upcast use val void when while with yield 保留字是指当前还不是关键字,但被F#保留做将来之用。可以用它们来定义标识符或类型名称,但编译器会报告一个警告。如果你在意程序与未来版本编译器的兼容性,最好不要使用。它们是: atomic break checked component const constraint constructor continue eager event external fixed functor global include method mixinobject parallel process protected pure sealed trait virtual volatile 文字值(Literals) 文字值表示常数值,在构建计算代码块时很有用,F#提供了丰富的文字值集。与C#类似,这些文字值包括了常见的字符串、字符、布尔值、整型数、浮点数等,在此不再赘述,详细信息请查看F#手册。 与C#一样,F#中的字符串常量表示也有两种方式。一是常规字符串(regular string),其中可包含转义字符;二是逐字字符串(verbatim string),其中的(")被看作是常规的字符,而两个双引号作为双引号的转义表示。下面这个简单的例子演示了常见的文字常量表示: let message = "Hello World"r"n!" // 常规字符串let dir = @"C:"FS"FP" // 逐字字符串let bytes = "bytes"B // byte 数组let xA = 0xFFy // sbyte, 16进制表示let xB = 0o777un // unsigned native-sized integer,8进制表示let print x = printfn "%A" xlet main = print message; print dir; print bytes; print xA; print xB; main Printf函数通过F#的反射机制和.NET的ToString方法来解析“%A”模式,适用于任何类型的值,也可以通过F#中的print_any和print_to_string函数来完成类似的功能。值和函数(Values and Functions) 在F#中函数也是值,F#处理它们的语法也是类似的。 let n = 10let add a b = a + blet addFour = add 4let result = addFour n printfn "result = %i" result 可以看到定义值n和函数add的语法很类似,只不过add还有两个参数。对于add来说a + b的值自动作为其返回值,也就是说在F#中我们不需要显式地为函数定义返回值。对于函数addFour来说,它定义在add的基础上,它只向add传递了一个参数,这样对于不同的参数addFour将返回不同的值。考虑数学中的函数概念,F(x, y) = x + y,G(y) = F(4, y),实际上G(y) = 4 + y,G也是一个函数,它接收一个参数,这个地方是不是很类似?这种只向函数传递部分参数的特性称为函数的柯里化(curried function)。 当然对某些函数来说,传递部分参数是无意义的,此时需要强制提供所有参数,可是将参数括起来,将它们转换为元组(tuple)。下面的例子将不能编译通过: let sub(a, b) = a - blet subFour = sub 4 必须为sub提供两个参数,如sub(4, 5),这样就很像C#中的方法调用了。 对于这两种方式来说,前者具有更高的灵活性,一般可优先考虑。 如果函数的计算过程中需要定义一些中间值,我们应当将这些行进行缩进: let halfWay a b = let dif = b - a let mid = dif / 2 mid + a 需要注意的是,缩进时要用空格而不是Tab,如果你不想每次都按几次空格键,可以在VS中设置,将Tab字符自动转换为空格;虽然缩进的字符数没有限制,但一般建议用4个空格。而且此时一定要用在文件开头添加#light指令。作用域(Scope)作用域是编程语言中的一个重要的概念,它表示在何处可以访问(使用)一个标识符或类型。所有标识符,不管是函数还是值,其作用域都从其声明处开始,结束自其所处的代码块。对于一个处于最顶层的标识符而言,一旦为其赋值,它的值就不能修改或重定义了。标识符在定义之后才能使用,这意味着在定义过程中不能使用自身的值。 let defineMessage = let message = "Help me" print_endline message // error 对于在函数内部定义的标识符,一般而言,它们的作用域会到函数的结束处。 但可使用let关键字重定义它们,有时这会很有用,对于某些函数来说,计算过程涉及多个中间值,因为值是不可修改的,所以我们就需要定义多个标识符,这就要求我们去维护这些标识符的名称,其实是没必要的,这时可以使用重定义标识符。但这并不同于可以修改标识符的值。你甚至可以修改标识符的类型,但F#仍能确保类型安全。所谓类型安全,其基本意义是F#会避免对值的错误操作,比如我们不能像对待字符串那样对待整数。这个跟C#也是类似的。 let changeType = let x = 1 let x = "change me" let x = x + 1 print_string x 在本例的函数中,第一行和第二行都没问题,第三行就有问题了,在重定义x的时候,赋给它的值是x + 1,而x是字符串,与1相加在F#中是非法的。 另外,如果在嵌套函数中重定义标识符就更有趣了。 let printMessages = let message = "fun value" printfn "%s" message; let innerFun = let message = "inner fun value" printfn "%s" message innerFun printfn "%s" message printMessages 打印结果: fun value inner fun valuefun value 最后一次不是inner fun value,因为在innerFun仅仅将值重新绑定而不是赋值,其有效范围仅仅在innerFun内部。递归(Recursion)递归是编程中的一个极为重要的概念,它表示函数通过自身进行定义,亦即在定义处调用自身。在FP中常用于表达命令式编程的循环。很多人认为使用递归表示的算法要比循环更易理解。 使用rec关键字进行递归函数的定义。看下面的计算阶乘的函数: let rec factorial x = match x with | x when x < 0 -> failwith "value must be greater than or equal to 0" | 0 -> 1 | x -> x * factorial(x - 1) 这里使用了模式匹配(F#的一个很棒的特性),其C#版本为: public static long Factorial(int n) { if (n < 0) { throw new ArgumentOutOfRangeException("value must be greater than or equal to 0"); } if (n == 0) { return 1; } return n * Factorial (n - 1); } 递归在解决阶乘、Fibonacci数列这样的问题时尤为适合。但使用的时候要当心,可能会写出不能终止的递归。匿名函数(Anonymous Function) 定义函数的时候F#提供了第二种方式:使用关键字fun。有时我们没必要给函数起名,这种函数就是所谓的匿名函数,有时称为lambda函数,这也是C#3.0的一个新特性。比如有的函数仅仅作为一个参数传给另一个函数,通常就不需要起名。在后面的“列表”一节中你会看到这样的例子。除了fun,我们还可以使用function关键字定义匿名函数,它们的区别在于后者可以使用模式匹配(本文后面将做介绍)特性。看下面的例子: let x = (fun x y -> x + y) 1 2let x1 = (function x -> function y -> x + y) 1 2let x2 = (function (x, y) -> x + y) (1, 2) 我们可优先考虑fun,因为它更为紧凑,在F#类库中你能看到很多这样的例子。 注意:本文中的代码均在F# 1.9.4.17版本下编写,在F# CTP 1.9.6.0版本下可能不能通过编译。 F#系列随笔索引页面
-
[图文教程] 编译和安装OpenCV在Windows平台上,并学习如何使用gocv实现OpenCV的Go语言实现(实践验证成功)
-
学习笔记:PyAV简介、安装和基础操作,以及使用Python获取RTSP(海康)的rtp、dts和pts时间戳 - 适用于Windows或Linux操作系统的安装指南