Cross Compillation for Raspberry PI - c

I am trying to cross-compile odas library for Raspberry PI. The code actually builds out of the box on the platform (either Raspberry PI, or Ubuntu box). However, when I am trying to cross compile the same code using this tool chain file (the whole thing started in the previous question):
# Cross-compilation system information
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR "arm")
# Specify the cross compiler
SET(CMAKE_C_COMPILER /home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/aarch64-rpi3-linux-gnu-gcc)
SET(CMAKE_CXX_COMPILER /home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/aarch64-rpi3-linux-gnu-g++)
# where is the target environment located
set(CMAKE_SYSROOT /home/raspberrypi/sysroot)
set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT})
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
# adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# search headers and libraries in the target environment
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
I am getting a bunch of errors, like
[1/2] Linking C executable bin/odasserver
FAILED: bin/odasserver
: && /home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/aarch64-rpi3-linux-gnu-gcc --sysroot=/home/raspberrypi/sysroot -g --sysroot=/home/raspberrypi/sysroot CMakeFiles/odasserver.dir/demo/odasserver/main.obj -o bin/odasserver lib/libodas.so -lfftw3f -lasound -lconfig -lpulse-simple -lpulse -lm -lpthread && :
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `__pthread_create'
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `_dl_wait_lookup_done'
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `_dl_init_static_tls'
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `__pointer_chk_guard_local'
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `_dl_stack_flags'
/home/raspberrypi/opt/x-tools/aarch64-rpi3-linux-gnu/bin/../lib/gcc/aarch64-rpi3-linux-gnu/12.2.0/../../../../aarch64-rpi3-linux-gnu/bin/ld.bfd: lib/libodas.so: undefined reference to `_dl_pagesize'
collect2: error: ld returned 1 exit status
What could be possibly wrong here?

long story short - when creating sysroot folder on the host machine, I was using commands like
sudo rsync -avz rpi#192.168.1.205:/lib .
sudo rsync -avz rpi#192.168.1.205:/usr/include usr
sudo rsync -avz rpi#192.168.1.205:/usr/lib usr
sudo rsync -avz rpi#192.168.1.205:/usr/local usr
Apparently, on the target, some symbolic links were pointing outside of the current directory, for example
/usr/lib/aarch64-linux-gnu/libz.so -> /lib/aarch64-linux-gnu/libz.so.1.2.11
So needed to replace absolute symlinks with relative ones.
Once this was done, then everything was fine and compiling, and running.

Related

glib in cross compiling for raspberry pi c

I'm trying to compile a program with glib. I set up all the include paths so compilation goes through without errors but linker can't find glib.
Invoking: Cross GCC Linker
arm-linux-gnueabihf-gcc -L/home/ja/raspberrypi/rootfs/usr/local/lib -L/home/ja/raspberrypi/rootfs/lib/arm-linux-gnueabihf -o "daemon_test" ./src/gpio/gpio.o ./src/can/can_stuff.o ./src/can/libsocketcan.o ./src/daemon_test.o -lglib-2.0 -lwiringPi
/home/ja/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../lib/gcc/arm-linux-gnueabihf/4.8.3/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lglib-2.0
makefile:32: recipe for target 'daemon_test' failed
collect2: error: ld returned 1 exit status
make: *** [daemon_test] Error 1
I have a cross compiler set up for my raspberry pi. it's composed of eclipse and a gcc compiler running on debian.
it seems obvious that the linker can't find the proper file.
On my target raspberry pi I've installed libglib2.0-dev and a test program builds fine. So I used rsync to update my cross compiler's rootfs
rsync -rl --delete-after --safe-links pi#192.168.2.160:/{lib,usr,opt} $HOME/raspberrypi/rootfs
my include paths are set to the same values as come out from pkg-config on my rpi(adjusted for rootfs location)
pkg-config --libs --cflags glib-2.0
as I was writing this I did a search for "libglib" on both rpi and my cross compiler. there's a libglib-2.0.so on my rpi, but in my crosscompiler it's a libglib-2.0.so.0, they are both same size.
I removed the ".0" and got a new error
warning: libpcre.so.3, needed by /home/ja/raspberrypi/rootfs/lib/arm-linux-gnueabihf/libglib-2.0.so, not found (try using -rpath or -rpath-link)
and a bunch of undefined refferences. the thing is, that file exists in the same location as libglib-2.0.so(renamed form libglib-2.0.so.0)
-L/home/ja/raspberrypi/rootfs/lib/arm-linux-gnueabihf
anyone has any ideas what to do? I spent my whole day on this..

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

