satyaGolladi
satyaGolladi

Reputation: 192

Unable to cross-compile for mips

I tried to cross compile the simple hello.c using mips cross compiler which throws me error.

Below is the error

satya@satya-dev:~/Test/kernel/check$ make ARCH=mips CROSS_COMPILE=mips-buildroot-linux-uclib-
make -C /opt/toolchains/crosstools-mips-gcc-5.3-linux-4.1-uclibc-1.0.12-binutils-2.25-NPTL/usr/bin/ M=/home/satya/Test/kernel/check modules
make[1]: Entering directory '/opt/toolchains/crosstools-mips-gcc-5.3-linux-4.1-uclibc-1.0.12-binutils-2.25-NPTL/usr/bin'
make[1]: *** No rule to make target 'modules'.  Stop.
make[1]: Leaving directory '/opt/toolchains/crosstools-mips-gcc-5.3-linux-4.1-uclibc-1.0.12-binutils-2.25-NPTL/usr/bin'
Makefile:7: recipe for target 'all' failed
make: *** [all] Error 2

My Makefile is:

obj-m := hello.o
#KDIR := /lib/modules/$(shell uname -r)/build/
KDIR := /opt/toolchains/crosstools-mips-gcc-5.3-linux-4.1-uclibc-1.0.12-binutils-2.25-NPTL/usr/bin/
#PWD = $(shell pwd)

all:
        $(MAKE) -C $(KDIR) M=$(shell pwd) modules
clean:
        $(MAKE) -C $(KDIR) M=$(shell pwd) clean

I added the compiler path to /bin also

satya@satya-dev:~/Test/kernel/check$ echo $PATH
/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/sbin:/opt/toolchains/crosstools-mips-gcc-5.3-linux-4.1-uclibc-1.0.12-binutils-2.25-NPTL:/opt/toolchains/crosstools-mips-gcc-5.3-linux-4.1-uclibc-1.0.12-binutils-2.25-NPTL/usr/bin

Even compiler works fine without makefile

satya@satya-dev:~/Test/kernel/check$ /opt/toolchains/crosstools-mips-gcc-5.3-linux-4.1-uclibc-1.0.12-binutils-2.25-NPTL/usr/bin/mips-buildroot-linux-uclibc-gcc in.c -o temp
satya@satya-dev:~/Test/kernel/check$ ll
total 24
-rw-rw-r-- 1 satya satya 1135 Aug 10 05:06 hello.c
-rw-rw-r-- 1 satya satya    0 Aug 10 06:29 Module.symvers
-rw-rw-r-- 1 satya satya   52 Aug 10 07:48 modules.order
-rw-rw-r-- 1 satya satya   66 Aug 10 07:59 in.c
-rw-rw-r-- 1 satya satya  281 Aug 10 08:30 Makefile
-rwxrwxr-x 1 satya satya 5604 Aug 10 09:37 temp

I know I am doing some vague mistake, but I couldn't figure it out. Can anyone tell me where am I going wrong?

Upvotes: 0

Views: 2385

Answers (1)

John Bollinger
John Bollinger

Reputation: 180201

I know I am doing some vague mistake, but I couldn't figure it out. Can anyone tell me where am I going wrong?

No, it's no "vague mistake". Your Makefile has valid form, but the contents are pretty much non-sensical.

In your command-line make invocation, you do not name a target, so make chooses the default (first) one in your Makefile, 'all'. This is probably what you want, as far as it goes.

The recipe for target 'all' contains only one command:

       $(MAKE) -C $(KDIR) M=$(shell pwd) modules

This runs a sub-make in directory $(KDIR), which is surprising because it does not look like the value set for KDIR is part of the project you are trying to build.

The sub-make is asked to build a target named 'modules', and the error message indicates that make does not have any rule for how to do so. Probably it does not see a makefile (under any of the names it recognizes) in the build directory, but if it does see one, then it does not contain a rule for that target. I note that the Makefile you present has no rule for such a target, either, so I have no idea where that comes from.

Even compiler works fine without makefile

Well that's good news, at least.

The key to cross-compiling is to use the correct toolchain for the purpose. You can do this in several ways, but most of them start with defining some standard make variables appropriately. For example:

tooldir = /opt/toolchains/crosstools-mips-gcc-5.3-linux-4.1-uclibc-1.0.12-binutils-2.25-NPTL/usr/bin
CC = $(tooldir)/mips-buildroot-linux-uclibc-gcc
LD = $(CC)

The CC variable names the C compiler, and the LD variable names the linker. There are other variables you might want or need to set, too, such as CFLAGS for any C compiler flags you want to use, and LDFLAGS for any linker flags.

There are several ways you could go from there, but the very simplest would be to name the default target "hello" instead of "all":

hello:

And that's probably all you need to build the "hello" program specifically.

Upvotes: 2

Related Questions