简体   繁体   中英

Global variables in c library

How can someone ensure whether source code of some c library (.a) uses global variables or not? Is there any tool which can parse such details?

A library consist of modules (compiled C files). 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 . It is then only visible in that module.

A global variable can be 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.

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.

With VS, open Class View , find Global Functions and Variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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