I generated a project by the STMCubeMX and wanted to import this project into Clion.
Here's the CmakeList.txt:
project(ClionTest)
cmake_minimum_required(VERSION 3.8)
add_definitions(-DSTM32F4xx)
file(GLOB_RECURSE USER_SOURCES "Src/*.c")
file(GLOB_RECURSE HAL_SOURCES "Drivers/STM32F4xx_HAL_Driver/Src/*.c")
add_library(CMSIS
startup/startup_stm32f407xx.s)
include_directories(Drivers/CMSIS/Device)
include_directories(Drivers/CMSIS/Device/ST/STM32F4xx/Include)
include_directories(Drivers/CMSIS/Include)
include_directories(Drivers/STM32F4xx_HAL_Driver)
include_directories(Drivers/STM32F4xx_HAL_Driver/Inc)
include_directories(Inc)
include_directories(Middlewares/Third_Party/LwIP/src/include)
add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${HAL_SOURCES} ${LINKER_SCRIPT})
target_link_libraries(${PROJECT_NAME}.elf CMSIS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.map")
set(HEX_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.bin)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMENT "Building ${HEX_FILE} \nBuilding ${BIN_FILE}")
And a STM32F4xx.cmake file:
INCLUDE(CMakeForceCompiler)
SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_VERSION 1)
# specify the cross compiler
#CMAKE_FORCE_C_COMPILER(C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q2-update/bin/arm-none-eabi-gcc.exe GNU)
#CMAKE_FORCE_CXX_COMPILER(D:/Lib/arm/bin/arm-none-eabi-g++.exe GNU)
SET(CMAKE_C_COMPILER "C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q2-update/bin/arm-none-eabi-gcc.exe")
SET(CMAKE_CXX_COMPILER "C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q2-update/bin/arm-none-eabi-g++.exe")
SET(LINKER_SCRIPT ${PROJECT_SOURCE_DIR}/STM32F407VETx_FLASH.ld)
#Uncomment for software floating point
#SET(COMMON_FLAGS "-mcpu=cortex-m4 -mthumb -mthumb-interwork -mfloat-abi=soft -ffunction-sections -fdata-sections -g -fno-common -fmessage-length=0")
SET(COMMON_FLAGS "-mcpu=cortex-m4 -mthumb -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16 -ffunction-sections -fdata-sections -g -fno-common -fmessage-length=0")
SET(CMAKE_CXX_FLAGS_INIT "${COMMON_FLAGS} -std=c++11")
SET(CMAKE_C_FLAGS_INIT "${COMMON_FLAGS} -std=gnu99")
SET(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,-gc-sections,-M=binary.map -T ${LINKER_SCRIPT}")
I set LINKER_SCRIPT ${PROJECT_SOURCE_DIR}/STM32F407VETx_FLASH.ld
File structure
And the Cmake options is:
Cmake options
When I reload this project, it's not go on well.
c:/progra~2/gnutoo~1/62017-~1/bin/../lib/gcc/arm-none-eabi/6.3.1/../../../../arm-none-eabi/bin/ld.exe: cannot open linker script file
D:/Project/ClionTest/cmake-build-default/CMakeFiles/CMakeTmp/STM32F407VETx_FLASH.ld:
Invalid argument
collect2.exe: error: ld returned 1 exit status
CMakeFiles\cmTC_bf7b4.dir\build.make:96: recipe for target 'cmTC_bf7b4' failed
mingw32-make.exe[1]: *** [cmTC_bf7b4] Error 1
mingw32-make.exe[1]: Leaving directory
'D:/Project/ClionTest/cmake-build-default/CMakeFiles/CMakeTmp'
Makefile:125: recipe for target 'cmTC_bf7b4/fast' failed
mingw32-make.exe: *** [cmTC_bf7b4/fast] Error 2
It seems like the path of ld.exe is not correct,how shoulld I set its path?
And why the path of STM32F407VETx_FLASH.ld it searched is not the path I set?
I'm using Linux, but I've had the a similar issue in regards to being unable to open the linker script.
cannot open linker script file D:/Project/ClionTest/cmake-build-default/CMakeFiles/CMakeTmp/STM32F407VETx_FLASH.ld
After looking for a while at the documentation for target_link_option (which does approximately the same kind of thing as setting a variable directly), I took note of the SHELL: directive, that's stated to:
The set of options is de-duplicated to avoid repetition. While beneficial for individual options, the de-duplication step can break up option groups. For example, -D A -D B becomes -D A B. One may specify a group of options using shell-like quoting along with a SHELL: prefix. The SHELL: prefix is dropped and the rest of the option string is parsed using the separate_arguments() UNIX_COMMAND mode.
By adding it to my command, I managed to get compilation working normally. The result was the following:
set(LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/STM32F303CCTX_FLASH.ld")
set(LINKER_FLAGS "SHELL:-T${LINKER_SCRIPT} -Wl,--gc-sections --specs=nano.specs --specs=nosys.specs")
target_link_options(${PROJECT_BINARY} PRIVATE ${LINKER_FLAGS})
Change this:
SET(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,-gc-sections,-M=binary.map -T ${LINKER_SCRIPT}")
To:
SET(CMAKE_EXE_LINKER_FLAGS "-Wl,-gc-sections,-M=binary.map -T${LINKER_SCRIPT}")

arm-gcc unresolved reference 'sinf'

I'm getting this error when trying to compile:
error: undefined reference to `sinf'
I have included math.h, and verified that it is defined in there:
#include <math.h>
However, I get an error while trying to link to the math library:
arm-none-eabi-ld -L/usr/lib -lm --gc-sections -T ../standalone.ld -o "main.elf" ./main.o ./startup_gcc.o
error: cannot find -lm
However, the library is obviously there:
Kens-MacBook-Pro:lib Ken$ pwd
/usr/lib
Kens-MacBook-Pro:lib Ken$ ls | grep libm
libm.dylib
libmecab.1.0.0.dylib
libmecab.dylib
libmecabra.dylib
libmenu.5.4.dylib
libmenu.dylib
libmx.A.dylib
libmx.dylib
Kens-MacBook-Pro:lib Ken$
What am I doing wrong? I'm using Eclipse.
Judging by your arm-none-eabi-ld command, I'm assuming you're cross compiling. You need to install some sort of math library into the toolchain for whatever your platform is.
The ls output you posted contains a list of libraries available for your host, not your target. On my machine, the libraries for my ARM cross-compiler resides in /opt/local/arm-none-eabi/lib/ for example.
danieltang ~$ ls /opt/local/arm-none-eabi/lib/
crt0.o ldscripts libm.a libssp_nonshared.a libstdc++.a-gdb.py libsupc++.la
elf2flt.ld libc.a libssp.a libssp_nonshared.la libstdc++.la thumb
fpu libg.a libssp.la libstdc++.a libsupc++.a

gromacs compilation gives undefined reference error

i would like to use gromacs on my open suse 12.3 platform but am having trouble with it.
when trying to compile an analyzing tool using gmx_template i first got this error:
g++ -L/usr/local/gromacs/lib -o msd msd.o -lmd -lgmx -lfftw3f -lxml2 -lnsl -lm
/usr/lib64/gcc/x86_64-suse-linux/4.7/../../../../x86_64-suse-linux/bin/ld: /usr/local /gromacs/lib/libgmx.a(pthreads.c.o): undefined reference to symbol 'pthread_getaffinity_np##GLIBC_2.3.4'
/usr/lib64/gcc/x86_64-suse-linux/4.7/../../../../x86_64-suse-linux/bin/ld: note: 'pthread_getaffinity_np##GLIBC_2.3.4' is defined in DSO /lib64/libpthread.so.0 so try adding it to the linker command line
/lib64/libpthread.so.0: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make: *** [msd] Fehler 1
then i added
/lib64/libpthread.so.0 to the -L options in the makefile
but now i get a lot of errors like this:
/usr/local/gromacs/lib/libgmx.a(pbc.c.o): In function `put_atoms_in_box_omp._omp_fn.0':
pbc.c:(.text+0x862f): undefined reference to `omp_get_num_threads'
i think they are all related to openmp. i do not understand enough of the building process
to enable openmp support (probably -fopenmp) and am at the same time surprised in case
i would have to change the cmake files in order to make gromacs work. i used the quick and dirty install following the gromacs installation instructions on their website.
any suggestions what i can do / did wrong so far ?
i am using gcc 4.7
Here is my gromacs-2018.4 installation process.
pre-installed packages
gcc-8.3.0
cmake-3.14.1
fftw-3.3.8
./configure --prefix=/histor/kang/yangpc/soft/lib/packages/fftw-3.3.8/single_nompi/ --enable-sse2 --enable-avx --enable-float --enable-shared
install gromacs
export PATH=/histor/kang/yangpc//soft/09.system/cmake-3.14.1-Linux-x86_64/bin/:/histor/kang/yangpc//soft/lib/packages/gcc-8.3.0/bin/:$PATH
export CMAKE_PREFIX_PATH="/histor/kang/yangpc/soft/lib/packages/fftw-3.3.8/single_nompi/:/histor/kang/yangpc//soft/lib/packages/gcc-8.3.0/"
export LD_LIBRARY_PATH="/histor/kang/yangpc/soft/lib/packages/gcc-8.3.0/lib64/:$LD_LIBRARY_PATH"
unset LIBRARY_PATH CPATH C_INCLUDE_PATH PKG_CONFIG_PATH CPLUS_INCLUDE_PATH INCLUD
export LD_LIBRARY_PATH=/histor/kang/yangpc/soft/lib/packages/gmp-5.1.3/lib/:/histor/kang/yangpc/soft/lib/packages/mpc-1.0.1/lib/:/histor/kang/yangpc/soft/lib/packages/mpfr-4.0.1/lib/:$LD_LIBRARY_PATH
~/soft/09.system/cmake-3.14.1-Linux-x86_64/bin/cmake .. -DCMAKE_INSTALL_PREFIX=/histor/kang/yangpc/soft/13.DrugDesign/GROMACS/gromacs-2018.4/InstallPos/ -DCMAKE_C_COMPILER="/histor/kang/yangpc/soft/lib/packages/gcc-8.3.0/bin/gcc" -DCMAKE_CXX_COMPILER="/histor/kang/yangpc/soft/lib/packages/gcc-8.3.0/bin/g++"
make
make install

Getting error on compiling newlib

I am trying to build a arm toolchain from scratch.
I am referring to this site.
http://www.nixit.co.uk/cortex-m3-mac-1
Followed all the instructions carefully. Everything went well till newlib compilation. I am getting strange error.
/bin/bash: arm-none-eabi-cc: command not found
So I checked the Makefile
there is a line in makefile. There is a directive related to arm-none-eabi-cc
# -----------------------------------------------
# Programs producing files for the TARGET machine
# -----------------------------------------------
AR_FOR_TARGET=arm-none-eabi-ar
AS_FOR_TARGET=arm-none-eabi-as
CC_FOR_TARGET=$(STAGE_CC_WRAPPER) arm-none-eabi-cc
# If GCC_FOR_TARGET is not overriden on the command line, then this
# variable is passed down to the gcc Makefile, where it is used to
# build libgcc2.a. We define it here so that it can itself be
# overridden on the command line.
GCC_FOR_TARGET=$(STAGE_CC_WRAPPER) arm-none-eabi-gcc
I checked the /usr/arm/bin directory. So i do find arm-none-eabi tools, but not arm-none-eabi-cc
arm-none-eabi-ld arm-none-eabi-readelf
arm-none-eabi-ar arm-none-eabi-gcc-4.4.3 arm-none-eabi-nm arm-none-eabi-size
arm-none-eabi-as **arm-none-eabi-gcc**
Please help
Try replacing arm-none-eabi-cc in the Makefile with arm-none-eabi-gcc, or symlinking arm-none-eabi-cc to arm-none-eabi-gcc.

Resources