C语言 stdlib.h:没有这样的文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6902254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:17:13  来源:igfitidea点击:

stdlib.h: no such file or directory

cgccincludec-preprocessorredhat

提问by Dolan Murvihill

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

我正在使用各种 stdlib 函数,如 srand() 等。我有这条线

#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.

所以,stdlib.h 显然在 /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!

所以,stdlib.h 显然在 /usr/include 中,我的预处理器肯定会搜索它,但我仍然收到此错误!

/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

采纳答案by Dave

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

您的错误似乎源于linux/time.h在尝试包含时包含stdlib.hlinux/time.h是一个内核头文件,应该只在内核代码中使用。 stdlib.h是用户级功能,只能在用户程序中使用。如果你注意到你得到的错误:

/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.his 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).

您可以看到您收到与此相关的错误。正如在您的长跟踪中所见,这是因为stdlib.h包括time.h(在 中的一个/usr/include,而不是/usr/include/linux)。我想这是您看到的关于未找到 stdlib.h 的错误的真正原因(尽管我无法想象这些错误是如何发生的细节)。

回答by Shailen

For anyone who landed here trying to compile a C++ program with Cygwinon Windows, my problem was that I had both MingWand 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.

对于那些试图在 Windows 上使用Cygwin编译 C++ 程序的人来说,我的问题是我同时拥有MingWCygwin。MingW 是作为 Cygwin 安装程序的一部分安装的。我使用了setup.exeCygwin的(我们称之为包安装程序)并删除了 MingW 的所有实例。之后,我的测试应用程序正确编译。

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

注意:此解决方案对我有用。有无数其他原因导致您出现相同的错误。

回答by cmannett85

I've found another cause when using g++ v6.x, bug 70129. I triggered it when specifying -isystem(via CMake's SYSTEMkeyword) for a Boost include directory - which ultimately included cstdlib.

我在使用 g++ v6.x 时发现了另一个原因,错误 70129。我在为 Boost 包含目录指定-isystem(通过 CMake 的SYSTEM关键字)时触发了它- 最终包含cstdlib.