How to cross compile GSL library for arm-none-eabi-gcc? - arm

I need to use the GSL library in my program on LPCXpresso 4367(ARM CORTEX M4). I tried to follow the library linking procedure for LPC xpresso but the MCU linker is giving me these errors:
MCUXpressoIDE_10.3.0_2200\workspace\test1\Debug/../src/test1.c:53: undefined reference to 'gsl_linalg_LU_decomp'
MCUXpressoIDE_10.3.0_2200\workspace\test1\Debug/../src/test1.c:56: undefined reference to 'gsl_matrix_alloc'
MCUXpressoIDE_10.3.0_2200\workspace\test1\Debug/../src/test1.c:57: undefined reference to 'gsl_linalg_LU_invert'
and so on for other functions as well.
I have the libgsl.a and libgslcblas.a precompiled libraries for windows which works perfectly on codeblocks on windows with GCC compiler.
I read that I need to crosscompile library for the arm-none-eabi-gcc toolchain. But can someone please provide me the procedure as well?

the libgsl.a and libgslcblas.a precompiled libraries for windows
Those won't do for ARM.
In order to work on another platform, these libs need to be compiled from source code with the proper compiler (and settings - Cortex-M4F requires Thumb2 instruction set).

As the libraries are precompiled for Windows they don't work for ARM (as it is said in the other answer)
You need to cross compile the libraries first. If you install the GSL libraries following this procedure, you only need to change the parameters in the ./config according to your platform, for example I used:
./config --host=arm-linux-gnueabihf --prefix=/home/yourname/gsl_arm
Inside the .zip file with the gsl-2.5 files, there is a file called INSTALL. There you can find more details on the options for cross compiling.
Make sure to make clean before if you have already compiled the library for different settings. After cross-compiling the library when you run make check on the terminal you will probably get errors, but still it works. Continue with make install and you are ready to use it.

Related

Static build of GMP for MSVC (Windows)

Is it possible to build GMP for MSVC on Windows?
I need fully static solution (static library), without any DLL dependencies. So that my final EXE doesn't depend on any external (non-system) DLLs.
I'm alright if building GMP will need Cygwin or MSYS, as far as it can be used later in MSVC without any problems. But as far as I know at least Cygwin builds always depend on extra DLLs like cygwin1.dll which is not affordable for me, fully static-library solution is needed.
I'm aware there exists MPIR library that is more Windows friendly. But right now I need specifically GMP solution if possible.
Of course would be great if all optimizations and assembly is used when building for Windows. But if assembly is not possible then at least non-assembly (generic) variant of GMP is needed.
Of course I need 64-bit version.
If someone can post all steps needed to produce such static library for MSVC usage? Or maybe link some web-site that has such instructions?
I successfully managed to compile a working fully statically linked program with GMP using MSVC under Windows.
For that I used installation of MSYS, which is located in c:/bin/msys/ on my machine.
Then inside MSYS shell installed GMP packages mingw-w64-clang-x86_64-gmp and gmp-devel (pacman -S gmp-devel to install and pacman -Ss gmp to search).
In MSVC compiler I added include directory c:/bin/msys/clang64/include/.
Wrote an example of GMP usage program in C++, that implements Trial Division / Pollard's Rho / Pollard's P-1 factoring algorithms using long arithmetics. This program uses both mpz_...() C routines and mpz_class C++ wrapper class. For example this program is located in main.cpp.
To linker command line I added following libraries:
c:/bin/msys/clang64/lib/libgmp.a
c:/bin/msys/clang64/lib/libgmpxx.a
c:/bin/msys/mingw64/lib/gcc/x86_64-w64-mingw32/10.3.0/libgcc.a
c:/bin/msys/clang64/x86_64-w64-mingw32/lib/libmingwex.a
Also I had to add /FORCE flag (read about it here) to linker command, because libmingwex.a has some symbols overlapping with default MSVC's libraries, precisely without /FORCE I had following errors:
libucrt.lib(strnlen.obj) : error LNK2005: wcsnlen already defined in libmingwex.a(lib64_libmingwex_a-wcsnlen.o)
libucrt.lib(strnlen.obj) : error LNK2005: strnlen already defined in libmingwex.a(lib64_libmingwex_a-strnlen.o)
bin\win-msvc-m-64-release\drafts\gmp_int_msvc.exe : fatal error LNK1169: one or more multiply defined symbols found
All steps produced working (tested) final statically-linked program without any external DLL dependencies (of course except for default system DLLs of Windows).
It means MSYS's libraries .a are fully compatible with MSVC and link successfully in MSVC compilation.
Not to have /FORCE linker flag I also did extra following steps. Made a copy of c:/bin/msys/clang64/x86_64-w64-mingw32/lib/libmingwex.a library. Used c:/bin/msys/clang64/bin/objcopy.exe util, which probably was installed together with Clang. With objcopy renamed overlapping symbols:
objcopy --redefine-sym wcsnlen=msys_wcsnlen libmingwex.a
objcopy --redefine-sym strnlen=msys_strnlen libmingwex.a
which allowed me to successfully use this modified libmingwex.a library to link in MSVC without using /FORCE.

