clang - 如何使用 clang/LLVM 交叉编译 Coreutils 或其他 GNU 项目?

标签 clang cross-compiling gnu-coreutils

我很难用 llvm 为其他架构编译 Coreutils:arm/aarch64/mips/mips32/ppc/ppc32...

因为我安装了所有的 gcc-cross 工具,比如 mips-linux-gnu , powerpc64-linux-gnu如果我有一个像 test.c 这样的简单 C 程序

#include<stdio.h>
int main(){
    printf("hello!");
    return 0;
}

我可以将它编译到拱门,即
clang --target=mips64-linux-gnuabi64 test.c -o test-mips64
➜  tests file test-mips64 
test-mips64: ELF 64-bit MSB executable, MIPS, MIPS64 rel2 version 1 (SYSV), dynamically linked, interpreter /lib64/ld.so.1, BuildID[sha1]=7b33d55a0d08e6cd18d966341590dc351e346a78, for GNU/Linux 3.2.0, not stripped

我尝试以相同的方式编译尝试设置的 Coreutils
export CC=clang
export CXX=clang++
CFLAGS = "--target=mips64-linux-gnuabi64"
./configure --host=mips64-linux-gnuabi64

但是,每次在配置或制作中出错...

我应该如何设置配置?我可以轻松地使用 llvm 为其他 archs 编译 Coreuntils 吗?

最佳答案

为交叉编译获取正确的命令行选项有点棘手。假设您在基于 Debian 的系统(如 Debian 或 Ubuntu)上工作,我让它与下面的命令一起工作。以下是步骤。

  • 安装 gcc-mips64-linux-gnuabi64gcc-powerpc64-linux-gnu .
  • CFLAGS 选择正确的参数
  • -B/usr/mips64-linux-gnuabi64/bin/表明我们要使用链接器 ld在那个目录中。对 powerpc 做同样的事情。
  • --target=mips64-linux-gnuabi64表明我们的编译目标是什么。对 powerpc 做同样的事情。
  • -I/usr/mips64-linux-gnuabi64/include包含头文件。对 powerpc 做同样的事情。

  • 使用 ./configure --host=mips64-linux-gnuabi配置 mips64 和 ./configure --host=powerpc64-linux-gnueabi配置powerpc64。

  • 以下是为 mips64 编译的命令:
    make clean
    CFLAGS="-B/usr/mips64-linux-gnuabi64/bin/ --target=mips64-linux-gnuabi64 -I/usr/mips64-linux-gnuabi64/include" \
        ./configure --host=mips64-linux-gnuabi
    make
    
    以及为 powerpc64 编译的命令:
    make clean
    CFLAGS="-B/usr/powerpc64-linux-gnu/bin/ --target=powerpc64-linux-gnueabi -I/usr/powerpc64-linux-gnu/include" \
        ./configure --host=powerpc64-linux-gnueabi
    make
    
    这是 file ./src/ls 的输出证明它是一个 powerpc64 可执行文件:
    $ file ./src/ls
    ./src/ls: ELF 64-bit MSB executable, 64-bit PowerPC or cisco 7500, version 1 (SYSV), dynamically linked, interpreter /lib64/ld64.so.1, for GNU/Linux 3.2.0, BuildID[sha1]=97fe33981ca0112160f44a6fb678d6dc1b462114, not stripped
    

    下面是一个 Dockerfile,可用于为 mips64 和 powerpc64 可重复地交叉编译 coreutils。
    # Cross-compile GNU coreutils for mips64 and powerpc64 using clang.
    # With help from https://medium.com/@wolfv/cross-compiling-arm-on-travis-using-clang-and-qemu-2b9702d7c6f3
    
    FROM debian:buster
    
    # Install compile-time dependencies.
    RUN apt-get update \
        && apt-get install --yes \
            clang \
            curl \
            gcc-mips64-linux-gnuabi64 \
            gcc-powerpc64-linux-gnu \
            make \
            perl \
        && rm -rf /var/lib/apt/lists/*
    
    # Download source code for release.
    WORKDIR /tmp/coreutils
    RUN curl -fsSL https://ftp.gnu.org/gnu/coreutils/coreutils-8.32.tar.xz \
        | tar xJ --strip-components 1
    
    # Compile and install for mips64.
    RUN CFLAGS="-B/usr/mips64-linux-gnuabi64/bin/ --target=mips64-linux-gnuabi64 -I/usr/mips64-linux-gnuabi64/include" \
            ./configure --host=mips64-linux-gnuabi --prefix=/opt/coreutils-mips \
        && make \
        && make install
    
    # Compile and install for powerpc64.
    RUN make clean \
        && CFLAGS="-B/usr/powerpc64-linux-gnu/bin/ --target=powerpc64-linux-gnueabi -I/usr/powerpc64-linux-gnu/include" \
            ./configure --host=powerpc64-linux-gnueabi --prefix=/opt/coreutils-powerpc64 \
        && make \
        && make install
    
    # Keep only the compiled programs from the previous stage.
    FROM debian:buster
    COPY --from=0 /opt /opt
    

    关于clang - 如何使用 clang/LLVM 交叉编译 Coreutils 或其他 GNU 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62391660/

    相关文章:

    c++ - Clang AST Matcher和AST Visitor之间有什么区别?

    iphone - 我应该在我的 C++ iPhone 项目中开始使用 Clang/LLVM 吗?

    linux - 使用 MSBuild 进行远程 Linux 编译

    CMake "ld.exe:cannot open linker script file"

    bash - 如何在等待用户输入时更改 Bash 脚本的输出?

    c++ - enable_if 的句法模式

    c++ - MacOS clang 通过 Homebrew 在 Mojave `wchar.h` 下损坏

    android - android NDK中预构建工具链和自定义工具链编译器之间的区别

    linux - 如果 coreutils 排序中的数值相等,是否保留原始顺序?

    linux - 为什么 touch - 不返回任何错误但不创建文件 -?