wordpress   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用LLVM在Windows上为ARM编译C程序?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

目标仅使用LLVM在 Windows上为ARM编译C程序. 为什么LLVM因为许可许可. 我开始怀疑我对LLVM的理解是否正确. 在主机上做 >使用clang(前端)生成中间表示.该表示是独立于目标的. >使用llc(后端)生成目标汇编代码. >使用lld-link.exe生成可执行文件. 然后在目标机器上执行. 主机Windows 10,64bit 目标机器用手臂皮质-57打开pX 该程序 in
目标仅使用LLVM在 Windows上为ARM编译C程序.

为什么LLVM因为许可许可.

我开始怀疑我对LLVM的理解是否正确.

在主机上做

>使用clang(前端)生成中间表示.该表示是独立于目标的.
>使用llc(后端)生成目标汇编@L_772_3@.
>使用lld-link.exe生成可执行文件.

然后在目标机器上执行.

主机Windows 10,64bit

目标机器用手臂皮质-57打开pX

该程序

int main(int argc,char* argv[]) 
{
    int x=41;
    x++;
    return x;
}

我已经检查并编译了LLVM(使用Visual studio 2015,发布版本,cpu = x64)

我的尝试

clang.exe -target arm -march=armv8-a -mcpu=cortex-a57 -mfloat-abi=hard  -emit-llvm -c -o main.bc  main.cpp
llc.exe -march=arm -mcpu=cortex-a57 -mattr=a57,armv8-a,v8 -meabi=gnu -o main.s main.bc
lld-link.exe /entry:main /machine:arm main.s

错误

lld-link.exe: error: main.s: unkNown file type

然后我尝试在Windows上执行前端步骤,在arm机器上执行后端操作.

clang.exe -target arm -march=armv8-a -mcpu=cortex-a57 -mfloat-abi=hard  -emit-llvm -c -o main.bc  main.cpp
llc.exe -march=arm -mcpu=cortex-a57 -mattr=a57,v8 -meabi=gnu -o main.s main.bc
SCP main.s to the arm machine. SSH and
gcc main.s (using gcc as a test. LLVM should do this.)

错误

@H_107_39@main.s: Assembler messages: main.s:2: Error: unkNown pseudo-op: `.Syntax' main.s:3: Error: unkNown pseudo-op: `.eabi_attribute' main.s:9: Error: unkNown pseudo-op: `.fpu' main.s:26: Error: junk at end of line,first unrecognized character is `@' main.s:29: Error: unkNown pseudo-op: `.code' main.s:31: Error: unkNown pseudo-op: `.fnstart' main.s:32: Error: junk at end of line,first unrecognized character is `@' main.s:34: Error: operand 1 should be an Integer register -- `mov r2,#0' main.s:41: Error: operand 1 should be an Integer or stack pointer register -- `add r0,r0,#1' main.s:45: Error: unkNown mnemonic `bx' -- `bx lr' main.s:48: Error: unkNown pseudo-op: `.cantunwind' main.s:49: Error: unkNown pseudo-op: `.fnend' main.s:50: Error: junk at end of line,first unrecognized character is `@'

所以我试图只针对Windows

clang.exe  -emit-llvm -c -o main.bc  main.cpp
llc.exe -march=x86 -c -o main.s main.bc
ld.lld.exe main.s

错误

ld.lld.exe: error: main.s:1: unkNown directive: .text

然后,使用gcc代替ld.lld.exe(再次使用gcc作为测试.LLVM应该这样做.)

clang.exe  -emit-llvm -c -o main.bc  main.cpp
llc.exe -march=x86 -c -o main.s main.bc
gcc main.s -o main.exe

这样可行.测试我的类型

@H_107_39@main.exe echo Exit Code is %errorlevel%

返回42

一般问题

在Windows下针对仅使用LLVM的arm cpu编译C程序有哪些步骤(没有gcc,没有从ARM下载)?

具体问题

