0

I am currently working on a simple Linux kernel module and have encountered an issue related to running make and sudo make commands. Here's the content of my Makefile:

obj-m := main.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

When I run make as a regular user, the compilation process works perfectly. However, when I try sudo make, the $(PWD) command seems to fail, resulting in an error. the pwd is an environment variable which exits even as in the shell as a root user and the compilation goes fine when running make as root

error output when running with sudo make :

make -C /lib/modules/`uname -r`/build M= modules
make[1]: Entering directory '/usr/src/linux-headers-6.2.0-39-generic'
make[2]: *** No rule to make target 'arch/x86/entry/syscalls/syscall_32.tbl', needed by 'arch/x86/include/generated/uapi/asm/unistd_32.h'.  Stop.
make[1]: *** [arch/x86/Makefile:248: archheaders] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-6.2.0-39-generic'
make: *** [Makefile:4: all] Error 2

$(PWD) command seems to fail, resulting in an error. I would appreciate if someone can please help understand or any insights that would be really helpful. Thank you

1 Answer 1

3

Your sudo is configured to clean up the environment before running the command it’s given; PWD is cleared before make runs.

To change this, you can ask sudo not to clean up the environment:

sudo -E make

(if the configuration allows this) or manually provide the PWD value:

sudo make PWD="$PWD"

It should also be possible to change the Makefiles to use Make’s own “current directory” variable, CURDIR:

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(CURDIR) modules

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .