0

I followed this guide to build a cross compiler for Raspberry Pi ARMv7. It works fine (compiles ok hello world) but the problem is I can't use Raspberry Pi libraries such as wiringpi, sqlite etc.

This is my Makefile.

CC = /opt/cross-pi-gcc/bin/arm-linux-gnueabihf-g++
QUOTE := "
CFLAGS = -g -lwiringPi -pthread -lpthread
PROGRAM = lora
OBJDIR = obj

CPP_SRCS += \
src/main.cpp \
src/radio/sx1276/sx1276.cpp \
src/radio/radio.cpp \

OBJ_FILES += \
$(OBJDIR)/main.o \
$(OBJDIR)/sx1276.o \
$(OBJDIR)/radio.o \


all: make_dir $(OBJ_FILES)
    $(CC) $(OBJ_FILES) $(CFLAGS) -o $(PROGRAM)  

make_dir:
    mkdir -p $(OBJDIR)

$(OBJDIR)/main.o: src/main.cpp
    $(CC) $(CFLAGS) -c -o $@ $<

$(OBJDIR)/service.o: src/service/service.cpp
    $(CC) $(CFLAGS) -c -o $@ $<

$(OBJDIR)/sx1276.o: src/radio/sx1276/sx1276.cpp
    $(CC) $(CFLAGS) -c -o $@ $<

$(OBJDIR)/radio.o: src/radio/radio.cpp 
    $(CC) $(CFLAGS) -c -o $@ $<

clean:
    rm $(PROGRAM)
    rm -rf $(OBJDIR)

The error I'm getting:

/opt/cross-pi-gcc/lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lwiringPi

I copied some libraries from my Pi into a folder on my home dir with this command.

rsync -vR --progress -rl --delete-after --safe-links pi@192.168.1.PI:/{lib,usr,opt/vc/lib} $HOME/raspberrypi/rootfs

Which I found it here.

But I'm not sure how to link them. Did I overlook something?

Note: The piece of code I'm trying to compile was compiling fine on Windows's cross-compiler. I'm trying to setup a tool-chain on my Linux desktop. I'm using Manjaro.

MrBit
  • 290
  • 4
  • 20
  • 1
    You could replace all those compile rules for individual files with a single pattern rule, together with your CPP_SRCS variable. Normally you preserve the directory structure, e.g. src/service/service.cpp -> obj/service/service.o and this will help you generate OBJ_FILES automatically from CPP_SRCS and have a generic rule. – Ben Voigt Apr 17 '20 at 17:25

1 Answers1

1

You have to tell the linker where to look for the libraries.
You can add the flag -L$HOME/raspberrypi/rootfs/usr/lib, or wherever libwiringPi.so is located.

You have to point the compiler to the right headers as well. For that you use the -I flag. E.g. -I$HOME/raspberrypi/rootfs/usr/include. This goes in the CFLAGS of the targets that include wiringPi.h.

Also, -lwiringPi -pthread -lpthread don't belong in the CFLAGS, they are linker flags. You only need them in the target that builds PROGRAM. You could add a separate variable LDFLAGS, for example.

tttapa
  • 1,397
  • 12
  • 26
  • I added the full path of -L/home/name/raspberrypi/rootfs at the end of the CFLAGS. I still get the same error – MrBit Apr 17 '20 at 17:17
  • @MrBit you have to use the full path to where libwiringPi.so is located. – tttapa Apr 17 '20 at 17:20
  • I replaced `-lwiringPi` `-pthread` `-lpthread` with the `-L$HOME/raspberrypi/rootfs` path but I'm getting `undefined reference to` the functions – MrBit Apr 17 '20 at 17:21
  • 2
    @MrBit don't replace it, add the `-L` flag. Keep the other flags. – tttapa Apr 17 '20 at 17:22
  • I added the full path `-L/home/username/raspberrypi/rootfs/usr/lib/` at the end, it gives me `fatal error: wiringPi.h: No such file or directory` – MrBit Apr 17 '20 at 17:26
  • You have to do the same for the include paths. See my edit. – tttapa Apr 17 '20 at 17:29
  • 2
    @MrBit: Are you adjusting the link rule or the compile rules? The linker doesn't need a header file and the compiler doesn't need libraries! – Ben Voigt Apr 17 '20 at 17:29
  • 1
    @tttapa: Since he was completing the compile step before and failing on the link step, I don't think he needs to mess with the include path any further. – Ben Voigt Apr 17 '20 at 17:30
  • 1
    yes I had to add the `-I` flag. Also, from what you said my Makefile is a little messy. I'll see if I can do something better. Thank you very much for your help! – MrBit Apr 17 '20 at 17:33