CLANG 交叉编译

boost+clang+

bash 复制代码
cd /root/dev_mipsel/boost

# 清理之前的编译
rm -rf stage bin.v2

# 创建 clang 工具链配置
cat > user-config.jam << 'EOF'
using clang : mipsel
    : clang++ --target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu
    : <cxxflags>-fPIC
      <cxxflags>-march=mips32r2
      <cxxflags>-mabi=32
      <cflags>-fPIC
      <linkflags>--target=mipsel-linux-gnu
      <linkflags>--sysroot=/usr/mipsel-linux-gnu
      <linkflags>-fuse-ld=lld
    ;
EOF

# 配置并编译 Boost
./bootstrap.sh --with-toolset=clang

# 编译 Boost
./b2 -j$(nproc) \
    toolset=clang-mipsel \
    target-os=linux \
    architecture=mips \
    address-model=32 \
    binary-format=elf \
    abi=o32 \
    cxxflags="-fPIC --target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu" \
    linkflags="--target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu -fuse-ld=lld" \
    variant=release \
    link=static \
    runtime-link=shared \
    --user-config=user-config.jam \
    stage

CMakeLists.txt

bash 复制代码
cmake_minimum_required(VERSION 3.10)
project(ppp LANGUAGES C CXX)

# 设置 Clang 交叉编译标志
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_C_COMPILER_TARGET mipsel-linux-gnu)
set(CMAKE_CXX_COMPILER_TARGET mipsel-linux-gnu)

# 添加目标特定的标志
add_compile_options(
    --target=mipsel-linux-gnu
    --sysroot=/usr/mipsel-linux-gnu
    -fPIC
    -march=mips32r2
    -mabi=32
    -O3
    -std=c++17
)

# 链接器标志
add_link_options(
    --target=mipsel-linux-gnu
    --sysroot=/usr/mipsel-linux-gnu
    -fuse-ld=lld
    -static-libstdc++
)

# 查找 Boost 库
set(BOOST_ROOT "/root/dev_mipsel/boost")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS system coroutine thread context regex filesystem)

# 添加可执行文件
add_executable(ppp main.cpp ...)

# 链接库
target_link_libraries(ppp
    ${Boost_LIBRARIES}
    ssl
    crypto
    pthread
    dl
    atomic
)

Test:

bash 复制代码
cmake_minimum_required(VERSION 3.10)
project(ppp LANGUAGES C CXX)

# 设置 Clang 交叉编译标志
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_C_COMPILER_TARGET mipsel-linux-gnu)
set(CMAKE_CXX_COMPILER_TARGET mipsel-linux-gnu)

# 添加目标特定的标志
add_compile_options(
    --target=mipsel-linux-gnu
    --sysroot=/usr/mipsel-linux-gnu
    -fPIC
    -march=mips32r2
    -mabi=32
    -O3
    -std=c++17
)

# 链接器标志
add_link_options(
    --target=mipsel-linux-gnu
    --sysroot=/usr/mipsel-linux-gnu
    -fuse-ld=lld
    -static-libstdc++
)

# 查找 Boost 库
set(BOOST_ROOT "/root/dev_mipsel/boost")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS system coroutine thread context regex filesystem)

# 添加可执行文件
add_executable(ppp main.cpp ...)

# 链接库
target_link_libraries(ppp
    ${Boost_LIBRARIES}
    ssl
    crypto
    pthread
    dl
    atomic
)

build.SH

bash 复制代码
#!/bin/bash
# build-mips-clang.sh

# 设置环境变量
export CC="clang --target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu"
export CXX="clang++ --target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu"
export CFLAGS="-fPIC -march=mips32r2 -mabi=32 -O3"
export CXXFLAGS="-fPIC -march=mips32r2 -mabi=32 -O3 -std=c++17"
export LDFLAGS="--target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu -fuse-ld=lld"
export AR="llvm-ar"
export RANLIB="llvm-ranlib"
export STRIP="llvm-strip"

# 清理并创建构建目录
rm -rf build-mips-clang
mkdir build-mips-clang && cd build-mips-clang

