C++ Environment Config
info
配置环境 Mac
- C++
- CLion
MacOS Install C++ (不推荐)
Basic environment check 基础环境
# 查看命令行工具路径(有输出表示已安装)
xcode-select -p
# 没装的话安装它(会弹出安装窗口)
xcode-select --install
# 查看编译器版本(应看到 Apple clang)
clang++ --version
which clang++ # 一般是 /usr/bin/clang++
Compile and run Hello World
cat > main.cpp <<'CPP'
#include <iostream>
int main() {
std::cout << "Hello C++!\\n";
return 0;
}
CPP
# 用 C++17 编译
clang++ -std=c++17 -O2 main.cpp -o hello
# 运行
./hello # 预期输出:Hello C++!
Verify that the standard library link is normal
验证标准库链接是否正常
otool -L ./hello
# output:
./hello:
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1300.36.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1319.0.0)
Docker for C++ Development (已跑通)
Dockerfile
# 基于你指定的镜像
FROM registry.cn-hangzhou.aliyuncs.com/hellofss/kuiperinfer:latest
# SSH 配置:允许 root 密码登录(仅限本机开发环境使用)
RUN mkdir -p /var/run/sshd \
&& (sed -ri 's/^#?PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config || true) \
&& (sed -ri 's/^#?PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config || true) \
&& echo 'root:root' | chpasswd \
&& ssh-keygen -A
# 工作目录(把你的代码挂载到这里)
WORKDIR /work
EXPOSE 22
# 兼容不同发行版的 sshd 路径;以前台模式常驻
CMD ["/bin/bash","-lc","mkdir -p /var/run/sshd && (sshd -D -e || /usr/sbin/sshd -D -e)"]
start_cpp_dev.sh
#!/usr/bin/env bash
set -euo pipefail
# 端口可传参,默认 2200
PORT="${1:-2200}"
IMAGE_TAG="kuiperinfer-cpp-dev:latest"
CONTAINER_NAME="kuiperinfer-cpp-dev"
# Mac M 系列需要强制 amd64;x86 可去掉 --platform
BUILD_PLATFORM="--platform=linux/amd64"
RUN_PLATFORM="--platform=linux/amd64"
echo "==> Building image (${IMAGE_TAG})..."
docker build ${BUILD_PLATFORM} -t "${IMAGE_TAG}" .
# 若已存在旧容器,先移除
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}\$"; then
echo "==> Removing existing container ${CONTAINER_NAME}..."
docker rm -f "${CONTAINER_NAME}" >/dev/null 2>&1 || true
fi
echo "==> Starting container (${CONTAINER_NAME}) on port ${PORT}..."
docker run -d \
${RUN_PLATFORM} \
--name "${CONTAINER_NAME}" \
-p "${PORT}:22" \
-v "$PWD:/work" \
--restart unless-stopped \
"${IMAGE_TAG}"
echo "==> Done."
echo "SSH 方式:ssh -p ${PORT} root@localhost (密码:root)"
echo "CLion 远程工具链:Host=localhost, Port=${PORT}, User=root, Auth=Password(root)"
Connect to SSH
ssh root@127.0.0.1 -p 2200
# clone to local vs code
git clone <repo_url>
# try compile
mkdir build
cd build
cmake ..
make -j8
VS Code Setup
Plugins
- C/C++
- Code Runner
- CMake
- C/C++ Runner
Install Armadillo
C++ library for linear algebra & scientific computing
用于线性代数和科学计算的 C++ 库
MacOS
brew update
brew install armadillo openmpi openblas lapack gcc pkg-config
快速自检
uname -m # arm64 还是 x86_64
which brew # 你当前 shell 用的是哪个 brew
file /usr/local/lib/libarmadillo.dylib # 多半会显示 x86_64
Compile and run with x86_64
# 进入 x86_64 shell
arch -x86_64 zsh
# 激活 /usr/local 的 brew 环境
eval "$(/usr/local/bin/brew shellenv)"
# 确保库齐全(x86_64)
brew reinstall armadillo pkg-config openblas
# 编译(不必写 /opt 路径)
arch -x86_64 c++ example1.cpp -o example1 -std=c++17 -O2 \
-I/usr/local/include -L/usr/local/lib \
-larmadillo -framework Accelerate
# 也可以用 openblas: ... -larmadillo -lopenblas -llapack
# 验证与运行
file example1 # 应显示 x86_64
./example1
# output
example1: Mach-O 64-bit executable x86_64
Armadillo version: 15.0.1 (Medium Roast)
A.n_rows: 2
A.n_cols: 3
A:
0 0 0
0 0 4.5600e+02
A:
5.0000
A:
5.0000 5.0000 5.0000 5.0000 5.0000
5.0000 5.0000 5.0000 5.0000 5.0000
5.0000 5.0000 5.0000 5.0000 5.0000
5.0000 5.0000 5.0000 5.0000 5.0000
A:
0.1653 0.4540 0.9958 0.1241 0.0471
0.6888 0.0365 0.5528 0.9377 0.8664
0.3487 0.4794 0.5062 0.1457 0.4915
0.1487 0.6823 0.5712 0.8747 0.4446
0.2457 0.5952 0.4093 0.3678 0.3857
det(A): -0.0246018
inv(A):
1.2916 2.0000 -7.4695 -6.0752 11.8714
-0.1011 -0.4619 -1.5556 -0.9830 4.1651
0.8976 -0.1524 1.9191 1.2554 -3.6600
0.1869 0.6267 -2.6662 0.1198 1.8289
-1.7976 -0.9973 7.6647 3.9404 -9.2573
B( span(0,2), span(3,4) ):
0.1241 0.0471
0.9377 0.8664
0.1457 0.4915
B( 0,3, size(3,2) ):
0.1241 0.0471
0.9377 0.8664
0.1457 0.4915
B.row(0):
0.1653 0.4540 0.9958 0.1241 0.0471
B.col(1):
0.4540
0.0365
0.4794
0.6823
0.5952
B.t():
0.1653 0.6888 0.3487 0.1487 0.2457
0.4540 0.0365 0.4794 0.6823 0.5952
0.9958 0.5528 0.5062 0.5712 0.4093
0.1241 0.9377 0.1457 0.8747 0.3678
0.0471 0.8664 0.4915 0.4446 0.3857
max(B):
0.6888 0.6823 0.9958 0.9377 0.8664
max(B,1):
0.9958
0.9377
0.5062
0.8747
0.5952
max(max(B)) = 0.995795
...