【问题标题】:How to find the definition of a c type like `pthread_t`?如何找到像`pthread_t`这样的c类型的定义?
【发布时间】:2021-07-24 06:32:48
【问题描述】:

我想查看pthread_t 的定义,如何找到pthread_t 的定义?我知道通过man 3 pthread_create,我可以找到函数的用法。但是如何找到一个类型的定义呢? 我用vim,可以直接跳转到vim中pthread_t的定义吗?

我知道我可以打开/usr/include/pthread.h,但是找到定义需要一些时间。如何直接跳转到pthread_t的定义?说,使用 ctags?

【问题讨论】:

  • 查看.h 文件。
  • 试试clang -E main.c | grep pthread_t。未测试。
  • 使用像 VSCode 这样的 IDE 绝对不可能?
  • 为什么?严重地。任何在文档中没有其成员的类型,例如pthread_t,都应该被视为不透明的。您不需要知道使用它的细节,并且在任何情况下都不应依赖您可能发现的任何细节,因为它们可能因实现而异,甚至从一个版本到另一个相同的实现。

标签: c vim pthreads manpage


【解决方案1】:

既然你在C工作,你应该有类似的东西:

#include <pthread.h>

靠近缓冲区的顶部,鉴于 Vim 的 C 导向默认值,它应该允许你做一些开箱即用的事情。

  1. 您可以通过将光标放在&lt;pthread.h&gt; 上并按gf 来跳转到标题。 &lt;C-w&gt;f 做同样的事情,但在一个新窗口中。

    :help gf

  2. 您可以通过将光标放在宏定义上并按[&lt;C-d&gt; 来跳转到宏的第一个可用定义。或者,[d 打印命令行中的第一个可用定义,[D 打印所有可用定义,&lt;C-w&gt;d 在新窗口中跳转到第一个可用定义。

    您也可以使用:djump:dsearch:dlist 达到相同的效果。

    :help definition-list

  3. 如果您要查找的不是宏,例如 pthread_t(类型)或 pthread_cancel()(函数),您可以使用上述命令的不太专业的表亲:

    还有相关的:help :psearch

标签是另一种解决方案,它需要您递归地索引/usr/include 并将Vim 指向生成的tags 文件。

请注意,在这种非常特殊的情况下,找不到任何有价值的东西。

【讨论】:

    【解决方案2】:

    首先,你必须设置:h 'path',这样Vim 才能找到pthread.h。从 gcc/clang 设置 path 的示例代码(取自 my config):

    " gcc_include([{gcc} [, {force} [, {ft}]]])
    " get GCC include dirs
    function s:gcc_include(gcc = 'gcc', force = 0, ft = &filetype) abort
        if a:force || !has_key(s:, 'gcc_include_' . a:ft)
            " $INCLUDE
            let s:gcc_include_{a:ft} = split($INCLUDE, has('win32') ? ';' : ':')
            " builtin dirs
            let l:cmd = printf('%s -x%s -v -E -', a:gcc, a:ft is# 'cpp' ? 'c++' : a:ft)
            let l:include = map(systemlist(l:cmd, []), 'trim(v:val)')
            let l:ix1 = match(l:include, '#include <\.\.\.>') + 1
            let l:ix2 = match(l:include, '\.$', l:ix1) - 1
            if l:ix1 <= l:ix2
                let s:gcc_include_{a:ft} += map(l:include[l:ix1 : l:ix2], 'simplify(v:val)')
            endif
        endif
        return s:gcc_include_{a:ft}
    endfunction
    
    " :GccInclude[!] [/path/to/gcc]
    " set local &path to GCC include dirs
    command! -bar -bang -nargs=? -complete=file GccInclude let &l:path = join(['.'] +
        \ s:gcc_include(empty(<q-args>) ? 'gcc' : <q-args>, <bang>0) + [','], ',')
    

    yourfile.c 处于活动状态时,运行:GccInclude clang 并确保:set path? 显示有意义的内容。

    之后你可以使用:h include-search下的任何东西,例如,将光标移动到“pthread_t”并按Ctrl-WCtrl-I。 p>

    请注意,它是纯内置功能,因此有时 Vim 可能无法正确解析 C 代码。

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      相关资源
      最近更新 更多