How do I find declarations/definitions without an IDE?

ε祈祈猫儿з 提交于 2020-01-25 22:00:09

问题


I've been using the Eclipse IDE for a few years now. When programming in C I'm used to Ctrl+Click on a symbol and Eclipse takes me to the file and line of the symbol's declaration.

Without an IDE, how do I achieve this? I'm using gcc to compile a massive FOSS project with hundreds of header files. Looking though the C files, I see functions that I would like to know more about. Finding the header file that declares said function is a tedious and manual task. Not to mention manual interpretation of macros...

GCC compiles the project and knows what the declarations are and where. Is it possible to generate a human readable index of all symbol declarations along with their filename and position for a given compilation?


回答1:


Sounds like you're looking for the -aux-info flag; it will write a list of all functions declared or defined in a translation unit (including those in header files) to a specified output file.

Here's a dumb piece of code I wrote for another SO question (don't remember what it was for):

#include <stdio.h>

int main( int argc, const char *argv[] )
{
  const unsigned long long lim = 2000000;
  unsigned long long nums2lim[lim];

  printf( "%zu\n", sizeof nums2lim );
  return 0;
}

Compiling it with

gcc -o test -std=c99 -pedantic -Wall -Werror -aux-info=test.aux test.c

yields this output in test.aux:

/* compiled from: . */
/* /usr/include/libio.h:413:NC */ extern int __underflow (_IO_FILE *);
/* /usr/include/libio.h:414:NC */ extern int __uflow (_IO_FILE *);
/* /usr/include/libio.h:415:NC */ extern int __overflow (_IO_FILE *, int);
/* /usr/include/libio.h:416:NC */ extern wint_t __wunderflow (_IO_FILE *);
/* /usr/include/libio.h:417:NC */ extern wint_t __wuflow (_IO_FILE *);
/* /usr/include/libio.h:418:NC */ extern wint_t __woverflow (_IO_FILE *, wint_t);
/* /usr/include/libio.h:451:NC */ extern int _IO_getc (_IO_FILE *);
/* /usr/include/libio.h:452:NC */ extern int _IO_putc (int, _IO_FILE *);
/* /usr/include/libio.h:453:NC */ extern int _IO_feof (_IO_FILE *);
/* /usr/include/libio.h:454:NC */ extern int _IO_ferror (_IO_FILE *);
/* /usr/include/libio.h:456:NC */ extern int _IO_peekc_locked (_IO_FILE *);
/* /usr/include/libio.h:462:NC */ extern void _IO_flockfile (_IO_FILE *);
/* /usr/include/libio.h:463:NC */ extern void _IO_funlockfile (_IO_FILE *);
/* /usr/include/libio.h:464:NC */ extern int _IO_ftrylockfile (_IO_FILE *);
/* /usr/include/libio.h:482:NC */ extern int _IO_vfscanf (_IO_FILE *, const char *, __va_list_tag *, int *);
/* /usr/include/libio.h:484:NC */ extern int _IO_vfprintf (_IO_FILE *, const char *, __va_list_tag *);
/* /usr/include/libio.h:485:NC */ extern __ssize_t _IO_padn (_IO_FILE *, int, __ssize_t);
/* /usr/include/libio.h:486:NC */ extern size_t _IO_sgetn (_IO_FILE *, void *, size_t);
/* /usr/include/libio.h:488:NC */ extern __off64_t _IO_seekoff (_IO_FILE *, __off64_t, int, int);
/* /usr/include/libio.h:489:NC */ extern __off64_t _IO_seekpos (_IO_FILE *, __off64_t, int);
/* /usr/include/libio.h:491:NC */ extern void _IO_free_backup_area (_IO_FILE *);
/* /usr/include/stdio.h:154:NC */ extern int remove (const char *);
/* /usr/include/stdio.h:156:NC */ extern int rename (const char *, const char *);
/* /usr/include/stdio.h:171:NC */ extern FILE *tmpfile (void);
/* /usr/include/stdio.h:185:NC */ extern char *tmpnam (char *);
/* /usr/include/stdio.h:213:NC */ extern int fclose (FILE *);
/* /usr/include/stdio.h:218:NC */ extern int fflush (FILE *);
/* /usr/include/stdio.h:249:NC */ extern FILE *fopen (const char *, const char *);
/* /usr/include/stdio.h:256:NC */ extern FILE *freopen (const char *, const char *, FILE *);
/* /usr/include/stdio.h:309:NC */ extern void setbuf (FILE *, char *);
/* /usr/include/stdio.h:314:NC */ extern int setvbuf (FILE *, char *, int, size_t);
/* /usr/include/stdio.h:334:NC */ extern int fprintf (FILE *, const char *, ...);
/* /usr/include/stdio.h:339:NC */ extern int printf (const char *, ...);
/* /usr/include/stdio.h:342:NC */ extern int sprintf (char *, const char *, ...);
/* /usr/include/stdio.h:349:NC */ extern int vfprintf (FILE *, const char *, __va_list_tag *);
/* /usr/include/stdio.h:354:NC */ extern int vprintf (const char *, __va_list_tag *);
/* /usr/include/stdio.h:357:NC */ extern int vsprintf (char *, const char *, __va_list_tag *);
/* /usr/include/stdio.h:365:NC */ extern int snprintf (char *, size_t, const char *, ...);
/* /usr/include/stdio.h:369:NC */ extern int vsnprintf (char *, size_t, const char *, __va_list_tag *);
/* /usr/include/stdio.h:406:NC */ extern int fscanf (FILE *, const char *, ...);
/* /usr/include/stdio.h:411:NC */ extern int scanf (const char *, ...);
/* /usr/include/stdio.h:414:NC */ extern int sscanf (const char *, const char *, ...);
/* /usr/include/stdio.h:425:NC */ extern int vfscanf (FILE *, const char *, __va_list_tag *);
/* /usr/include/stdio.h:432:NC */ extern int vscanf (const char *, __va_list_tag *);
/* /usr/include/stdio.h:437:NC */ extern int vsscanf (const char *, const char *, __va_list_tag *);
/* /usr/include/stdio.h:447:NC */ extern int fgetc (FILE *);
/* /usr/include/stdio.h:448:NC */ extern int getc (FILE *);
/* /usr/include/stdio.h:454:NC */ extern int getchar (void);
/* /usr/include/stdio.h:489:NC */ extern int fputc (int, FILE *);
/* /usr/include/stdio.h:490:NC */ extern int putc (int, FILE *);
/* /usr/include/stdio.h:496:NC */ extern int putchar (int);
/* /usr/include/stdio.h:539:NC */ extern char *fgets (char *, int, FILE *);
/* /usr/include/stdio.h:546:NC */ extern char *gets (char *);
/* /usr/include/stdio.h:596:NC */ extern int fputs (const char *, FILE *);
/* /usr/include/stdio.h:602:NC */ extern int puts (const char *);
/* /usr/include/stdio.h:609:NC */ extern int ungetc (int, FILE *);
/* /usr/include/stdio.h:617:NC */ extern size_t fread (void *, size_t, size_t, FILE *);
/* /usr/include/stdio.h:623:NC */ extern size_t fwrite (const void *, size_t, size_t, FILE *);
/* /usr/include/stdio.h:656:NC */ extern int fseek (FILE *, long int, int);
/* /usr/include/stdio.h:661:NC */ extern long int ftell (FILE *);
/* /usr/include/stdio.h:666:NC */ extern void rewind (FILE *);
/* /usr/include/stdio.h:705:NC */ extern int fgetpos (FILE *, fpos_t *);
/* /usr/include/stdio.h:710:NC */ extern int fsetpos (FILE *, const fpos_t *);
/* /usr/include/stdio.h:733:NC */ extern void clearerr (FILE *);
/* /usr/include/stdio.h:735:NC */ extern int feof (FILE *);
/* /usr/include/stdio.h:737:NC */ extern int ferror (FILE *);
/* /usr/include/stdio.h:753:NC */ extern void perror (const char *);
/* test.c:4:NF */ extern int main (int argc, const char **argv); /* (argc, argv) int argc; const char **argv; */

This flag only works for C code, though - it won't give you anything for C++.




回答2:


A light-weight, hackish approach would be to grep through the header files for the function name. Linux syntax would be something like:

find . -type f -name "*.h" | xargs grep [functionName]

It's what I use for my moderately-sized projects. I can't speak to how well it would scale.



来源:https://stackoverflow.com/questions/43004338/how-do-i-find-declarations-definitions-without-an-ide

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!