问题描述
假设我有两个文件main.c和func.c func.c是从main.c的main函数调用的.通常,我将生成main.o和func.o,链接器将找到func的定义并将其与main .c
Suppose I have two files main.c and func.c func.c is called from main.c's main function. Normally, I would generate main.o and func.o and linker would find definition of func and tie it up to it's call in main .c
现在,我想通过libclang API做同样的事情.这是我正在制作的Doxygen类型代码浏览实用程序.我能够解析这两个文件.从这里开始,我不知道如何进行.我应该生成* .o文件并用clang链接它们吗?
Now, I want to do same thing through libclang APIs. This is for a Doxygen type code browsing utility I am making. I am able to parse the two files. From here, I don't know how to proceed.Should I generate *.o files and make clang link them?
谢谢,我希望我能清楚地问这个问题
Thanks, I hope I am clear in asking the question
推荐答案
不,不需要将代码实际编译为目标文件.
No, there is no need for actually compiling your code to object files.
可以使用USR(统一符号解析度)在两个翻译单元中建立符号之间的链接.当您在翻译单元的AST中找到一个有趣的地方(在libclang中用CXCursor
表示)时,请调用clang_getCursorUSR()
以获得关联的USR.
The link between symbols in both translation units can be established using USR (Unified Symbol Resolution). When you find an interesting place in the AST of a translation unit (represented by a CXCursor
in libclang), call clang_getCursorUSR()
to get the associated USR.
如果两个CXCursor
具有相同的USR,即使在两个不同的翻译单元中,它们也都与同一符号相关联.
If two CXCursor
have the same USR, even in two different translation units, they are associated to the same symbol.
这篇关于Libclang API获取在其他文件中定义的函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!