# 配置 CMake
cmake .. \
    -DCMAKE_SYSTEM_NAME=Linux \
    -DCMAKE_SYSTEM_PROCESSOR=mipsel \
    -DCMAKE_C_COMPILER=clang \
    -DCMAKE_C_COMPILER_TARGET=mipsel-linux-gnu \
    -DCMAKE_CXX_COMPILER=clang++ \
    -DCMAKE_CXX_COMPILER_TARGET=mipsel-linux-gnu \
    -DCMAKE_C_FLAGS="${CFLAGS}" \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS}" \
    -DCMAKE_EXE_LINKER_FLAGS="${LDFLAGS}" \
    -DCMAKE_SHARED_LINKER_FLAGS="${LDFLAGS}" \
    -DCMAKE_AR="llvm-ar" \
    -DCMAKE_RANLIB="llvm-ranlib" \
    -DCMAKE_STRIP="llvm-strip" \
    -DBOOST_ROOT=/root/dev_mipsel/boost \
    -DBoost_USE_STATIC_LIBS=ON \
    -DCMAKE_BUILD_TYPE=Release

# 构建
make -j$(nproc)

CMakeFile.txt

bash 复制代码
# 设置系统名称和处理器
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR mipsel)

# 指定交叉编译工具链
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET mipsel-linux-gnu)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET mipsel-linux-gnu)

# 设置编译和链接标志
set(CMAKE_C_FLAGS "--target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu -fPIC -march=mips32r2 -mabi=32" CACHE STRING "C flags")
set(CMAKE_CXX_FLAGS "--target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu -fPIC -march=mips32r2 -mabi=32" CACHE STRING "C++ flags")
set(CMAKE_EXE_LINKER_FLAGS "--target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu -fuse-ld=lld" CACHE STRING "Executable linker flags")
set(CMAKE_SHARED_LINKER_FLAGS "--target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu -fuse-ld=lld" CACHE STRING "Shared library linker flags")

# 设置库查找路径,确保找到交叉编译的库
set(CMAKE_FIND_ROOT_PATH /usr/mipsel-linux-gnu)
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)

AAA

bash 复制代码
PROJECT(${NAME} C CXX) # CMAKE_CURRENT_SOURCE_DIR

SET(CMAKE_C_COMPILER clang)
SET(CMAKE_C_COMPILER_TARGET mipsel-linux-gnu)
SET(CMAKE_CXX_COMPILER clang++)
SET(CMAKE_CXX_COMPILER_TARGET mipsel-linux-gnu)

# C/CXX compiler configurations.
SET(CMAKE_C_FLAGS "--target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu -fPIC -march=mips32r2 -mabi=32 -fvisibility=hidden -Wno-format -Wno-implicit-function-declaration")
SET(CMAKE_CXX_FLAGS "--target=mipsel-linux-gnu --sysroot=/usr/mipsel-linux-gnu -fPIC -march=mips32r2 -mabi=32 -Wno-format -Wno-inconsistent-missing-override -Wno-pointer-bool-conversion -Wno-unsequenced -Wno-null-dereference -Wno-gnu-variable-sized-type-not-at-end -Wno-tautological-undefined-compare -Wno-deprecated-declarations -Wno-shift-count-overflow")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -gcc-toolchain /usr")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gcc-toolchain /usr")
相关推荐
独角鲸网络安全实验室1 分钟前
2026微信小程序抓包全解析:从实操落地到合规风控,解锁前端调试新范式
前端·微信小程序·小程序·抓包·系统代理绕过·https证书严格校验·进程隔离
紫微AI1 分钟前
前端文本测量成了卡死一切创新的最后瓶颈,pretext实现突破了
前端·人工智能·typescript
GISer_Jing2 分钟前
AI前端(From豆包)
前端·aigc·ai编程
IT枫斗者2 分钟前
前端部署后如何判断“页面是不是最新”?一套可落地的版本检测方案(适配 Vite/Vue/React/任意 SPA)
前端·javascript·vue.js·react.js·架构·bug
测试修炼手册2 分钟前
[测试技术] 深入理解 JSON Web Token (JWT)
前端·json
有谁看见我的剑了?2 分钟前
linux 添加硬盘后系统识别不到硬盘处理
linux·运维·服务器
AI老李4 分钟前
2026 年 Web 前端开发的 8 个趋势!
前端
里欧跑得慢6 分钟前
15. Web可访问性最佳实践:让每个用户都能平等访问
前端·css·flutter·web
钛态6 分钟前
前端趋势:别被时代抛弃
前端·vue·react·web
小码哥_常24 分钟前
Room 3.0:移动端持久化的“重生”变革
前端