> 文档中心 > clang-llvm跨平台编译

clang-llvm跨平台编译

本文以openharmony富设备为例,使用框架代码的现有编译器,交叉编译开发板中运行的可执行程序。--sysroot目录是代码编译后才生成的目录。

1. 可执行程序编译

其中路径要根据自己环境的实际情况来修改, 所有内容去掉换行成一行在终端中去运行。

/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang -I/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl/usr/include/arm-linux-ohos -I/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/include/c++/v1 --target=arm-linux-ohos --sysroot=/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl -L/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/lib/arm-linux-ohos/c++ -L/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl/usr/lib/arm-linux-ohos -L/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/10.0.1/lib/arm-linux-ohos amazon_hello2.c -o am_hello2

 更精简:

/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang --target=arm-linux-ohos --sysroot=/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl amazon_hello2.c -o am_hello2

2. 动态库编译

其中路径要根据自己环境的实际情况来修改, 所有内容去掉换行成一行在终端中去运行。

/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang --target=arm-linux-ohos --sysroot=/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl -I/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl/usr/include/arm-linux-ohos -I/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/include/c++/v1 -I../include -shared -L/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/lib/arm-linux-ohos/c++ -L/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl/usr/lib/arm-linux-ohos -L/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/10.0.1/lib/arm-linux-ohos oh_shared.c -o libhhshared.z.so

更精简:

/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang --target=arm-linux-ohos --sysroot=/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl -I../include -shared oh_shared.c -o libhhshared.z.so

3. 配置环境变量

1. 执行命令:vim .bashrc2. 添加如下代码:export clang_path=/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/binexport PATH=$clang_path:$PATHexport SROOT="--target=arm-linux-ohos --sysroot=/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl"export CINC="-I/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl/usr/include/arm-linux-ohos -I/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/include/c++/v1"export CLib="-L/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/lib/arm-linux-ohos/c++ -L/home/openharmony/L2.31/out/hi3516dv300/obj/third_party/musl/usr/lib/arm-linux-ohos -L/home/openharmony/L2.31/prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/10.0.1/lib/arm-linux-ohos"export CDefines="-DV8_DEPRECATION_WARNINGS -DNO_TCMALLOC -D_GNU_SOURCE -DHAVE_SYS_UIO_H -D__MUSL__ -D_LIBCPP_HAS_MUSL_LIBC -D__BUILD_LINUX_WITH_CLANG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FORTIFY_SOURCE=2 -DCOMPONENT_BUILD -D__GNU_SOURCE=1 -DCHROMIUM_CXX_TWEAK_INLINES -D__MUSL__ -D_LIBCPP_HAS_MUSL_LIBC -D__BUILD_LINUX_WITH_CLANG -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0"export CFlags="-Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -funwind-tables -fcolor-diagnostics -fmerge-all-constants -Xclang -mllvm -Xclang -instcombine-lower-dbg-declare=0 -no-canonical-prefixes -ffunction-sections -fno-short-enums --target=arm-linux-ohos -march=armv7-a -mfloat-abi=softfp -mtune=generic-armv7-a -mfpu=neon -mthumb -Wall -Wextra -Wthread-safety -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-unneeded-internal-declaration -Wno-error=c99-designator -Wno-error=anon-enum-enum-conversion -Wno-error=sizeof-array-div -Wno-error=reorder-init-list -Wno-error=range-loop-construct -Wno-error=deprecated-copy -Wno-error=implicit-int-float-conversion -Wno-error=inconsistent-dllimport -Wno-error=unknown-warning-option -Wno-error=abstract-final-class -Wno-error=sign-compare -Wno-error=int-in-bool-context -Wno-error=xor-used-as-pow -Wno-error=return-stack-address -Wno-error=dangling-gsl -Wno-undefined-var-template -Wno-nonportable-include-path -Wno-user-defined-warnings -Wno-unused-lambda-capture -Wno-null-pointer-arithmetic -Wno-enum-compare-switch -O2 -fdata-sections -fomit-frame-pointer -gdwarf-3 -g2 -ggnu-pubnames -fno-common -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -fPIE"export cc="$clang_path/clang $CDefines $SROOT $CINC $CFlags "export cxx="$clang_path/clang++ $CDefines $SROOT $CINC $CFlags "export readelf=$clang_path/llvm-readobjexport ar=$clang_path/llvm-arexport ld="$clang_path/clang++ $SROOT $CLib "export ranlib=$clang_path/llvm-ranlibexport strip=$clang_path/llvm-stripexport CC=$ccexport CXX=$cxxexport READELF=$readelfexport AR=$arexport LD=$ldexport RANLIB=$ranlibexport STRIP=$stripalias cc=$ccalias cxx=$cxxalias readelf=$readelfalias ar=$aralias ld=$ldalias ranlib=$ranlibalias strip=$stripalias CC=$ccalias CXX=$cxxalias READELF=$readelfalias AR=$aralias LD=$ldalias RANLIB=$ranlibalias STRIP=$stri3. 让配置生效,执行:source ~/.bashrc 

这样就可以直接执行cc hello.c -o hello来交叉编译可执行程序了。

4. CMake交叉编译

资源见:(15条消息) openharmony的cmake交叉编译demo-C++文档类资源-CSDN文库

第一步解压:tar -zxvf cross_build_demo.tar.gz第二步切换到cross_build_demo目录:修改toolchain_arm.cmake中的路径第三步创建build:mkdir build第四步在buid目录下运行cmake:cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain_arm.cmake ..第五步运行make:make第六步将编译好的amazon_hello2放入开发板中去运行。