Back to Blog
By AriesZhou · · 1 min read

On Function Symbol Export Issues in GCC Compilation

Android

I added a feature to Android P’s liblog module that calls a function from another source file. A.c defines fun, A.h declares it, and B.c includes A.h before calling fun. The file names and paths are correct, but compilation still fails with error: undefined reference to.

The function is clearly defined, so the linker must be unable to see it. I later noticed that some functions in logprint.c use the LIBLOG_ABI_PUBLIC prefix. This macro is defined as #define LIBLOG_ABI_PUBLIC __attribute__((visibility("default"))) and controls symbol visibility. liblog’s Android.bp sets the following compiler option:

cflags: [
        "-Werror",
        "-fvisibility=hidden",
  ......
  ]

The visibility attribute overrides the value specified at compile time via the -fvisibility option. The default visibility attribute causes the symbol to be exported in all cases, while the hidden visibility attribute hides the corresponding symbol.

The "-fvisibility=hidden" option hides symbols by default. Functions and variables that are not explicitly marked as visible therefore cannot be referenced from other files in the shared library, producing the undefined reference error. The fix is to mark externally shared functions and variables with default visibility.

Reference: Controlling symbol visibility