-1

I have #include <stdio.h> (no extra spaces) and locate stdio.h shows a few viable options. And yet, when I build, it always says it can't be found. I can include it with a full path #include "/usr/include/stdio.h", but then its own dependencies (features.h) can't be found. I tried specifically sourcing the /usr/include folder which I imagine should already be good to go anyway, but that doesn't help it find it either. So... what the heck? I'm also building with sudo privileges. I can't fathom a good reason for it to not be found.

output from make V=1 VERBOSE=1

******************** Release Build **********************
make -C /lib/modules/4.15.0-58-generic/build SUBDIRS=/home/username/project modules 
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-58-generic'
test -e include/generated/autoconf.h -a -e include/config/auto.conf || (        \
echo >&2;                           \
echo >&2 "  ERROR: Kernel configuration is invalid.";       \
echo >&2 "         include/generated/autoconf.h or include/config/auto.conf are missing.";\
echo >&2 "         Run 'make oldconfig && make prepare' on kernel src to fix it.";  \
echo >&2 ;                          \
/bin/false)
mkdir -p /home/username/project/.tmp_versions ; rm -f /home/username/project/.tmp_versions/*                                        */
make -f ./scripts/Makefile.build obj=/home/username/project
******************** Release Build **********************
  gcc -Wp,-MD,/home/username/project/.file_main.o.d  -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/5/include  -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -Iubuntu/include  -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -fno-PIE -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -DCONFIG_X86_X32_ABI -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_SSSE3=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -DCONFIG_AS_AVX512=1 -DCONFIG_AS_SHA1_NI=1 -DCONFIG_AS_SHA256_NI=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0 -DCC_HAVE_ASM_GOTO -Wframe-larger-than=1024 -fstack-protector-strong -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-var-tracking-assignments -pg -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fno-merge-all-constants -fmerge-constants -fno-stack-check -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -DCHARACTER_DRIVER -DCUSTOM_LOGIC -DOEM_001_CUSTOM_LOGIC -DNO_STATS_DEBUG  -DMODULE  -DKBUILD_BASENAME='"file_main"'  -DKBUILD_MODNAME='"file"' -c -o /home/username/project/file_main.o /home/username/project/file_main.c
In file included from /home/username/project/file_main.c:13:0:
/home/username/project/file.h:10:19: fatal error: stdio.h: No such file or directory
compilation terminated.
CodeMonkey
  • 1,795
  • 3
  • 16
  • 46
  • 5
    If the compiler can't find ``, it's probably misconfigured/misinstalled. – Thomas Jager Aug 26 '19 at 19:02
  • Where is the executable of your compiler? The shown paths don't look like "close neighbours" of likely compiler locations. – Yunnosch Aug 26 '19 at 19:06
  • 1
    Are you compiling with `-nostdinc`? – S.S. Anne Aug 26 '19 at 20:12
  • Compiler is located at /usr/bin. I don't see "nostdinc" in the makefile anywhere. How would one "configure" gcc to know to include the /usr/include folder if not via sourcing? My colcon builds work fine to find stdio.h. I presume that still uses gcc, no? – CodeMonkey Aug 26 '19 at 20:15
  • 2
    What's the command-line invocation? – S.S. Anne Aug 26 '19 at 20:23
  • 1
    See [this question](https://stackoverflow.com/questions/4980819/what-are-the-gcc-default-include-directories) to find out which paths are searched. – the busybee Aug 27 '19 at 06:01
  • Thanks, @thebusybee. The /usr/include folder appears to be listed when using those commands. – CodeMonkey Aug 27 '19 at 13:22
  • @JL2210 To be honest, I'm not familiar enough with make-files to even know where the command is that's starting the build and I can't share the file's contents for proprietary reasons. If I were to guess, however, it would be the part of the make file that says default: $(MAKE) -C $(KERNELDIR) SUBDIRS=$(CURDIR) modules. Maybe the problem is SUBDIRS overriding any other potential include locations? – CodeMonkey Aug 27 '19 at 13:22
  • 1
    If you run `make -n` it would just print the commands without executing them. You can copy and edit these to show them here. But even with the standard call to `make` the commands are printed by default. Presumably we need only the command before the erroneous compile. – the busybee Aug 27 '19 at 13:41
  • @thebusybee, it appears to be just as I showed above. It turns into "make -C /lib/modules/4.15.0-58-generic/build SUBDIRS=/home/username/project modules" – CodeMonkey Aug 27 '19 at 13:48
  • 1
    It looks like the makefile is printing an abbreviated version of the compilation command. Try passing `V=1` to `make` and it will probably print the full command. – dbush Aug 27 '19 at 14:14
  • @dbush, it looks like after a V=1, I can now see the -nostdinc flag. I'm not sure where it's coming from though. Any idea where it might be that I could remove it? – CodeMonkey Aug 27 '19 at 14:29
  • You're building a kernel module. – S.S. Anne Aug 27 '19 at 14:51
  • @JL2210, yes. I am attempting to update a driver from a supplier that was made for 14.04 and has a few deprecated/different functions in 16.04. When I run it after updating, it crashes, so I'm trying to add debugging print statements to see where it's crashing. Something inherent with kernels that prevent std includes? – CodeMonkey Aug 27 '19 at 14:59
  • @JL2210, feel free to answer that the issue to my specific question is the -nostdinc flag and I will mark it correct. It seems I now simply have more questions on top of it. – CodeMonkey Aug 27 '19 at 15:02

1 Answers1

0

You need to use printk, not printf. Its definition is in linux/printk.h, not stdio.h.

Please include these details in your question the next time you ask.

The reason your build is not able to find stdio.h is because it's not available in the kernel sources, and you're compiling with -nostdinc. -nostdinc causes all the default search paths to be ignored.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • My question was why stdio could not be found, not how to debug or print statements for a kernel (which will be a followup question as printk does not suit my needs). Now knowing that it could not be found "because kernels can't use stdio and somehow includes the -nostdinc flag during build" (though I'm not sure why or how), I can now move on to other questions. If you update your answer, I can accept it. – CodeMonkey Aug 27 '19 at 15:42
  • @CodeMonkey See edit. I'm still going to keep the original (for future reference), but I added a note about `stdio.h`. – S.S. Anne Aug 27 '19 at 15:49
  • 1
    Fair enough. Had I known that kernel drivers were handled differently, I might have known to include that info, but then I probably would have already known my answer :-p – CodeMonkey Aug 27 '19 at 16:00