1

LLVM (Clang) newbie question. I have installed the LLVM 11.0.0 on a clear Windows 10 mashine. What do I have to do to get an a.out for -target armv7a-none-eabi?

    #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...

Important:

  1. I need to cross-compile and get an output for -target armv7a-none-eabi
  2. no Visual Studio on that mashine installed
dnogcc
  • 11
  • 3
  • Does this answer your question? [How run clang from command line on Windows?](https://stackoverflow.com/questions/18711595/how-run-clang-from-command-line-on-windows) – Ari Fordsham Dec 16 '20 at 12:13
  • No, I do not have Visual Studio on that mashine where I want to compile – dnogcc Dec 17 '20 at 09:50
  • 1
    I think, in order to get useful information, you need to be more specific. It would be important to know what the target platform is. Are you sure that the libraries you are trying to use are available for your target platform? If so, have you installed them? – Xaver Dec 17 '20 at 22:20
  • I do not know if I have to install arm6.14.1 since all the files from arm6.14.1 are already in the LLVM\bin?? – dnogcc Dec 18 '20 at 06:01
  • I am trying to get working output for -target armv7a-none-eabi on windows, which means I am trying to cross compile on windows for arm, on a maschine that do not have Visual Studio installed, this is important, no Visual Studio. This is why I am using MINGW (64 bits) – dnogcc Dec 18 '20 at 06:09

2 Answers2

1

Typically, when installing LLVM for Windows, the path variable is adjusted automatically, so you don't have to modify it. 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).

What you have to do is the following:

  1. Run a shell (for example PowerShell) in a terminal.
  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.

After hat, you have a a.exe file.

Xaver
  • 1,035
  • 9
  • 17
  • I am trying to cross-compile from windows to ARM. When I compile with clang, I get an error missing stdio.h. When I compile with clang++ I got warning: unable to find a Visual Studio installation. What to do? When I compile with -target armv7a-none-eabi, I got missing stdio.h. What can I do to compile in WIndows where I do not have Visual Studio installed? – dnogcc Dec 16 '20 at 13:29
  • I installed Mingw (64bit), and added C:\Mingw\bin to the PATH. C:\Program FIles\LLVM\bin was in PATH by the installation of LLVM. I tried to compile with "C:\Program FIles\LLVM\bin\clang++.exe" -MD -x c++ "test.cpp" "-IC:\Mingw\include"-> error "unable to find Visual Studo installation" I tried to compile with: "C:\Program FIles\LLVM\bin\clang++.exe" -MD -x c++ "test.cpp" -target armv7a-none-eabi "-IC:\Mingw\include" "-IC:\Mingw\include\c++\3.4.5\"-> fatal error "bits/c++config.h" not found. c++config.h is only in the "C:\Mingw\include\c++\3.4.5\mingw32\bits" folder. Help?? – dnogcc Dec 17 '20 at 12:49
0

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. 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. Yes, they include a compiler, assembler, linker, C++ library. 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). Yes, it's true that the compiler/assembler that are included do support all of the LLVM targets. But you don't want just a compiler, you want a toolchain. A complete cross-target C/C++ toolchain will typically include:

  1. OS headers that declare types and functions
  2. C library headers that refer to the OS headers and declare types and functions, library/shared object archive that define functions.
  3. C++ library headers that declare templates, types and functions, library/shared object archive that define functions.
  4. target linker
  5. binutils: standalone assembler, objcopy, objdump, archiver, etc.

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. Your linked ./a.out executable could then run that printf code and expect it to emit something to some console or semihosted output.

I am trying to cross-compile from windows to ARM. When I compile with clang, I get an error missing stdio.h. When I compile with clang++ I got warning: unable to find a Visual Studio installation. What to do? When I compile with -target armv7a-none-eabi, I got missing stdio.h.

armv7a-none-eabi describes a target that has no operating system. If we had a stdio.h that declared printf, that would get us part of the way. But what should happen in the printf() implementation for this particular target?

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. Usually they're bundled with the toolchain itself. Sometimes the development board and corresponding toolchain come in one big bundle/BSP.

Since you asked for armv7a-none-eabi specifically, I would strongly recommend that you find a vendor to give you what you want. Especially if you need to use a Windows host.

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.

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. 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).

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; }.
  2. try to build an executable from a .c file with int main(void) {}
  3. try to build an executable from a .c file with a call to printf().

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. You can use libc++/libc++abi or libstdc++. C++ libraries on baremetal/freestanding targets are probably not very common. That said, there's lots of C++ library content that has little or no system dependencies. Especially the C++98/03 content that focused on STL - they probably only depend on the system heap allocator. Your example only shows std::vector<> and std::cout, so it's probably doable.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • Brian Cain, I like your answer! So, it means I have to build my own toolchain, on my developer mashine where I have Visual Studio 2017. So, I have get the source of LLVM llvm-11.0.0.src.tar.xz, I enabled every project with CMAKE, I used following: cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;compiler-rt;libc;libclc;libcxx;libcxxabi;libunwind;clang_rt;lld;mlir;parallel-libs;pstl" -G "Visual Studio 15 2017" -A x64 -Thost=x64 ../llvm I have opened the ALL_BUILD.vcxproj, and build everything. – dnogcc Dec 18 '20 at 07:21
  • So, did I got everything compiled for ARM? Or do I have to do something else? – dnogcc Dec 18 '20 at 07:21
  • Or do I have to do something totally different to get the toolchain for ARM on Windows? I just took the first target entry, but will use probably some other ARM target while compiling with CLang. What about using headers from MinGW? – dnogcc Dec 18 '20 at 07:37
  • Or, another question, is there a meaningfull way to generate libraries and headers for ARM on Linux, transfer the generated libraries and headers to Windows and use it on Windows? What about the libraries and headers that came with LLVM, are they not used for cross-compile? – dnogcc Dec 18 '20 at 09:57
  • I would expect that if you had a target C library installation tree (header files, target archives/shared objects), it probably shouldn't matter whether you got it from a Windows or a linux system. If you want to create your own cross toolchain, you should pick a C library that suits your target triple and your use case(s). – Brian Cain Dec 18 '20 at 13:56
  • Where can I get a Windows cross toolchain based on clang? Do I have to compile LLVM by myself to make it, or is there anyplace where I can download the complete LLVM toolchain, that works on WIndows not using Visual Studio as backend? I will not have Visual Studio on systems where I want to use LLVM. Please help – dnogcc Dec 21 '20 at 06:42
  • Another question: since it is statted that LLVM is cross-compile, does that mean, if I install the current LLVM 11 on Windows, that I can cross-compile for ARM on Windows, without having Visual Studio installed? I see that the installed LLVM uses Visuall Studio tools... – dnogcc Dec 21 '20 at 09:54