Search code examples
gcccross-compilingstm32

GCC cross compilation result throws error if the linker is called separately


I'm trying to compile a simple hello-world program for the STM32MP157C-DK2 demoboard which runs the openst-linux distribution. I'm using a Ubuntu-VM and OpenEmbedded/Yocto for cross compilation. Everything works fine if I compile and link the program using only one line. But if I split the command into compilation and linking the resulting program does not work. I want to split the process because Ecplise calls the compiler and the linker separately.

I can compile the program successfully using this line:

arm-openstlinux_weston-linux-gnueabi-gcc -march=armv7ve -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=[path to sysroot] -o gtk_hello_world_manual src/main.c -O2 -pipe -g -feliminate-unused-debug-types -Wall -pthread -I[long list of include directories] -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

This results is a working gtk_hello_world_manual programm. I tried to split the process by using the same line, but with main.o as target instead of gtk_hello_world_manual. Then I linked the resulting main.o with this line:

arm-openstlinux_weston-linux-gnueabi-ld --sysroot=[path to sysroot] -o gtk_hello_world_manual -O1 --hash-style=gnu --as-needed -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 main.o

Both times the program is compiled and linked without errors. If I used the one-line compilation/linking, the program runs flawless on the target board.

If I use the split variant, the error-message

"error while loading shared libraries: type: cannot open shared object file: No such file or directory"

shows up and the program is terminated.

[Edit] Found a solution: You have to call the compiler with the -c option so that no linking process is done. This leads to the following command:

arm-openstlinux_weston-linux-gnueabi-gcc -march=armv7ve -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=[path to sysroot] -o main.o -c src/main.c -O2 -pipe -g -feliminate-unused-debug-types -Wall -pthread -I[long list of include directories]

And after that calling the linker indirectly with the gcc command:

arm-openstlinux_weston-linux-gnueabi-gcc -march=armv7ve -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=[path to sysroot] -O2 -pipe -g -feliminate-unused-debug-types -o gtk_helloworld_manual -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 main.o

This results in a working program file. With this it can be included in the ecplise makefile project and build and debug in the IDE.


Solution

  • Found a solution and added it to the original post. Made this answer so I can mark this as solved.