2012-10-18 41 views
3

我只是有这个错误:如何找到丢失的C头文件(没有Internet)?

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> 

和谷歌搜索会相当很快显露缺少的头。

但是,如果我没有互联网,我该怎么办?我如何找到缺少的头文件?

回答

7

aproposman

apropos [text]可以通过人的文件进行搜索,例如:

$ 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 

解决办法:

[email protected]:/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, 
+1

great respons电子! – logoff