在Linux上读取C程序时如何找到结构体的定义?

发布于 2025-01-06 08:54:00 字数 196 浏览 5 评论 0 原文

我正在阅读xl2tpd的源代码,在阅读这段代码时遇到很多问题。例如,我找不到结构 lac 的定义位置。我如何找到这个结构的定义?

我使用ctags和vim阅读了这段代码,但未能找到结构。我用谷歌搜索并找不到结构。有没有什么方法可以让代码阅读过程变得更舒服呢?也就是说,我可以跳转到大多数变量、函数和结构的定义?

I am reading source code of xl2tpd, and face lots of problems when reading this code. For example I cannot find where the structure lac is defined. How do I find the definition of this structure?

I have used ctags and vim to read this code, but failed to find the structure. I googled and could not find the structure. Is there any method that can make the code reading process more comfortable? That is, I can jump to definition of most variables, functions and structures?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情未る 2025-01-13 08:54:00

尝试使用 vim 来使用 cscope。请按照以下步骤操作 -
1)在xl2tpd目录中运行cscope -R。它将创建文件 cscope.out
2)用vim打开文件,其中使用结构lac
3) 使用 :cs fg 。现在它将显示定义 lac 的文件。
4) 选择file.h。它包含定义。
如果您对 struct lac 的定义特别感兴趣,它如下 -

struct lac
{
    struct lac *next;
    struct host *lns;           /* LNS's we can connect to */
    struct schedule_entry *rsched;
    int tun_rws;                /* Receive window size (tunnel) */
    int call_rws;               /* Call rws */
    int rxspeed;                /* Tunnel rx speed */
    int txspeed;                /* Tunnel tx speed */
    int active;                 /* Is this connection in active use? */
    int hbit;                   /* Permit hidden AVP's? */
    int lbit;                   /* Use the length field? */
    int challenge;              /* Challenge authenticate the peer? */
    unsigned int localaddr;     /* Local IP address */    
    unsigned int remoteaddr;    /* Force remote address to this */
    char authname[STRLEN];      /* Who we authenticate as */
    char password[STRLEN];      /* Password to authenticate with */
    char peername[STRLEN];      /* Force peer name to this */
    char hostname[STRLEN];      /* Hostname to report */
    char entname[STRLEN];       /* Name of this entry */
    int authpeer;               /* Authenticate our peer? */
    int authself;               /* Authenticate ourselves? */
    int pap_require;            /* Require PAP auth for PPP */
    int chap_require;           /* Require CHAP auth for PPP */
    int pap_refuse;             /* Refuse PAP authentication for us */
    int chap_refuse;            /* Refuse CHAP authentication for us */
    int idle;                   /* Idle timeout in seconds */
    int autodial;               /* Try to dial immediately? */
    int defaultroute;           /* Use as default route? */
    int redial;                 /* Redial if disconnected */
    int rmax;                   /* Maximum # of consecutive redials */
    int rtries;                 /* # of tries so far */
    int rtimeout;               /* Redial every this many # of seconds */
    char pppoptfile[STRLEN];    /* File containing PPP options */
    int debug;
    struct tunnel *t;           /* Our tunnel */
    struct call *c;             /* Our call */
};

try cscope with vim. follow steps below -
1) run cscope -R in xl2tpd directory . it will create file cscope.out
2) open file with vim where structure lac is used
3) use :cs f g <lac> . now it will show the files where lac is defined .
4) choose file.h. it contain the definition .
if you are perticulerly interested in definition of struct lac it is below -

struct lac
{
    struct lac *next;
    struct host *lns;           /* LNS's we can connect to */
    struct schedule_entry *rsched;
    int tun_rws;                /* Receive window size (tunnel) */
    int call_rws;               /* Call rws */
    int rxspeed;                /* Tunnel rx speed */
    int txspeed;                /* Tunnel tx speed */
    int active;                 /* Is this connection in active use? */
    int hbit;                   /* Permit hidden AVP's? */
    int lbit;                   /* Use the length field? */
    int challenge;              /* Challenge authenticate the peer? */
    unsigned int localaddr;     /* Local IP address */    
    unsigned int remoteaddr;    /* Force remote address to this */
    char authname[STRLEN];      /* Who we authenticate as */
    char password[STRLEN];      /* Password to authenticate with */
    char peername[STRLEN];      /* Force peer name to this */
    char hostname[STRLEN];      /* Hostname to report */
    char entname[STRLEN];       /* Name of this entry */
    int authpeer;               /* Authenticate our peer? */
    int authself;               /* Authenticate ourselves? */
    int pap_require;            /* Require PAP auth for PPP */
    int chap_require;           /* Require CHAP auth for PPP */
    int pap_refuse;             /* Refuse PAP authentication for us */
    int chap_refuse;            /* Refuse CHAP authentication for us */
    int idle;                   /* Idle timeout in seconds */
    int autodial;               /* Try to dial immediately? */
    int defaultroute;           /* Use as default route? */
    int redial;                 /* Redial if disconnected */
    int rmax;                   /* Maximum # of consecutive redials */
    int rtries;                 /* # of tries so far */
    int rtimeout;               /* Redial every this many # of seconds */
    char pppoptfile[STRLEN];    /* File containing PPP options */
    int debug;
    struct tunnel *t;           /* Our tunnel */
    struct call *c;             /* Our call */
};
梦途 2025-01-13 08:54:00

在浏览第三方代码时,我发现有一些非常有价值的工具:

我相信 Eclipse CDT 也可以让你快速找到您正在查找的任何变量的定义at,但我还没有实际使用它 - 我更喜欢使用控制台程序来进行实际的 C 编码。

尽管至少可以通过 vimemacs 使用 ctags,但这些都不是基于 vim 的。尽管如此,当探索您一无所知的新代码库时,它们可能非常有用......

When going through third-party code, there are a few tools that I have found invaluable:

I believe that the Eclipse CDT also allows you to quickly find the definition of any variable you are looking at, but I have not actually used it - I prefer using console programs for my actual C coding.

None of those are vim-based, although at least ctags can be used via vim or emacs. Nevertheless, they can be very useful when exploring a new codebase that you know nothing about...

浮萍、无处依 2025-01-13 08:54:00

您在谈论这个吗?

源代码已附带一个 tags 文件。

在 Vim 中加载任何文件(在我的例子中为 common.h),您可以使用 :tag lac 跳转到 lac 或 < code>:tselect lac 在此项目中的 3 个匹配项之间进行选择,:tag gconfig 跳转到 gconfig 的唯一定义。

请参阅:帮助标签

Are you talking about this?

The source code already comes with a tags file.

Loading any file (common.h in my case) in Vim you can use :tag lac to jump to the first definition of lac or :tselect lac to choose between the 3 occurrences in this project and :tag gconfig to jump to the unique definition of gconfig.

See :help tags.

我纯我任性 2025-01-13 08:54:00

我正在使用 vim + cscope 并与你有同样的问题。我找到了解决这个问题的方法。

在 vim 中,搜索文本而不是定义。例如,在linux内核源代码中,如果你试图找到“struct file”,
命令如下:

cs find t struct file {

在大多数情况下,您将及时得到准确的定义,请注意,文本“struct file {”没有引号。

希望它能帮助你。

I'm using vim + cscope and have the same issue with you. I find a way to workaround this issue.

in vim, search the text instead of the definition. for example, in the linux kernel source code, if you're trying to find "struct file",
commands this:

cs find t struct file {

you will have a accurate definition timely in most cases, take care, no quotation mark for the text "struct file {".

hope it will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文