简体   繁体   English

在 Windows 10 mashine 上使用 LLVM 11.0.0 编译 cpp

[英]Compiling cpp using LLVM 11.0.0 on Windows 10 mashine

LLVM (Clang) newbie question. LLVM(Clang)新手问题。 I have installed the LLVM 11.0.0 on a clear Windows 10 mashine.我已经在一个清晰的 Windows 10 机器上安装了 LLVM 11.0.0。 What do I have to do to get an a.out for -target armv7a-none-eabi?我需要做什么才能为 -target armv7a-none-eabi 获取 a.out?

    #include <stdio.h>
    #include <iostream>
    #include <vector>

    int main(void) {
        int counter = 0;
        counter++;
        printf("counter: %d\n", counter);
        
        printf("c++14 output:");
        
        std::vector<int> vect{1, 2, 3, 4, 5};

        for (auto & el : vect)
            std::cout << "-" << el << std::endl;

        return counter;
    }

Please write in detail what do I have to do, where to get needed headers, what to put in PATH, etc...请详细写下我必须做什么,在哪里获得所需的标头,在 PATH 中放置什么等...

Important:重要的:

  1. I need to cross-compile and get an output for -target armv7a-none-eabi我需要交叉编译并为 -target armv7a-none-eabi 获取 output
  2. no Visual Studio on that mashine installed该机器上没有安装 Visual Studio

Typically, when installing LLVM for Windows, the path variable is adjusted automatically, so you don't have to modify it.通常,在为 Windows 安装 LLVM 时,路径变量会自动调整,因此您不必修改它。 Of course, when installing LLVM, you have to make sure to install all files that are relevant for your build target (in your case: armv7a-none-eabi ).当然,在安装 LLVM 时,您必须确保安装与构建目标相关的所有文件(在您的情况下: armv7a-none-eabi )。

What you have to do is the following:您需要做的是:

  1. Run a shell (for example PowerShell ) in a terminal.在终端中运行 shell(例如PowerShell )。
  2. Change to the folder that contains your source file.切换到包含源文件的文件夹。
  3. Type clang -target armv7a-none-eabi myfile.cpp (provided you file's name is myfile.cpp ) and press enter.键入clang -target armv7a-none-eabi myfile.cpp (假设您的文件名为myfile.cpp )并按 Enter。

After hat, you have a a.exe file.帽子之后,你有一个a.exe文件。

The bad news is that you can't really do exactly what you're asking for very easily.坏消息是,你真的不能很容易地完全按照你的要求去做。 Unless you are terribly ambitious or tasked with creating the developer toolchain that can do what you ask, you don't need to bother with the rest below.除非您非常雄心勃勃或负责创建可以满足您要求的开发人员工具链,否则您无需为下面的 rest 烦恼。 Switch to a platform like Ubuntu linux that has easily-installed (gcc) cross toolchains, or ask your BSP vendor for a Windows cross toolchain based on clang. Switch to a platform like Ubuntu linux that has easily-installed (gcc) cross toolchains, or ask your BSP vendor for a Windows cross toolchain based on clang.


The release tarballs provided by LLVM community at https://releases.llvm.org/ are best suited for native builds. LLVM 社区在https://releases.llvm.org/提供的发布 tarball 最适合原生构建。 Yes, they include a compiler, assembler, linker, C++ library.是的,它们包括编译器、汇编器、linker、C++ 库。 They all work together well as a native toolchain (but will refer to the native host C library, and the native host linker by default).它们都可以作为原生工具链很好地协同工作(但默认情况下会引用原生主机 C 库和原生主机 linker)。 Yes, it's true that the compiler/assembler that are included do support all of the LLVM targets.是的,包含的编译器/汇编器确实支持所有 LLVM 目标。 But you don't want just a compiler, you want a toolchain.但是你不想要一个编译器,你想要一个工具链。 A complete cross-target C/C++ toolchain will typically include:一个完整的跨目标 C/C++ 工具链通常包括:

  1. OS headers that declare types and functions声明类型和函数的 OS 头文件
  2. C library headers that refer to the OS headers and declare types and functions, library/shared object archive that define functions. C 库头文件引用 OS 头文件并声明类型和函数,库/共享 object 归档文件定义函数。
  3. C++ library headers that declare templates, types and functions, library/shared object archive that define functions.声明模板、类型和函数的 C++ 库头文件,定义函数的库/共享 object 存档。
  4. target linker目标 linker
  5. binutils: standalone assembler, objcopy, objdump, archiver, etc. binutils:独立的汇编器、objcopy、objdump、归档器等。

With #2 above you could make a call to printf() and the linker would be able to find its implementation in the C library archive.使用上面的#2,您可以调用printf()并且 linker 将能够在 C 库存档中找到它的实现。 Your linked ./a.out executable could then run that printf code and expect it to emit something to some console or semihosted output.然后,您链接的./a.out可执行文件可以运行该 printf 代码,并期望它向某些控制台或半托管 output 发出一些信息。

I am trying to cross-compile from windows to ARM.我正在尝试从 windows 交叉编译到 ARM。 When I compile with clang, I get an error missing stdio.h.当我使用 clang 编译时,我收到一个缺少 stdio.h 的错误。 When I compile with clang++ I got warning: unable to find a Visual Studio installation.当我使用 clang++ 编译时,我收到警告:无法找到 Visual Studio 安装。 What to do?该怎么办? When I compile with -target armv7a-none-eabi, I got missing stdio.h.当我使用 -target armv7a-none-eabi 编译时,我丢失了 stdio.h。

armv7a-none-eabi describes a target that has no operating system. armv7a-none-eabi描述了一个没有操作系统的目标。 If we had a stdio.h that declared printf, that would get us part of the way.如果我们有一个声明 printf 的 stdio.h,那将让我们参与其中。 But what should happen in the printf() implementation for this particular target?但是这个特定目标的printf()实现应该发生什么?

What most users want is a vendor to provide the C library headers and archive -- a commercial or open source distributor who packages up a C library.大多数用户想要的是提供 C 库头文件和存档的供应商——打包 C 库的商业或开源分销商。 Usually they're bundled with the toolchain itself.通常它们与工具链本身捆绑在一起。 Sometimes the development board and corresponding toolchain come in one big bundle/BSP.有时开发板和相应的工具链包含在一个大捆绑包/BSP 中。

Since you asked for armv7a-none-eabi specifically, I would strongly recommend that you find a vendor to give you what you want.由于您特别要求armv7a-none-eabi ,我强烈建议您找到供应商来提供您想要的东西。 Especially if you need to use a Windows host.特别是如果您需要使用 Windows 主机。

If you aren't stuck on Windows, or are willing to use WSL: Debian and Ubuntu provide cross toolchains and C libraries (including ones like libnewlib-arm-none-eabi ). If you aren't stuck on Windows, or are willing to use WSL: Debian and Ubuntu provide cross toolchains and C libraries (including ones like libnewlib-arm-none-eabi ). Unfortunately, I don't think there's any clang-based cross toolchain that would leverage this C library.不幸的是,我认为没有任何基于 clang 的交叉工具链可以利用这个 C 库。

While you could try to bind libnewlib-arm-none-eabi with a clang tarball from https://releases.llvm.org/ , it won't be particularly easy.虽然您可以尝试将libnewlib-arm-none-eabihttps://releases.llvm.org/中的 clang 压缩包绑定,但这并不是特别容易。 I would start off by reviewing https://releases.llvm.org/11.0.0/tools/clang/docs/UsersManual.html#configuration-files - create a config file that references the relevant include path(s) and library path(s).我将首先查看https://releases.llvm.org/11.0.0/tools/clang/docs/UsersManual.html#configuration-files - 创建一个引用相关包含路径和库路径的配置文件(s)。

Once you have the config file prototyped, start small and build:将配置文件原型化后,从小处着手并构建:

  1. try to compile an object file for your target from int foo(void) { return 33; }尝试从int foo(void) { return 33; } int foo(void) { return 33; } . int foo(void) { return 33; }
  2. try to build an executable from a.c file with int main(void) {}尝试使用int main(void) {}从 a.c 文件构建可执行文件
  3. try to build an executable from a.c file with a call to printf() .尝试通过调用printf()从 a.c 文件构建可执行文件。

If you get through those three steps, congratulations, you probably had to figure out a lot of interesting challenges.如果你通过了这三个步骤,那么恭喜你,你可能不得不找出很多有趣的挑战。 If you want std::cout and friends to work, you will need to build a C++ library for your target.如果你想让std::cout和朋友工作,你需要为你的目标构建一个 C++ 库。 You can use libc++/libc++abi or libstdc++.您可以使用 libc++/libc++abi 或 libstdc++。 C++ libraries on baremetal/freestanding targets are probably not very common.裸机/独立目标上的 C++ 库可能不是很常见。 That said, there's lots of C++ library content that has little or no system dependencies.也就是说,有很多 C++ 库内容很少或没有系统依赖性。 Especially the C++98/03 content that focused on STL - they probably only depend on the system heap allocator.尤其是专注于 STL 的 C++98/03 内容——它们可能只依赖于系统堆分配器。 Your example only shows std::vector<> and std::cout , so it's probably doable.您的示例仅显示std::vector<>std::cout ,因此它可能是可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM