简体   繁体   English

C库中的全局变量

[英]Global variables in c library

How can someone ensure whether source code of some c library (.a) uses global variables or not? 有人如何确保某个c库(.a)的源代码是否使用全局变量? Is there any tool which can parse such details? 是否有任何工具可以解析此类详细信息?

A library consist of modules (compiled C files). 库由模块(编译的C文件)组成。 A module can declare a global variable. 模块可以声明全局变量。 Any module (inside or outside the library) referencing the global variable will cause the module that declares the global variable to be included in your build. 任何引用全局变量的模块(库内部或外部)都将导致声明全局变量的模块包含在您的构建中。

A global variable can be static . 全局变量可以是static It is then only visible in that module. 然后,它仅在该模块中可见。

A global variable can be extern . 全局变量可以是extern That means it does not exist in the current module, and could not exist in the library at all in which case the user must provide the variable. 这意味着它在当前模块中不存在,并且根本不可能在库中存在,在这种情况下,用户必须提供变量。

In the first and the third case, the variable will be listed in the symbol table of the library or in the fixup table of the library. 在第一种和第三种情况下,该变量将在库的符号表或库的修订表中列出。 The first is the list of symbols available to a callee; 第一个是被叫方可用的符号列表; the second a list of variables whose exact address must still be fixed at load time. 第二个变量的列表,其确切地址在加载时仍必须固定。 There may be variables that must be fixed-up that are not exported. 可能有一些必须修正的变量没有导出。 Symbols always include the name, fix-ups don't need to be listed with their name. 符号始终包含名称,无需列出修补程序的名称。

So a tool to look at the symbol table of the library is probably what can answer your question. 因此,查看库的符号表的工具可能是可以回答您问题的工具。

Note: the fact that a global variable is listed in the symbol table of the library does not imply the variable is used by any function in the library. 注意:库的符号表中列出了全局变量,但这并不意味着该库中的任何函数都会使用该变量。

By hand: 用手:

In case you've already known how to open a .a file, take a look in it. 如果您已经知道如何打开.a文件,请查看它。

If in the file, there is: 如果在文件中,则有:

  • A variable is used without declaration (so it is defined in another file) 使用变量时无需声明(因此在另一个文件中定义)

  • A variable is declared out of every function 从每个函数中声明一个变量

So this file contains global variable. 因此,此文件包含全局变量。

By tool: 通过工具:

Every compiler have this option for you (I believe) 每个编译器都为您提供此选项(我相信)

For example: 例如:

With GCC, using GDB, type info variables to list all the global variable. 在GCC和GDB中,键入info variables以列出所有全局变量。

With VS, open Class View , find Global Functions and Variable 使用VS,打开“ 类视图” ,找到全局函数和变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM