How do I use a buildtool with clang? - c

I'm starting out with Linux programming, and I don't want to learn the gcc.
What buildtools can I use to compile large nested source directories with ease with clang? And cross platform? Cmake looks nice, but there is no mention of how to specify a different compiler.
edit: I'd like to use it with vim.

To use clang with cmake, you simply set the CC variable:
CC=clang cmake ...
There's also CXX for the C++ compiler, ie CXX=clang++.
CC/CXX are standard variables and should work with any build system.

Related

If I am building an OS, does it make sense for me to use my host OS's gcc compiler?

I am following the tutorial at https://littleosbook.github.io/ and wanted to understand whether or not what I have currently working is conceptually correct. In terms of where I am at, I am using macOS 10.15.7 for the development and was able to call a C function from the loader. The loader is in assembly. However, I used the Clang compiler (Apple clang version 12.0.0) to compile the C file in which the aforementioned C function is. Then, I compiled the C file to generate an object file and linked the .o file with loader.o
Is this how it should be done? Or should I be trying to firstly install gcc or clang inside the OS and have that compiler compile the C function for me?

Adding libraries to a C compiler

I use Linux and compile the C programs using inbuilt gcc compiler. While creating a program that is for the Windows platform I had to use certain predefined functions listed in <windows.h>. Similarly there are other functions whose libraries are not predefined in GCC compiler.
So how to add those customized libraries to the C compiler in Linux?
I guess you mean static libraries when you say libraries. You can include the library file path in your gcc command so that gcc can link your library or you can create a makefile and use 'make'. if you don't know how to use 'make' I recomend you to learn it.

How do I use custom assembler for clang?

I've compiled clang to use it as a cross compiler for ARM (by configuring it with ./configure --target=armv7l-unknown-linux-gnueabihf ), but when I try to compile any C code, it tries to use /usr/bin/as. I already have binutils compiled for ARM, and they are in a separate directory. How do I direct clang (or llvm) to use the assembler that I specify?
try passing the --host option to configure which will cause all the cc ar etc utilities to prefix with armv7l-unknown-linux-gnueabihf-
eg:
./configure --host=armv7l-unknown-linux-gnueabihf --build=i686-unknown-linux-gnu
Since you are using configure with hopefully autotools take a look at:
automake Cross compiling
I've always had trouble configuring from the source directory using ./configure and gave up in the end. These days I always configure from a separate directory i.e. ..//configure although I'm told it's recommended to use an absolute path for configure.
Your ARM binutils should be installed in the same prefix you're using for clang and make sure that they're in the path when you configure & build clang - i.e. PATH=/some/prefix/bin:$PATH; /configure --target=armv7l-unknown-linux-gnueabihf. If you're keeping them in separate directories for packaging purposes then make install DESTDIR= should help.
I don't generally build clang but the buildscripts I use for devkitARM might be helpful - http://sourceforge.net/p/devkitpro/buildscripts/ci/c372699fc7b4de90eb044314ce5bed04db640291/tree/

How I can get a cross compiler under Ubuntu for sparc target?

How I can get a cross compiler under Ubuntu for sparc target?
I want to compile my c and c++ program, working in Ubuntu, for sparc architecture? How i can do this? Can I use mingw32 cross compiler?
You need to compile a gcc cross compiler. The first step is to download the source code for gcc, bin-utils (gnu as, ld, etc), and the standard library for the platform.
Once you have the necessary source code(s) you need to configure, make, and then install the cross compiler without clobbering your host gcc.
./configure --target=$TARGET --prefix=$PREFIX
make
make install
Rinse and repeat for bin-utils. I believe you'll have to pass in the location of the source for the standard library to configure, but I don't know what the argument is. I've just done this for OS dev where you don't really need it. Look into newlib if you're having trouble with this.
$TARGET is obviously the target platform, for you it'll be a string like sparc-elf or sparc64-elf, but it depends.
$PREFIX is where your cross compiler will be located. It will be named something like $TARGET-gcc. So this is optional, just make sure it ends up on your path.
http://www.netgull.com/gcc/releases/gcc-4.6.2/
http://ftp.gnu.org/gnu/binutils/
http://sourceware.org/newlib/
The "W" in "MingW" stands for Windows, so no you can not use that.
Check out this page on how to make a GCC version that cross compile to SPARC.
Build it yourself from sources or download prebuilt binaries, for example from here or from here.
You should be able to use one of the cross compilers from emdebian.
If you get strange errors about missing includes, then you stumbled over a version where the dependency on the kernel headers is missing, in this case you need to manually install linux-libc-dev-sparc-cross as well.
One of the simplest ways of obtaining a working cross compiler for Sparc V8 is to use Buildroot. Here's a small tutorial on installing the cross compiler and testing the executables on Qemu emulator.

how to use llvm+clang to compile for stm32

Has someone infos how to build a llvm+clang toolchain using binutils and newlib and how to use it?
host: Linux, AMD64
target: cortex-m3, stm32
c-lib: newlib
assembler: gnu as
I created a firmware framework - PolyMCU https://github.com/labapart/polymcu - that is based on CMake that support GCC and LLVM. Because it is based on CMake you can build your firmware on Linux/Windows/MacOS.
It also uses Newlib - it looks all your requirements are there!
I also wrote a blog where I compared GCC and LLVM build size on ARM Cortex-M: http://labapart.com/blogs/3-the-importance-of-the-toolchain-version-in-embedded-space
Interesting results, Clang generated code is not much bigger than GCC on Cortex-M...
Unfortunately, right now clang does not support flexible cross-compilation settings. So, most probably you will need to invoke necessary tools with all necessary arguments.
Start with building llvm + clang using --target=thumbv7-eabi configure argument (note that you will need llvm + clang as of yesterday for this). You might want to specify --enable-targets=arm as well. This will instruct clang to generate code for thumb by default. After this you can invoke clang -mcpu=cortex-m3 to generate the code for you.
You will have to provide all necessary include / library paths by hands via -I / -L, etc.
If you're happy with some C++ hacking, you can write necessary "HostInfo", so it will invoke the right tools and provide right paths automagically.

Resources