33

I'm trying to cross compile a helloworld kernel (2.6.x) module for ARM architecture on my intel x86 host.

The codesourcery tool chain for ARM is located at: /home/ravi/workspace/hawk/arm-2009q3

The kernel source is located at :/home/ravi/workspace/hawk/linux-omapl1

My Makefile:

ARCH=arm
CROSS_COMPILE=arm-none-linux-gnueabi
obj-m := Hello.o
KDIR := /home/ravi/workspace/hawk/linux-omapl1
PWD := $(shell pwd)
default:
          $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
          $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean

When i run make, the .ko produced is that of my host machine which means the makefile is invoking the native compiler instead of the cross compiler.What am I doing wrong? The cross compiler's binaries are in my path.

ysth
  • 96,171
  • 6
  • 121
  • 214
itisravi
  • 3,406
  • 3
  • 23
  • 30

6 Answers6

35

Putting ARCH and CROSS_COMPILE in the Makefile doesn't work. You need to put them on the command line:

make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
JayM
  • 4,798
  • 1
  • 21
  • 15
  • 3
    On a side note, can we compile a module without writing a makefile, just by using gcc switches? – itisravi Aug 13 '10 at 04:58
  • 2
    I'm sure you could, but it wouldn't be easy. Add `V=1` to your commandline when you call make to see how gcc is called. That's what you'd have to type in instead of `make ...` – JayM Aug 13 '10 at 15:33
  • 3
    As a somehow linux noob I wonder why ARCH and CROSS_COMPILE are in some Makefiles if they don't work that way. – Patric Dec 11 '13 at 20:43
  • Instead of command line, It can be exported also either from command line or form makefile – Jagdish Mar 14 '16 at 09:34
  • @itisravi have you tried to compile a module without writing a makefile. i.e just by using gcc – GShaik Aug 22 '16 at 06:45
  • @AbdulGafoor No, AFAIK, kbuild works only with makefiles. – itisravi Aug 22 '16 at 08:21
20

Replace

ARCH=arm
CROSS_COMPILE=arm-none-linux-gnueabi

by

export ARCH:=arm
export CROSS_COMPILE:=arm-none-linux-gnueabi-

this will also work if you do not want to give these parameter command line each time.

raj_gt1
  • 4,653
  • 2
  • 20
  • 28
7

Sidenote: SUBDIRS= is deprecated in favor of M=.

user502515
  • 4,346
  • 24
  • 20
5

could you try, you forgot to add ARCH and CROSS_COMPILE into the default and clean

ARCH=arm
COMPILER=arm-none-linux-gnueabi
obj-m := Hello.o
KERNELDIR := /home/ravi/workspace/hawk/linux-omapl1
PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(COMPILER) modules

clean:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=$(ARCH) clean
Albert Chen
  • 1,331
  • 1
  • 12
  • 13
1

adding export at the end of your Makefile variable declarations will make them available to subshells. and add the dash to the CROSS_COMPILE prefix as JayM pointed out, and M instead of SUBDIRS as user502515 answered.

and it's generally a good idea to use := rather than = in a Makefile, so the variable only gets interpolated once. really doesn't matter in this particular case though.

ARCH := arm
CROSS_COMPILE := arm-none-linux-gnueabi-
obj-m := Hello.o
KDIR := /home/ravi/workspace/hawk/linux-omapl1
PWD := $(shell pwd)
export
default:
          $(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
          $(MAKE) -C $(KDIR) M=$(PWD) clean
jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
-1
MODULES := hola_kern.o

#guest architecture
ARCH := arm

CROSS_COMPILE := arm-linux-gnueabi-
obj-m := $(MODULES)

#path of the arm compiled kernel
ROOTDIR := /home/aldo/c/proyectos/prefixa/work/kernels/linux-omap-5f0a6e2

MAKEARCH := $(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE)

all: modules
modules:
    $(MAKEARCH) -C $(ROOTDIR) M=${shell pwd} modules

clean:
    $(MAKEARCH) -C $(ROOTDIR) M=${shell pwd} clean
Bartosz Moczulski
  • 1,209
  • 9
  • 18
aldo
  • 11