>自编译LLVM附带的工具(例如clang.exe,llc.exe,lld.exE)是否可以在Windows目标臂上编译可执行文件?例如,还在开发中吗?
>为什么我尝试在Windows下编译和链接,以Windows为目标失败?
>在主机上为目标链接时,头文件和库(例如libstdC)来自何处?我想我需要从机器上拿出那些?将它们复制到主机并告诉链接器在哪里找到它们?那是对的吗?

更新

所以我最初尝试过Cross-compilation using Clang

clang.exe --target=arm --sysroot=c:\code\clang\FromCmdLine main.cpp  -v

结果是

clang.exe: error: linker (via gcC) command Failed with exit code 1 (use -v to see invocation)

而-v的细节是

"C:\\llvm\\clang.exe" -cc1 -triple armv4t-- -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mreLOCATIOn-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -target-cpu arm7tdmi -target-feature +soft-float -target-feature +soft-float-abi -target-feature -fp-only-sp -target-feature -d16 -target-feature -vfp2 -target-feature -vfp3 -target-feature -fp16 -target-feature -vfp4 -target-feature -fp-armv8 -target-feature -neon -target-feature -crypto -target-feature +Strict-align -target-abi aapcs -msoft-float -mfloat-abi soft -fallow-half-arguments-and-returns -dwarf-column-info -debugger-tuning=gdb -v -resource-dir "c:\\llvm\\clang\\7.0.0" -isysroot "c:\\code" -fdeprecated-macro -fdebug-compilation-dir "c:\\code" -ferror-limit 19 -fmessage-length 293 -fno-signed-char -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o "C:\\Users\\ApPDAta\\Local\\Temp\\main-b17d06.o" -x c++ main.cpp
clang -cc1 version 7.0.0 based upon LLVM 7.0.0svn default target x86_64-pc-win32
ignoring nonexistent directory "c:\code\usr/local/include"
ignoring nonexistent directory "c:\code\usr/include"
#include "..." search starts here:
#include <...> search starts here:
 C:\llvm\clang\7.0.0\include
End of search list.
 "C:\\MinGW\\bin\\gcc.exe" "--sysroot=c:\\code" -v -o a.out "C:\\Users\\ApPDAta\\Local\\Temp\\main-b17d06.o"
Using built-in specs.
COLLECT_GCC=C:\MinGW\bin\gcc.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,Obj-C++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/6.3.0/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../;c:/code/clang/FromCmdLine/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'a.out' '-mtune=generic' '-march=i586'
 c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/collect2.exe -plugin c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/liblto_plugin-0.dll -plugin-opt=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\ApPDAta\Local\Temp\ccufvVIA.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -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-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt --sysroot=c:\code\clang\FromCmdLine -Bdynamic -o a.out c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../crt2.o c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtbegin.o -Lc:/mingw/bin/../lib/gcc/mingw32/6.3.0 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../.. -Lc:/code/clang/FromCmdLine/lib C:\Users\ApPDAta\Local\Temp\main-b17d06.o -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lsHell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtend.o
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: C:\Users\ApPDAta\Local\Temp\main-b17d06.o: ReLOCATIOns in generic ELF (EM: 40)
C:\Users\ApPDAta\Local\Temp\main-b17d06.o: error adding symbols: File in wrong format

更新

这并没有完全回答我的问题,但它确实有助于我取得进展.

为了更好地理解,我发现crosstool-NG很有用,特别是他们的documentation(第1章到第5章).

然后我阅读了cmake cross compiling文档.

我写了一个小的cmake C测试.

Helloworld.cpp

#include <iostream>
int main(int argc,char *argv[])
{
   std::cout << "Hello World!" << std::endl;
   return 0;
}

的CMakeLists.txt

cmake_minimum_required(VERSION 2.8.9)
project (Hello)
add_executable(Hello Helloworld.cpp)

针对cmake定位特定配置.这是从4.

set(CMAKE_SYstem_NAME LinuX)
set(CMAKE_SYstem_PROCESSOR arm)

set(CMAKE_SYSROOT /home/user/x-tools/aarch64-unkNown-linux-gnueabi/aarch64-unkNown-linux-gnueabi/sysroot/)
set(CMAKE_STAGING_PREFIX /home/user/crosscompile/stagE)

set(tools /home/user/x-tools/aarch64-unkNown-linux-gnueabi)
set(CMAKE_C_COMPILER ${tools}/bin/aarch64-unkNown-linux-gnueabi-gcC)
set(CMAKE_CXX_COMPILER ${tools}/bin/aarch64-unkNown-linux-gnueabi-g++)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

和命令行

cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain_file.txt ..

该交叉编译为ARM,程序在ARM机器上运行.

但这不使用LLVM / Clang.要使用LLVM我想改变要使用的工具链配置

set(tools /usr/bin)
set(CMAKE_C_COMPILER ${tools}/clang)
set(CMAKE_CXX_COMPILER ${tools}/clang++)

失败的原因是该bin文件夹用于主机.

我也尝试过使用http://releases.llvm.org/download.html的AArch64下载.是的,也没用.

总而言之,这就是所需要的.

>包含目标系统的lib和include文件夹的sysroot文件夹.好吧,在那个sysroot文件夹中需要比lib和include更多.
>目标系统的工具链(编译器,汇编器,链接器).

解决方法

这是我必须@L_944_58@的,要获得概念验证工作,只使用llvm进行交叉编译,使用host = linux x86_64和target = DrivePX(arm aarch64). (也适用于host = Windows 10 x86_64.)

我推荐像croostool-ng这样的工具为你设置交叉编译工具链,但是下面的步骤显示了幕后发生的事情,它只使用了llvm.

>在主机上,签出并编译LLVM,包括Clang

>见http://llvm.org/docs/GettingStarted.html.
>这需要很长时间,因此发布版本也是如此.还要确保你有足够的空间(GB).
>告诉cmake进行发布版本,例如
> CC = gcc CXX = g cmake -G“Unix Makefiles”-DCMAKE_BUILD_TYPE = Release ../llvm/
>然后@L_904_63@make.

>在主机上拨打clang.

>请记住设置目标,例如–target = aarch64-Linux的GNU
>告诉clang使用llvm链接器,即-fuse-ld = lld
> clang –target = aarch64-linux-gnu -v main.cpp -o main -fuse-ld = lld

>会有很多链接错误

>还有一些关于丢失crtX.o文件错误,例如:

>在目标机器上找到所有libgcc,libc等文件.

>请记住复制完全版本的库,例如for libgcc_s.so copy libgcc_s.so.1
>将这些文件复制到主机.
>告诉clang在哪里找到这些库,例如:将文件复制到名为libs的文件夹中
> clang –target = aarch64-linux-gnu -v main.cpp -o main -fuse-ld = lld -l./libs
>还记得链接器正在寻找例如libgcc_s.so所以创建符号链接到例如libgcc_s.so.1.

>将crtX.o文件从目标复制到主机.把它们放在main.cpp旁边.
>然后又一个错误

>该符号位于libc_nonshared.a中.这answer有所帮助.

>在主机上找到并打开libc.so并查看libc.so.6和libc_nonshared.a所在的位置.
>将它们复制到主机.
>告诉链接器使用这两个库,即-lc -lc_nonshared
> clang –target = aarch64-linux-gnu -v main.cpp -o main -fuse-ld = lld -l./libs -lc -lc_nonshared

我的短文本@L_772_3@仅依赖于libc,libgcc并且不需要头文件.如果您的@L_772_3@需要其他库和头文件,则必须将它们从目标复制到主机.

更新
如果您对libgcc感到疑惑,请阅读this question.

大佬总结

以上是大佬教程为你收集整理的如何使用LLVM在Windows上为ARM编译C程序?全部内容,希望文章能够帮你解决如何使用LLVM在Windows上为ARM编译C程序?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。