3.11.4.1. Tools

The tool for listing symbols differs for different platforms.

Examples on GitHub

3.11.4.1.1. Example

Here is an example of library which has both defined and undefined symbols:

# Top-level CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(foo)

add_library(boo Boo.hpp Boo.cpp Foo.hpp)

Method Boo::boo declared and will be defined:

// Boo.hpp

#ifndef BOO_HPP_
#define BOO_HPP_

class Boo {
 public:
  int boo(int, char);
};

#endif // BOO_HPP_
// Boo.cpp

#include "Boo.hpp"

#include "Foo.hpp"

int Boo::boo(int x, char a) {
  Foo foo;

  return foo.foo(a, 1.0 + x);
}

Method Foo::foo declared, will be used but will not be defined:

// Foo.hpp

#ifndef FOO_HPP_
#define FOO_HPP_

class Foo {
 public:
  int foo(char, double);
};

#endif // FOO_HPP_
// Boo.cpp

#include "Boo.hpp"

#include "Foo.hpp"

int Boo::boo(int x, char a) {
  Foo foo;

  return foo.foo(a, 1.0 + x);
}

Build library:

[library-examples]> rm -rf _builds
[library-examples]> cmake -Hlibrary-symbols -B_builds
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /.../library-examples/_builds

[library-examples]> cmake --build _builds
Scanning dependencies of target boo
[ 50%] Building CXX object CMakeFiles/boo.dir/Boo.cpp.o
[100%] Linking CXX static library libboo.a
[100%] Built target boo

[library-examples]> ls _builds/libboo.a
_builds/libboo.a

3.11.4.1.2. Linux

Use nm for Linux:

> which nm
/usr/bin/nm

Install instructions for Ubuntu:

> sudo apt-get install binutils

nm --defined-only will show symbols defined by current module. Add --demangle to beautify output:

[library-examples]> nm --defined-only --demangle _builds/libboo.a

Boo.cpp.o:
0000000000000000 T Boo::boo(int, char)

nm --undefined-only will show undefined:

[library-examples]> nm --undefined-only --demangle _builds/libboo.a

Boo.cpp.o:
                 U __stack_chk_fail
                 U Foo::foo(char, double)

3.11.4.1.3. OSX

Same nm tool with --defined-only/--undefined-only options can be used on OSX platform. However --demangle is not available, c++filt can be used instead:

> which nm
/usr/bin/nm

> which c++filt
/usr/bin/c++filt

Defined symbols:

> nm --defined-only _builds/libboo.a | c++filt

_builds/libboo.a(Boo.cpp.o):
0000000000000000 T Boo::boo(int, char)

Undefined symbols:

> nm --undefined-only _builds/libboo.a | c++filt

_builds/libboo.a(Boo.cpp.o):
Foo::foo(char, double)

3.11.4.1.4. Windows

DUMPBIN tool can help to discover symbols on Windows platform. It’s available via Developer Command Prompt:

> where dumpbin
...\msvc\2015\VC\bin\dumpbin.exe

Add /SYMBOLS to see the table. Defined symbols can be filtered by External + SECT:

[library-examples]> dumpbin /symbols _builds\Debug\boo.lib | findstr "External" | findstr "SECT"
00A 00000000 SECT4  notype ()    External     | ?boo@Boo@@QAEHHD@Z (public: int __thiscall Boo::boo(int,char))
01C 00000000 SECT7  notype       External     | __real@3ff0000000000000

Undefined by External + UNDEF:

[library-examples]> dumpbin /symbols _builds\Debug\boo.lib | findstr "External" | findstr "UNDEF"
00B 00000000 UNDEF  notype ()    External     | ?foo@Foo@@QAEHDN@Z (public: int __thiscall Foo::foo(char,double))
00C 00000000 UNDEF  notype ()    External     | @_RTC_CheckStackVars@8
00D 00000000 UNDEF  notype ()    External     | __RTC_CheckEsp
00E 00000000 UNDEF  notype ()    External     | __RTC_InitBase
00F 00000000 UNDEF  notype ()    External     | __RTC_Shutdown
019 00000000 UNDEF  notype       External     | __fltused

Use /EXPORTS if you want to see the symbols available in DLL:

[library-examples]> dumpbin /exports _builds\Release\boo.dll | findstr "Boo"
  1    0 00001000 ?boo@Boo@@QAEHHD@Z

Use undname to demangle:

[library-examples]> undname ?boo@Boo@@QAEHHD@Z
Microsoft (R) C++ Name Undecorator
Copyright (C) Microsoft Corporation. All rights reserved.

Undecoration of :- "?boo@Boo@@QAEHHD@Z"
is :- "public: int __thiscall Boo::boo(int,char)"