简体   繁体   中英

Adapt Makefile for cross-compilation

I have a makefile that works fine when I compile using /usr/bin/gcc to compile it. However I'm trying to compile it using a crosstool-ng compiler. I've changed CC to the cross-compilers location, and added a prefix to the directory that holds the compiler, but I get an error compiling.

The Makefile is here (sorry, it's long):

CFLAGS ?= -Wall -O0 -ggdb3
PREFIX = /home/me/crosstool-ng-1.18.0/x-tools/i586-system-linux-gnu/
CC = /home/me/crosstool-ng-1.18.0/x-tools/i586-system-linux-gnu/bin/i586-system-linux-gnu-gcc
ALL_CFLAGS = $(CFLAGS) -D_GNU_SOURCE

.phony: all
all:  food libfood.so.1.0.0 foo_query

.phony: tools
tool tools: libfood_print foo_print

.phony: install
install: libfood.so.1.0.0
    cp libfood.so.1.0.0 $(PREFIX)/lib
cd $(PREFIX)/lib ; \
  ln -sf libfood.so.1.0.0 libfood.so.1 ; \
  ln -sf libfood.so.1 libfood.so
cp libfood.h $(PREFIX)/include
cp foo_data.h $(PREFIX)/include
cp food $(PREFIX)/bin
cp foo_query $(PREFIX)/bin


%.o: %.c
$(CC) $(ALL_CFLAGS) -c $<

food: food.o foo.o
$(CC) $(ALL_CFLAGS) -o $@ $^ -lm -lpthread

libfood.so.1.0.0: libfood.o
$(CC) -shared -Wl,-soname,libfood.so.1 -o libfood.so.1.0.0 libfood.o

libfood_print: libfood_print.o
$(CC) $(ALL_CFLAGS) -o $@ $^ -lfood

foo_print: foo_print.o foo.o
$(CC) $(ALL_CFLAGS) -o $@ $^ -lm -lpthread

foo_query: foo_query.o
$(CC) $(ALL_CFLAGS) -o $@ $^ -lfood

food.o: food.c foo.h foo_data.h
foo.o: foo.c foo.h foo_data.h
foo_print.o: foo_print.c foo_data.h
foo_query.o: foo_query.c foo_data.h
libfood.o: libfood.c libfood.h
$(CC) $(ALL_CFLAGS) -fPIC -c $<

foo_print.o: foo_print.c foo.h

.phony:clean
clean:
rm -rf *.o *~ food libfood.so.1.0.0 foo_print libfood_print foo_query

The error message I'm getting says cannot find -lfood collect2: ld returned 1 exit status

If anyone could suggest a fix for this I'd be very grateful.

EDIT: My Solution:

I should probably have been clearer but this Makefile was being used to build a package that was included in buildroot. I tried the suggestion by Jonatan, but unfortunately I still got the same error. My workaround was to run buildroot using make -k , and then build again using make .

An easy way to solve this would be:

ALL_CFLAGS += -L$(PREFIX)/lib

If you really want to install your lib in the toolchain, you should look for the usr/lib directory, usually the path is TOOLCHAIN_DIR/TOOLCHAIN_PREFIX/sysroot/usr/lib

Check other binaries in the $(PREFIX)/lib directory, you will notice that they were compile to run in you host, and not in your target.

The files the compiler need to check dependencies, link, and execute in your target, are installed in the sysroot directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM