Question

There are a lot of system variables in output of nm it looks like this

N
_CRT_MT
_CRT_fmode
_CRT_glob
Dictionary::variable4
namespace1::variable1
__cpu_features
__crt_xc_end__
__crt_xc_start__
__crt_xi_end__
__crt_xi_start__
__crt_xl_start__
__crt_xp_end__
__crt_xp_start__
__crt_xt_end__
__crt_xt_start__
__tls_end__
__tls_start__
__xl_a
__xl_c
__xl_d
__xl_z
_argc
_argv
_bss_end__
_bss_start__
_data_end__
_data_start__
_end__
_fmode
_tls_end
_tls_index
_tls_start
_tls_used
mingw_initltsdrot_force
mingw_initltsdyn_force
mingw_initltssuo_force
variable0
variable10

Is it possible to print only user defined variables - in this case variable10, variable0, Dictionary::variable1, Dictionary::variable4, N?

Was it helpful?

Solution

Not that I know of. But at least you can safely filter all variables starting with double underscore or underscore + uppercase letter, as these are reserved for the implementation:

$ nm -j foo | grep -v '^_[A-Z]\|^__\+[A-Za-z]'
N
Dictionary::variable4
namespace1::variable1
_argc
_argv
_bss_end__
_bss_start__
_data_end__
_data_start__
_end__
_fmode
_tls_end
_tls_index
_tls_start
_tls_used
mingw_initltsdrot_force
mingw_initltsdyn_force
mingw_initltssuo_force
variable0
variable10

You can probably filter more by adding additional patterns that reliably denote implementation-defined identifiers.

Alternatively, create an empty executable (i.e. one which contains no user-defined symbols) and compute the difference of the output of nm on each executable via comm:

$ # Preparation
$ echo 'int main() { }' > mt.cpp
$ g++ -o mt.out mt.cpp
$ nm -j mt.out > mt.symbols
$ 
$ nm -j your_exe > your_exe.symbols
$ comm -23 your_exe.symbols mt.symbols
N
Dictionary::variable4
namespace1::variable1
variable0
variable10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top