
为了整理思路,文章采用模拟2人对话方式。
本文主要内容
export PATH="/usr/local/opt/llvm/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/llvm/lib/c++ -L/usr/local/opt/llvm/lib/unwind -lunwind"
export CPPFLAGS="-I/usr/local/opt/llvm/include“操作系统 | 开发语言 | 编译器 | 连接器 | c++标准库 | 集成工具 |
|---|---|---|---|---|---|
Linux | c/c++ | GCC | GNU Linker (ld) | libstdc++ | gcc/g++ |
macOS | c/c++ | Clang | LLVM Linker (lld) | libc++ | |
Windows | c++ | MSVC | link.exe | Microsoft Visual C++/MinGW | |
跨平台 | Go | gc | |||
跨平台 | rust | rustc |
提问:gcc是编译c语言代码,g++编译cpp代码,有什么区别 或者说c与cpp有什么区别?
gcc hello.cpp -lstdc++
如果想使用 gcc 指令来编译执行 C++ 程序,需要在使用 gcc 指令时,手动为其添加
-lstdc++ 选项,表示 gcc 在编译 C++ 程序时可以链接必要的 C++ 标准库
该程序中使用了标准库 <iostream> 和 <string> 提供的类对象,而 gcc 默认是无法找到它们的
提问:标准库:libc ,libc++, libstdc++区别?
ldd a.out
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f62cd56f000) c++
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f62cd346000) mac 使用Clang配置VS Code C/C++环境
mac 版本:
sw_vers
ProductName: macOS
ProductVersion: 15.2
BuildVersion: 24C101
问题:
Warning: You are using macOS 12.
We (and Apple) do not provide support for this old version.
但在今年9月份的一次更新中,Brew 放弃了对 Monterey 的支持
解决:
升级os版本 耗时一个晚上installing LLVM on MacOS
xcode-select --install //大约15分钟
xcode-select -p
/Applications/Xcode.app/Contents/Developer
brew update
xcode-select --install //大约15分钟
brew install llvm //查看帮助文档:brew info llvm
==> llvm: stable 19.1.6 (bottled), HEAD [keg-only]
Next-gen compiler infrastructure
https://llvm.org/
Installed
/usr/local/Cellar/llvm/19.1.6 (8,107 files, 2GB)
Poured from bottle using the formulae.brew.sh API on 2025-01-12 at 08:26:07
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/l/llvm.rb
License: Apache-2.0 WITH LLVM-exception
==> Dependencies
Build: cmake, ninja, swig
Required: python@3.13, xz, z3, zstd
==> Options
--HEAD
Install HEAD version
==> Caveats
CLANG_CONFIG_FILE_SYSTEM_DIR: /usr/local/etc/clang
CLANG_CONFIG_FILE_USER_DIR: ~/.config/clang
LLD is now provided in a separate formula:
brew install lld
We plan to build LLVM 20 with `LLVM_ENABLE_EH=OFF`. Please see:
https://github.com/orgs/Homebrew/discussions/5654
Using `clang`, `clang++`, etc., requires a CLT installation at `/Library/Developer/CommandLineTools`.
If you don't want to install the CLT, you can write appropriate configuration files pointing to your
SDK at ~/.config/clang.
To use the bundled libunwind please use the following LDFLAGS:
LDFLAGS="-L/usr/local/opt/llvm/lib/unwind -lunwind"
To use the bundled libc++ please use the following LDFLAGS:
LDFLAGS="-L/usr/local/opt/llvm/lib/c++ -L/usr/local/opt/llvm/lib/unwind -lunwind"
NOTE: You probably want to use the libunwind and libc++ provided by macOS unless you know what you're doing.
llvm is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
If you need to have llvm first in your PATH, run:
echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.zshrc
For compilers to find llvm you may need to set:
export LDFLAGS="-L/usr/local/opt/llvm/lib"
export CPPFLAGS="-I/usr/local/opt/llvm/include"编译环境配置:
echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.bashrc
//这将使你的终端会话能够找到 LLVM 的 `clang` 和 `clang++` 命令。
clang -v
Homebrew clang version 19.1.6
Target: x86\_64-apple-darwin24.2.0
Thread model: posix
InstalledDir: /usr/local/Cellar/llvm/19.1.6/bin
Configuration file: /usr/local/etc/clang/x86\_64-apple-darwin24.cfg
System configuration file directory: /usr/local/etc/clang设置编译器标志
To use the bundled libc++ please use the following LDFLAGS:
echo 'export LDFLAGS="-L/usr/local/opt/llvm/lib - L/usr/local/opt/llvm/lib/c++ -L/usr/local/opt/llvm/lib/unwind -lunwind"' >> ~/.bashrc
echo 'export CPPFLAGS="-I/usr/local/opt/llvm/include"' >> ~/.bashrc
source ~/.bashrcHow to use LDFLAGS in makefile
Makefile选项CPPFLAGS :
Makefile选项LDFLAGS

说明:
GCC程序的编译过程大概分四个阶段
Using built-in specs.
COLLECT\_GCC=g++
COLLECT\_LTO\_WRAPPER=/usr/lib/gcc/x86\_64-linux-gnu/11/lto-wrapper
OFFLOAD\_TARGET\_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD\_TARGET\_DEFAULT=1
Target: x86\_64-linux-gnx
Thread model: posix默认情况下是使用动态链接
file hello
hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked
clang++ hello.cpp -o hello -L/usr/local/opt/llvm/lib/c++ -L/usr/local/opt/llvm/lib/unwind -lc++ -lunwind
export LDFLAGS="-L/usr/local/opt/llvm/lib/c++ -L/usr/local/opt/llvm/lib/unwind -lunwind"
clang++ hello.cpp -o hello
otool -L hello
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1800.105.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)lang++:使用 LLVM 的 C++ 编译器来编译 C++ 源代码。hello.cpp:你的 C++ 源文件。-o hello:指定输出文件名为 hello,即编译后生成的可执行文件名。-L/usr/local/opt/llvm/lib/c++:告诉编译器去 /usr/local/opt/llvm/lib/c++ 目录查找 libc++ 库,这是通过 Homebrew 安装的 LLVM 提供的库。-L/usr/local/opt/llvm/lib/unwind:告诉编译器去 /usr/local/opt/llvm/lib/unwind 目录查找 libunwind 库,这是 LLVM 提供的用于处理堆栈展开和异常的库。-lc++:指定使用 LLVM 提供的 libc++ 库。-lunwind:指定链接 LLVM 提供的 libunwind 库clang++ 将使用 Homebrew 安装的 LLVM 提供的 libc++ 和 libunwind,而不是系统自带的版本。小王提问:
/usr/lib/libc++.1.dylib 是 macOS 系统自带的 C++ 标准库,
不是通过 LLVM 安装的。这个编译不对
export LDFLAGS="-L/usr/local/opt/llvm/lib/c++ -L/usr/local/opt/llvm/lib/unwind -lunwind" export CPPFLAGS="-I/usr/local/opt/llvm/include
clang++ hello.cpp -o hello
otool -L hello
hello:
/usr/local/opt/llvm/lib/c++/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/local/opt/llvm/lib/unwind/libunwind.1.dylib (compatibility version 1.0.0, current version 1.0.0)完整过程:clang++ -v hello.cpp -o hello
clang++ -v -E hello.cpp -o hello.i
clang++ -v -c hello.s -o hello.o
clang++ -v hello.o -o hello
从你提供的 clang++ -v hello.cpp -o hello 命令的输出中,我们可以看到 clang++ 编译过程的详细信息,涉及几个关键步骤:
clang version 19.1.6:表示你正在使用 Homebrew 安装的 clang++ 版本 19.1.6。x86_64-apple-darwin24.2.0,表明编译器为 macOS 的 x86_64 架构生成代码。/usr/local/Cellar/llvm/19.1.6/bin。预处理:hello.cpp,展开其中的宏和头文件。编译器在日志中显示了头文件搜索路径。例如,它会查找 #include 文件,在 /usr/local/Cellar/llvm/19.1.6/bin/../include/c++/v1 和 /usr/local/Cellar/llvm/19.1.6/lib/clang/19/include 等目录中查找标准库。编译:clang++ 将 C++ 源代码文件 hello.cpp 编译为目标文件(.o 文件)。它调用了 clang-19(clang++ 的前端),并且使用了很多编译选项,如 -target-sdk-version=15.2 来指定 SDK 版本。-fcxx-exceptions(启用 C++ 异常支持),-fexceptions(启用异常),-mrelocation-model pic(生成位置无关代码),等等。链接:ld(链接器)来链接生成目标文件(.o)和所需的标准库(如 libc++)。这会生成最终的可执行文件 hello。libc++ 和 libSystem),并使用 -syslibroot 指定了 macOS 的系统库路径。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。