如何找到丢失的C头文件(没有Internet)?

时间:2012-10-18 07:26:43

标签: c header-files

我刚才遇到了这个错误:

main.c:8: error: implicit declaration of function ‘malloc’
main.c:8: error: incompatible implicit declaration of built-in function ‘malloc’
main.c:14: error: implicit declaration of function ‘printf’
main.c:14: error: incompatible implicit declaration of built-in function ‘printf’

由于我之前遇到过这些错误,我知道我忘了添加

#include <stdlib.h>
#include <stdio.h>

并且谷歌搜索很快就会显示丢失的标题。

但如果我没有互联网,我该怎么办?我怎样才能找到丢失的头文件?

1 个答案:

答案 0 :(得分:7)

aproposman

apropos [text]可以搜索man文件,例如:

$ apropos malloc
__malloc_hook (3)    - malloc debugging variables
malloc (3)           - Allocate and free dynamic memory
malloc_hook (3)      - malloc debugging variables
mtrace (1)           - Interpret output from MALLOC_TRACE
mtrace (3)           - malloc debugging
muntrace (3)         - malloc debugging
XtMalloc (3)         - memory management functions

然后你可以看一下手册页:

$ man malloc

NAME
       calloc, malloc, free, realloc - Allocate and free dynamic memory

SYNOPSIS
       #include <stdlib.h>

       void *calloc(size_t nmemb, size_t size);
       void *malloc(size_t size);
       void free(void *ptr);
       void *realloc(void *ptr, size_t size);

DESCRIPTION
       calloc()  allocates memory for an array of nmemb elements of size bytes
       each and returns a pointer to the allocated memory.  The memory is  set

这表明缺少#include <stdlib.h>

findgrep

如果apropos不起作用,例如以下示例,您可以使用findgrep搜索所有源文件:

//#include "stdint.h"
#include <stdio.h>

main() {
    int8_t   *xy = (int8_t*) 1;
    printf("%p\n", xy);
}
test.c: In function ‘main’:
test.c:4: error: ‘int8_t’ undeclared (first use in this function)
test.c:4: error: (Each undeclared identifier is reported only once
test.c:4: error: for each function it appears in.)
test.c:4: error: ‘xy’ undeclared (first use in this function)
test.c:4: error: expected expression before ‘)’ token

解决方案:

moose@pc07:/usr/include$ find *.h | xargs grep "int8_t"
db.h:   u_int8_t fileid[DB_FILE_ID_LEN];/* File id. */
db.h:   u_int8_t *bp;           /* Allocated read buffer. */
db.h:   u_int8_t            /* Unique file ID. */
db.h:   int (*get_fileid) __P((DB_MPOOLFILE *, u_int8_t *));
db.h:   int (*set_fileid) __P((DB_MPOOLFILE *, u_int8_t *));
db.h:   int   (*prepare) __P((DB_TXN *, u_int8_t *));
db.h:   u_int8_t gid[DB_GID_SIZE];
db.h:   u_int8_t  gid[DB_GID_SIZE]; /* Global transaction ID */
db.h:   u_int8_t fileid[DB_FILE_ID_LEN];/* File's unique ID for locking. */
db.h:   (pointer = (u_int8_t *)(dbt)->data +                \
db.h:       retdata = (u_int8_t *)(dbt)->data + *__p--;     \
db.h:       if (retdlen == 0 && retdata == (u_int8_t *)(dbt)->data) \
db.h:       retkey = (u_int8_t *)(dbt)->data + *__p--;      \
db.h:       retdata = (u_int8_t *)(dbt)->data + *__p--;     \
db.h:       retdata = (u_int8_t *)(dbt)->data + *__p--;     \
db.h:       pointer = (u_int8_t *)(dbt)->data +         \
db.h:       u_int32_t __off = ((pointer) == (u_int8_t *)(dbt)->data +\
db.h:       if ((u_int8_t *)(dbt)->data + __off + (writedlen) > \
db.h:           (u_int8_t *)(__p - 2))              \
db.h:           writedata = (u_int8_t *)(dbt)->data + __off;    \
db.h:       u_int32_t __off = ((pointer) == (u_int8_t *)(dbt)->data +\
db.h:       if ((u_int8_t *)(dbt)->data + __off + (writeklen) + \
db.h:           (writedlen) > (u_int8_t *)(__p - 4)) {      \
db.h:           writekey = (u_int8_t *)(dbt)->data + __off; \
db.h:           writedata = (u_int8_t *)(dbt)->data + __off;    \
db.h:       pointer = (u_int8_t *)(dbt)->data +         \
db.h:       u_int32_t __off = ((pointer) == (u_int8_t *)(dbt)->data +\
db.h:       if (((u_int8_t *)(dbt)->data + __off) + (writedlen) >   \
db.h:           (u_int8_t *)(__p - 3))              \
db.h:           writedata = (u_int8_t *)(dbt)->data + __off;    \
db.h:   u_int8_t       *lk_conflicts;   /* Two dimensional conflict matrix */
db.h:   int  (*get_lk_conflicts) __P((DB_ENV *, const u_int8_t **, int *));
db.h:   int  (*set_lk_conflicts) __P((DB_ENV *, u_int8_t *, int));
stdint.h:#ifndef __int8_t_defined
stdint.h:# define __int8_t_defined
stdint.h:typedef signed char        int8_t;
stdint.h:typedef unsigned char      uint8_t;
xf86drmMode.h:extern int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
xf86drmMode.h:          uint8_t bpp, uint32_t pitch, uint32_t bo_handle,