Mac M2(arm64) homebrew安装llvm

homebrew直接安装llvm,遇到了一大堆环境配置问题,做简单记录,供参考。

问题

问题包括但不限于

  1. 头文件定义问题
Undefined symbols for architecture arm64:   "_LLVMInitializeAArch64AsmParser", referenced from:       _main in main-83696a.o
  1. 目标文件链接问题
Undefined symbols for architecture arm64:
  "_LLVMLinkInMCJIT", referenced from:
      __GLOBAL__sub_I_main.cpp in main-483380.o
  "llvm::DisableABIBreakingChecks", referenced from:
      llvm::VerifyDisableABIBreakingChecks in main-483380.o
ld: symbol(s) not found for architecture arm64
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lLLVM-16
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)

有些问题过程中遇到但是忘了记录。

解决

  1. bin加到$PATH中,安装的llvm是带clang和clang++的,为了避免与本机自带clang冲突,所以把llvm的bin加到PATH的最前面:
export LLVM_BIN_PATH=/opt/homebrew/opt/llvm/bin
export PATH=$LLVM_BIN_PATH:$PATH

llvm的安装路径可能不同

  1. 把clang编译所需头文件加到环境变量中
export LLVM_INCLUDE_PATH=/opt/homebrew/Cellar/llvm/16.0.6/include
export CPP_INCLUDE_PATH=/opt/homebrew/Cellar/llvm/16.0.6/include/c++/v1
export CLANG_INCLUDE_PATH=/opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include

经过一系列debug和查找,我把以上三个路径加到了CPLUS_INCLUDE_PATH

export CPLUS_INCLUDE_PATH=$LLVM_INCLUDE_PATH:$CPLUS_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=$CLANG_INCLUDE_PATH:$CPLUS_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=$CPP_INCLUDE_PATH:$CPLUS_INCLUDE_PATH

注意要把c++的头文件放在c的头文件前面,不然由于版本不对,clang++先找到了c版本的头文件(个人理解,未经证实),会出现编译问题,比如上面提到的undefined ...
3. 把clang编译所需库文件加到环境变量中

export LLVM_LIB_PATH=/opt/homebrew/opt/llvm/lib/
export LIBRARY_PATH=$LLVM_LIB_PATH:$LIBRARY_PATH

这里问gpt应该怎么命名环境变量,gpt说是LD_LIBRARY_PATH(Linux)orDYLD_LIBRARY_PATH(Mac),结果试了半天都不行,看了一下ld版本,用的是~/anaconda/bin/ld,就问gpt是不是anaconda的ld配置方法不太一样,gpt说有可能是,再追问,它说应该用LIBRARY_PATH,最后通过了。
但是用系统自带的\usr\bin\ld进行测试,仍然是LIBRARY_PATH有效而DYLD_LIBRARY_PATH无效。不太清楚原因,因为用的是静态库?但是目标库是动态库libLLVM-16.dylib呀?

ENDING:

最后有一个帮助我debug的方法:clang的-v参数,可以输出编译的详细信息

> clang++ -O3 main.cpp  -v  -lLLVM-16 -o main
> Homebrew clang version 16.0.6
Target: arm64-apple-darwin22.4.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin
 "/opt/homebrew/Cellar/llvm/16.0.6/bin/clang-16" -cc1 -triple arm64-apple-macosx13.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu apple-m1 -target-feature +v8.5a -target-feature +crc -target-feature +crypto -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +lse -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +neon -target-feature +zcm -target-feature +zcz -target-feature +fullfp16 -target-feature +sm4 -target-feature +sha3 -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=lldb -target-linker-version 857.1 -v -fcoverage-compilation-dir=/Users/***/work/ZJU/2023_09_06_text_to_code_plus_llvm/LLVM_test/JIT_test -resource-dir /opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk -cxx-isystem /opt/homebrew/Cellar/llvm/16.0.6/include/c++/v1 -cxx-isystem /opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include -cxx-isystem /opt/homebrew/Cellar/llvm/16.0.6/include -cxx-isystem . -internal-isystem /opt/homebrew/opt/llvm/bin/../include/c++/v1 -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/local/include -internal-isystem /opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include -O3 -fdeprecated-macro -fdebug-compilation-dir=/Users/***/work/ZJU/2023_09_06_text_to_code_plus_llvm/LLVM_test/JIT_test -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -vectorize-loops -vectorize-slp -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/ps/0f73rsr921q3qfm6g_404sz40000gn/T/main-601357.o -x c++ main.cpp
clang -cc1 version 16.0.6 based upon LLVM 16.0.6 default target arm64-apple-darwin22.4.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/Library/Frameworks"
ignoring duplicate directory "/opt/homebrew/Cellar/llvm/16.0.6/include/c++/v1"
ignoring duplicate directory "/opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/homebrew/Cellar/llvm/16.0.6/include/c++/v1
 /opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include
 /opt/homebrew/Cellar/llvm/16.0.6/include
 .
 /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Users/***/anaconda3/bin/ld" -demangle -lto_library /opt/homebrew/Cellar/llvm/16.0.6/lib/libLTO.dylib -dynamic -arch arm64 -platform_version macos 13.0.0 13.0.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk -o main -L/opt/homebrew/opt/llvm/lib/ -L. /var/folders/ps/0f73rsr921q3qfm6g_404sz40000gn/T/main-601357.o -lLLVM-16 -lc++ -lSystem /opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/lib/darwin/libclang_rt.osx.a

也可以用命令查看相关信息

clang++ -v -E -x c++ - < /dev/null

另外clang++和gcc使用的头文件好像并不通用,在配置好clang/llvm后,gcc就无法使用了,不过还好我是在zsh里面配置的,我还有bash可以用,所以以后就zsh用clang,bash用gcc了。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值