cross-compile kernel module failed

I use these commands to compile kernel successfully.

export LOCALVERSION=-tegra
export CROSS_COMPILE=aarch64-linux-gnu-
make ARCH=arm64 O=$TEGRA_KERNEL_OUT tegra_defconfig
make ARCH=arm64 O=$TEGRA_KERNEL_OUT -j4

then I want to cross compile a simple kernel module and it failed.

#include <linux/init.h>
#include <linux/module.h>
static int hello_init(void)
  printk(KERN_ALERT "Hello, world\n");
  return 0;
static void hello_exit(void)
  printk(KERN_ALERT "Goodbye, cruel world\n");
make ARCH=arm64 O=$TEGRA_KERNEL_OUT -j4 modules_prepare

cd ~/nvidia/nvidia_sdk/module_src
make ARCH=arm64 -C ~/nvidia/nvidia_sdk/JetPack_4.2.1_Linux_GA_P2888/Linux_for_Tegra/sources/kernel/kernel-4.9 M=$(pwd)

failed message:

make: Entering directory '~/nvidia/nvidia_sdk/JetPack_4.2.1_Linux_GA_P2888/Linux_for_Tegra/sources/kernel/kernel-4.9'

  ERROR: Kernel configuration is invalid.
         include/generated/autoconf.h or include/config/auto.conf are missing.
         Run 'make oldconfig && make prepare' on kernel src to fix it.

WARNING: Symbol version dump ./Module.symvers
           is missing; modules will have no dependencies and modversions.

  LD      ~/nvidia/nvidia_sdk/module_src/built-in.o
  CC [M]  ~/nvidia/nvidia_sdk/module_src/hello.o
In file included from <command-line>:0:0:
././include/linux/kconfig.h:4:32: fatal error: generated/autoconf.h: No such file or directory
compilation terminated.
scripts/ recipe for target '~/nvidia/nvidia_sdk/module_src/hello.o' failed
make[1]: *** [/~/nvidia/nvidia_sdk/module_src/hello.o] Error 1
Makefile:1629: recipe for target '_module_/~/nvidia/nvidia_sdk/module_src' failed
make: *** [_module_/~/nvidia/nvidia_sdk/module_src] Error 2
make: Leaving directory '/~/nvidia/nvidia_sdk/JetPack_4.2.1_Linux_GA_P2888/Linux_for_Tegra/sources/kernel/kernel-4.9'

when I have cross-compile kernel to TEGRA_KERNEL_OUT path, how can I cross-compile external kernel module next?

I follow NVIDIA Tegra Linux Driver Package Development Guide 32.2 Release, and faild.

The kernel_directory should be below as the documentation said at the beginning.

/usr/src/linux-headers-$(uname -r)-ubuntu18.04_aarch64
/lib/modules/$(uname -r)/build

Thanks ShaneCCC.
I am testing this on a non-Tegra system. I decompress the kernel_headers.tbz2. Finally I use this conmmand to compile kernel module successfully.

make ARCH=arm64 -C ~/linux-headers-4.9.140-tegra-linux_x86_64/kernel-4.9 M=$(pwd)

I still have a question. According to the documentation as follow, “$TEGRA_KERNEL_OUT as the kernel directory” , TEGRA_KERNEL_OUT is a path to kernel source or linux-headers? But when compiling kernel, TEGRA_KERNEL_OUT is a path to the desired destination for the compiled kernel.

2. Execute the following command to prepare the kernel source tree for building out-of-tree kernel modules:
$ make ARCH=arm64 O=$TEGRA_KERNEL_OUT -j<n> modules_prepare
Where <n> indicates the number of parallel processes to be used. A typical value is the number of CPUs in your system.
When building out-of-tree modules, specify $TEGRA_KERNEL_OUT as the kernel directory.

TEGRA_KERNEL_OUT here is the out file from building the kernel from kernel source like Image…
And you should “export TEGRA_KERNEL_OUT=” to build the kernel Image