stdlib.h: no such file or directory

63,632

Solution 1

Your error appears to stem from including linux/time.h when also trying to include stdlib.h. linux/time.h is a kernel header and should only be used in kernel code. stdlib.h is a user-land function and should only be used in user programs. If you notice the error you get:

/usr/include/linux/time.h:12: error: redefinition of 'struct timespec'
/usr/include/linux/time.h:18: error: redefinition of 'struct timeval'

you can see that you are getting an error related to this. As seen in your long trace, this is because stdlib.h is including time.h (the one in /usr/include, not /usr/include/linux). I imagine that this is the real cause of the error you see about not finding stdlib.h (although I cannot imagine the details of how the errors are occurring the way they are).

Solution 2

For anyone who landed here trying to compile a C++ program with Cygwin on Windows, my problem was that I had both MingW and Cygwin. MingW was installed as part of the Cygwin setup. I used the setup.exe (let's call it package installer) from Cygwin and removed all instances of MingW. After that, my test application compiled properly.

Note: This solution is what worked for me. There are gazillion other reasons why you have the same error.

Share:
63,632
Dolan Murvihill
Author by

Dolan Murvihill

I write high-performance data science tools for Internet advertising.

Updated on September 26, 2020

Comments

  • Dolan Murvihill
    Dolan Murvihill over 3 years

    I am using various stdlib functions like srand(), etc. I have the line

    #include <stdlib.h>
    

    at the top of my code.

    I entered this on the command line:

    # find / -name stdlib.h
    
    find: `/home/dmurvihill/.gvfs: permission denied
    /usr/include/stdlib.h
    /usr/include/bits/stdlib.h
    

    So, stdlib.h is clearly in /usr/include.

    My preprocessor:

    # gcc -print-prog-name=cc1
    
    /usr/libexec/gcc/x86_64-redhat-linux/4.5.1/cc1
    

    My preprocessor's default search path:

    # /usr/libexec/gcc/x86_64-redhat-linux/4.5.1/cc1 -v
    
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.5.1/include-fixed"
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../x86_64-redhat-linux/include"
    #include "..." search starts here:
    #include <...> search starts here:
    /usr/local/include
    /usr/lib/gcc/x86_64-redhat-linux/4.5.1/include
    /usr/include
    End of search list.
    

    So, stdlib.h is clearly in /usr/include, which is most definitely supposed to be searched by my preprocessor, but I still get this error!

    /path/to/cpa_sample_code_main.c:15:20: fatal error: stdlib.h: No such file or directory
    compilation terminated
    

    Update

    A program I wrote to test this code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <linux/time.h>
    
    int main()
    {
        printf("Hello, World!\n");
        printf("Getting time...\n");
        time_t seconds;
        time(&seconds);
        printf("Seeding generator...\n");
        srand((unsigned int)seconds);
        printf("Getting random number...\n");
        int value = rand();
        printf("It is %d!",value);
        printf("Goodbye, cruel world!");
        return 0;
    }
    

    The command

    gcc -H -v -fsyntax-only stdlib_test.c
    

    output

    Using built-in specs.
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.5.1/lto-wrapper
    Target: x86_64-redhat-linux
    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,lto --enable-plugin --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
    Thread model: posix
    gcc version 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC) 
    COLLECT_GCC_OPTIONS='-H' '-v' '-fsyntax-only' '-mtune=generic' '-march=x86-64'
     /usr/libexec/gcc/x86_64-redhat-linux/4.5.1/cc1 -quiet -v -H /CRF_Verify/stdlib_test.c -quiet -dumpbase stdlib_test.c -mtune=generic -march=x86-64 -auxbase stdlib_test -version -fsyntax-only -o /dev/null
    GNU C (GCC) version 4.5.1 20100924 (Red Hat 4.5.1-4) (x86_64-redhat-linux)
        compiled by GNU C version 4.5.1 20100924 (Red Hat 4.5.1-4), GMP version 4.3.1, MPFR version 2.4.2, MPC version 0.8.1
    GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.5.1/include-fixed"
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../x86_64-redhat-linux/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/local/include
     /usr/lib/gcc/x86_64-redhat-linux/4.5.1/include
     /usr/include
    End of search list.
    GNU C (GCC) version 4.5.1 20100924 (Red Hat 4.5.1-4) (x86_64-redhat-linux)
        compiled by GNU C version 4.5.1 20100924 (Red Hat 4.5.1-4), GMP version 4.3.1, MPFR version 2.4.2, MPC version 0.8.1
    GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
    Compiler executable checksum: ea394b69293dd698607206e8e43d607e
    . /usr/include/stdio.h
    .. /usr/include/features.h
    ... /usr/include/sys/cdefs.h
    .... /usr/include/bits/wordsize.h
    ... /usr/include/gnu/stubs.h
    .... /usr/include/bits/wordsize.h
    .... /usr/include/gnu/stubs-64.h
    .. /usr/lib/gcc/x86_64-redhat-linux/4.5.1/include/stddef.h
    .. /usr/include/bits/types.h
    ... /usr/include/bits/wordsize.h
    ... /usr/include/bits/typesizes.h
    .. /usr/include/libio.h
    ... /usr/include/_G_config.h
    .... /usr/lib/gcc/x86_64-redhat-linux/4.5.1/include/stddef.h
    .... /usr/include/wchar.h
    ... /usr/lib/gcc/x86_64-redhat-linux/4.5.1/include/stdarg.h
    .. /usr/include/bits/stdio_lim.h
    .. /usr/include/bits/sys_errlist.h
    . /usr/include/stdlib.h
    .. /usr/lib/gcc/x86_64-redhat-linux/4.5.1/include/stddef.h
    .. /usr/include/bits/waitflags.h
    .. /usr/include/bits/waitstatus.h
    ... /usr/include/endian.h
    .... /usr/include/bits/endian.h
    .... /usr/include/bits/byteswap.h
    ..... /usr/include/bits/wordsize.h
    .. /usr/include/sys/types.h
    ... /usr/include/time.h
    ... /usr/lib/gcc/x86_64-redhat-linux/4.5.1/include/stddef.h
    ... /usr/include/sys/select.h
    .... /usr/include/bits/select.h
    ..... /usr/include/bits/wordsize.h
    .... /usr/include/bits/sigset.h
    .... /usr/include/time.h
    .... /usr/include/bits/time.h
    ... /usr/include/sys/sysmacros.h
    ... /usr/include/bits/pthreadtypes.h
    .... /usr/include/bits/wordsize.h
    .. /usr/include/alloca.h
    ... /usr/lib/gcc/x86_64-redhat-linux/4.5.1/include/stddef.h
    . /usr/include/linux/time.h
    .. /usr/include/linux/types.h
    ... /usr/include/asm/types.h
    .... /usr/include/asm-generic/types.h
    ..... /usr/include/asm-generic/int-ll64.h
    ...... /usr/include/asm/bitsperlong.h
    ....... /usr/include/asm-generic/bitsperlong.h
    ... /usr/include/linux/posix_types.h
    .... /usr/include/linux/stddef.h
    .... /usr/include/asm/posix_types.h
    ..... /usr/include/asm/posix_types_64.h
    In file included from /CRF_Verify/stdlib_test.c:3:0:
    /usr/include/linux/time.h:9:8: error: redefinition of ‘struct timespec’
    /usr/include/time.h:120:8: note: originally defined here
    /usr/include/linux/time.h:15:8: error: redefinition of ‘struct timeval’
    /usr/include/bits/time.h:75:8: note: originally defined here
    Multiple include guards may be useful for:
    /usr/include/asm/posix_types.h
    /usr/include/bits/byteswap.h
    /usr/include/bits/endian.h
    /usr/include/bits/select.h
    /usr/include/bits/sigset.h
    /usr/include/bits/stdio_lim.h
    /usr/include/bits/sys_errlist.h
    /usr/include/bits/time.h
    /usr/include/bits/typesizes.h
    /usr/include/bits/waitflags.h
    /usr/include/bits/waitstatus.h
    /usr/include/gnu/stubs-64.h
    /usr/include/gnu/stubs.h
    /usr/include/wchar.h
    
    • Josh
      Josh almost 13 years
      Smells like either you are hardcoding search paths in your build tool, or it's a permission issue to me (Why can't you access .gvfs? Are you not the owner of all the files in your home folder??). Try using a tool like strace and recompile again, checking for file access errors. Grep the strace output for stdlib.h, make sure it's looking where you expect.
    • zwol
      zwol almost 13 years
      Recompile with gcc -H -v -fsyntax-only /path/to/cpa_sample_code_main.c and post the COMPLETE, UNEDITED output, please.
    • Jason
      Jason almost 13 years
      can you give us the compiler command you're using? For example, gcc -g -o cpa_sample_code ...
    • Dolan Murvihill
      Dolan Murvihill almost 13 years
      Hi all, I am root. The permissions line for .gvfs is: d?????????? which is very odd. The compiler command/output will take a while, as I am building using a very large, complicated makefile system that I was provided. I'll create a simple program that uses stdlib and try to compile that; if it works, I will start looking in the makefile. Will get back to you soon. Thanks, SO!
    • Dolan Murvihill
      Dolan Murvihill almost 13 years
      Hi Zack, The output is longer than 600 characters; I added it to the OP as an update. I'll also try doing this with the original file and get back to you soon. Also, I forgot to mention in my original question that this program is a kernel module.
    • Misha M
      Misha M almost 13 years
      Can you run make -n in the directory are compiling for your large project? I agree with Josh, it sounds like your project Makefile(s) is messing up the include paths.
    • Dolan Murvihill
      Dolan Murvihill almost 13 years
      Hi Misha, I ran make -n, but I can't post it; the output is over 160,000 characters. I will try to find the relevant part.
    • bdonlan
      bdonlan almost 13 years
      @SirTasty, gvfs is a FUSE filesystem and can only be accessed by the UID that mounted it (not even root can view a FUSE mount from a different user if cross-uid access is disabled), so those errors are expected
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' almost 13 years
      What would be relevant, but isn't included in your question, is the options you're invoking gcc with. There's probably a stray -I- or -nostdinc in there.
    • Misha M
      Misha M almost 13 years
      Gilles brings up a good point. Maybe you can grep for -nostdinc in all your Makefiles.
    • Dolan Murvihill
      Dolan Murvihill almost 13 years
      Hi all, A grep for -nostdinc did, indeed, turn up a lot, but not in any makefiles. They are cropping up in a bunch of hidden files ending with ".o.cmd" (the file names correspond to the names of actual object files I am trying to build). I have never seen this file type before. The file command says that all of these files are of type "ASCII text, with very long lines." Does anyone know what these are for?
    • Alexeon
      Alexeon over 3 years
      IF YOU ARE FACING THE ERROR THAT "stdlib.h" NOT FOUND IN C++ THEN I HAVE A SIMPLE SOLUTION FOR YOU:- SOLUTION: remove the "stdlib.h" and include "cstdlib" in its place
  • Dolan Murvihill
    Dolan Murvihill almost 13 years
    Bingo. I replaced srand with get_random_bytes() from linux/random.h, and it now works like a charm. I am new to kernel space programming, and I hadn't realized that ALL of the standard libraries were unavailable (which explains the -nostdinc tags). Thanks for all of the help, everyone!