Creating a library for openwrt environment

I am working on a project where in I create a C library(compiled through Makefile), then I write another C program (that uses the above C library ) and compile. I did all the above in a linux x86_64 system and it worked. Unfortunately I had to do all these things in a device that uses openwrt environment.Having not been too familiar with openwrt, what I did is placed the library that i created in linux, in openwrt's lib folder and tried to compile it, but because the architecture of linux of openwrt is not same, it threw the following error
could not read symbols: File format not recognized
Now, my question is. How can i create the above library for openwrt environment. Do I need to use a makefile, a cross compiler(if yes, which one) or use some other option?
From the wikipedia page of OpenWrt (https://en.wikipedia.org/wiki/OpenWrt):
The OpenWrt build system ...
Provides an integrated cross-compiler toolchain (gcc, ld, uClibc etc.)
so yes, you need to cross compile, please follow the instructions at:
https://wiki.openwrt.org/doc/devel/crosscompile
However, You will need to know the architecture of your OpenWrt box ...

How to work with external libraries when cross compiling?

I am writing some code for raspberry pi ARM target on x86 ubuntu machine. I am using the gcc-linaro-armhf toolchain. I am able to cross compile and run some independent programs on pi. Now, I want to link my code with external library such as ncurses. How can I achieve this.
Should I just link my program with the existing ncurses lib on host machine and then run on ARM? (I don't think this will work)
Do I need to get source or prebuilt version of lib for arm, put it in my lib path and then compile?
What is the best practice in this kind of situation?
I also want to know how it works for the c stdlib. In my program I used the stdio functions and it worked after cross compiling without doing anything special. I just provided path for my arm gcc in makefile. So, I want to know, how it got correct std headers and libs?
Regarding your general questions:
Why the C library works:
The C library is part of your cross toolchain. That's why the headers are found and the program correctly links and runs. This is also true for some other very basic system libraries like libm and libstdc++ (not in every case, depends on the toolchain configuration).
In general when dealing with cross-development you need some way to get your desired libraries cross-compiled. Using binaries in this case is very rare. That is, especially with ARM hardware, because there are so many different configurations and often everything is stripped down much in different ways. That's why binaries are not very much binary compatible between different devices and Linux configurations.
If you're running Ubuntu on the Raspberry Pi then there is a chance that you may find a suitable ncurses library on the internet or even in some Ubuntu apt repository. The typical way, however, will be to cross compile the library with the specific toolchain you have got.
In cases when a lot and complex libraries need to be cross-compiled there are solutions that make life a bit easier like buildroot or ptxdist. These programs build complete Linux kernels and root file systems for embedded devices.
In your case, however, as long as you only want ncurses you can compile the source code yourself. You just need to download the sources, run configure while specifying your toolchain using the --host option. The --prefix option will choose the installation directory. After running make and make install, considering everything went fine, you will have got a set of headers and the ARM-compiled library for your application to link against.
Regarding cross compilation you will surely find loads of information on the internet and maybe ncurses has got some pointers in its shipped documentation, too.
For the query How the C library works in cross-tools
When compiling and building cross-tool chain during configuration they will provide sysroot.
like --with-sysroot=${CLFS_CROSS_TOOLS}
--with-sysroot
--with-sysroot=dir
Tells GCC to consider dir as the root of a tree that contains (a subset of) the root filesystem of the target operating system. Target system headers, libraries and run-time object files will be searched for in there. More specifically, this acts as if --sysroot=dir was added to the default options of the built compiler. The specified directory is not copied into the install tree, unlike the options --with-headers and --with-libs that this option obsoletes. The default value, in case --with-sysroot is not given an argument, is ${gcc_tooldir}/sys-root. If the specified directory is a subdirectory of ${exec_prefix}, then it will be found relative to the GCC binaries if the installation tree is moved.
So instead of looking /lib /usr/include it will look /Toolchain/(libc) and (include files) when its compiling
you can check by
arm-linux-gnueabihf-gcc -print-sysroot
this show where to look for libc .
also
arm-linux-gnueabihf-gcc -print-search-dirs
gives you clear picture
Clearly, you will need an ncurses compiled for the ARM that you are targeting - the one on the host will do you absolutely no good at all [unless your host has an ARM processor - but you said x86, so clearly not the case].
There MAY be some prebuilt libraries available, but I suspect it's more work to find one (that works and matches your specific conditions) than to build the library yourself from sources - it shouldn't be that hard, and I expect ncurses doesn't take that many minutes to build.
As to your first question, if you intend to use ncurses library with your cross-compiler toolchain, you'll have its arm-built binaries prepared.
Your second question is how it works with std libs, well it's really NOT the system libc/libm the toolchain is using to compile/link your program is. Maybe you'll see it from --print-file-name= option of your compiler:
arm-none-linux-gnuabi-gcc --print-file-name=libm.a
...(my working folder)/arm-2011.03(arm-toolchain folder)/bin/../arm-none-linux-gnuabi/libc/usr/lib/libm.a
arm-none-linux-gnuabi-gcc --print-file-name=libpthread.so
...(my working folder)/arm-2011.03(arm-toolchain folder)/bin/../arm-none-linux-gnuabi/libc/usr/lib/libpthread.so
I think your Raspberry toolchain might be the same. You can try this out.
Vinay's answer is pretty solid. Just a correction when compiling the ncurses library for raspberry pi the option to set your rootfs is --sysroot=<dir> and not --with-sysroot . Thats what I found when I was using the following compiler:
arm-linux-gnueabihf-gcc --version
arm-linux-gnueabihf-gcc (crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03) 4.8.3 20140303 (prerelease)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

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 do I install C packages on windows

I have to use LU decompostion to fit a simple model to some data (simulated) in C. An example of what I need to do is here:
However, I'm stuck with a more basic problem: how do I install packages in C and call them in my code?
I'm new in C and I'm used to R. But I have this assingment to do some tests about Matrix inversion, LU decomposision and the professor suggested using Lapack to easy things (thus, I don't need to code myself the LU decomposition etc.). But I don't know how to install the package and call it in my code, in order to use the functions of LAPACK.
I have a windows 7 64 bits and I'm using compiler Code Blocks 8.02
Thanks for any help.
Normally you don't "install" C libraries in that sense. Normally, in Windows you have three types of files. The header files, typically ending in .h, the dynamic library, .dll, and most likely some linker files (typically, .lib, .a or something). The linker and compiler will need to be able to find these files somewhere. Normally you set the include directory paths, and library directory paths.
E.g. Let's say you downloaded a library called foo, and you extract it to C:\foo.
In that folder, libfoo.a, foo.dll and foo.h reside. In Code::Blocks you will have to point include directory path to C:\foo and library path to C:\foo so that the linker and compiler know where to look for these files. Since you're linking against the foo library, you will also have to set -lfoo or something similiar in linker command line. This is GCC syntax, but I think Code::Blocks uses GCC compiler behind the scenes anyways.
In the C code you can just #include <foo.h> and the compiler will find it for you.
You need to install that library and it might actually supply a tool for that. Check their documentation (e.g. a file INSTALL or README in their distributed sources). If the library uses only headers you might only need to copy it's headers to some directory on your system, but their buildsystem might be able to do that for you.
Once that is done you would tell your IDE on where to look for the sources and if the library uses not just headers to link against the actual library file. See the documentation in the Code::Blocks Wiki on how this is done for some example cases and adapt for your library.
The simplest thing to do in your situation is to install Cygwin. You can use the setup.exe installer to install the GCC and the LAPACK libraries. When you want to use the LAPACK library, you will add the -llapack option to your GCC command line.

Resources