Repository: abseil/abseil-cpp

Issue #817: Underlinking issues when linking with -Wl,--no-undefined
Created: 2020-10-14T11:54:24Z
Describe the bug
With the current LTS version (20200923.1) and latest master branch (f3f785a), I run into issues when compiling Abseil with the -Wl,--no-undefined linker flag:
[ 74%] Linking CXX shared library libabsl_flags_internal.so
cd /home/akien/Projects/mageia/Sandbox/_rpm/abseil-cpp/build/absl/flags && /usr/bin/cmake -E cmake_link_script CMakeFiles/flags_internal.dir/link.txt --verbose=1
/usr/bin/c++ -fPIC -Wl,--no-undefined -shared -Wl,-soname,libabsl_flags_internal.so -o libabsl_flags_internal.so CMakeFiles/flags_internal.dir/internal/flag.cc.o  -Wl,-rpath,/home/akien/Projects/mageia/Sandbox/_rpm/abseil-cpp/build/absl/flags:/home/akien/Projects/mageia/Sandbox/_rpm/abseil-cpp/build/absl/synchronization:/home/akien/Projects/mageia/Sandbox/_rpm/abseil-cpp/build/absl/debugging:/home/akien/Projects/mageia/Sandbox/_rpm/abseil-cpp/build/absl/base:/home/akien/Projects/mageia/Sandbox/_rpm/abseil-cpp/build/absl/time:/home/akien/Projects/mageia/Sandbox/_rpm/abseil-cpp/build/absl/strings:/home/akien/Projects/mageia/Sandbox/_rpm/abseil-cpp/build/absl/numeric: libabsl_flags_commandlineflag_internal.so libabsl_flags_config.so libabsl_flags_marshalling.so libabsl_flags_program_name.so ../synchronization/libabsl_synchronization.so ../synchronization/libabsl_graphcycles_internal.so ../debugging/libabsl_stacktrace.so ../debugging/libabsl_symbolize.so ../base/libabsl_malloc_internal.so ../debugging/libabsl_debugging_internal.so ../debugging/libabsl_demangle_internal.so ../time/libabsl_time.so ../time/libabsl_civil_time.so ../time/libabsl_time_zone.so ../strings/libabsl_str_format_internal.so ../strings/libabsl_strings.so ../base/libabsl_base.so ../base/libabsl_spinlock_wait.so -lpthread ../strings/libabsl_strings_internal.so ../numeric/libabsl_int128.so ../base/libabsl_throw_delegate.so ../base/libabsl_raw_logging_internal.so ../base/libabsl_log_severity.so 
/usr/bin/ld: CMakeFiles/flags_internal.dir/internal/flag.cc.o:(.data.rel.ro._ZTVN4absl14flags_internal8FlagImplE[_ZTVN4absl14flags_internal8FlagImplE]+0x28): undefined reference to `absl::CommandLineFlag::IsRetired() const'
/usr/bin/ld: CMakeFiles/flags_internal.dir/internal/flag.cc.o:(.data.rel.ro._ZTIN4absl14flags_internal8FlagImplE[_ZTIN4absl14flags_internal8FlagImplE]+0x10): undefined reference to `typeinfo for absl::CommandLineFlag'
collect2: error: ld returned 1 exit status

This is the first error, but there seems to be more further on:
/usr/bin/ld: CMakeFiles/statusor.dir/statusor.cc.o: in function `absl::internal_statusor::Helper::HandleInvalidStatusCtorArg(absl::Status*)':
statusor.cc:(.text+0x160): undefined reference to `absl::InternalError(absl::string_view)'
/usr/bin/ld: CMakeFiles/statusor.dir/statusor.cc.o: in function `absl::Status::ToString[abi:cxx11]() const':
statusor.cc:(.text._ZNK4absl6Status8ToStringB5cxx11Ev[_ZNK4absl6Status8ToStringB5cxx11Ev]+0x61): undefined reference to `absl::Status::ToStringSlow[abi:cxx11]() const'
/usr/bin/ld: CMakeFiles/statusor.dir/statusor.cc.o: in function `absl::Status::Unref(unsigned long)':
statusor.cc:(.text._ZN4absl6Status5UnrefEm[_ZN4absl6Status5UnrefEm]+0x27): undefined reference to `absl::Status::UnrefNonInlined(unsigned long)'

Steps to reproduce the bug
mkdir build && cd build
export LDFLAGS="-Wl,--no-undefined"
cmake .. -DBUILD_SHARED_LIBS=ON ..
make

What version of Abseil are you using?
LTS version (20200923.1) and latest master branch (f3f785a)
What operating system and version are you using
Mageia Linux 8 (Cauldron, development release)
What compiler and version are you using?
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-mageia-linux-gnu/10/lto-wrapper
Target: x86_64-mageia-linux-gnu
Configured with: ../configure --prefix=/usr --libexecdir=/usr/lib --with-slibdir=/lib64 --with-pkgversion='Mageia 10.2.0-2.mga8' --with-bugurl=https://bugs.mageia.org/ --mandir=/usr/share/man --infodir=/usr/share/info --enable-checking=release --enable-languages=c,c++,ada,fortran,objc,obj-c++,d --enable-linker-build-id --build=x86_64-mageia-linux-gnu --host=x86_64-mageia-linux-gnu --with-cpu=generic --with-system-zlib --enable-threads=posix --with-linker-hash-style=gnu --enable-shared --enable-long-long --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --enable-ssp --disable-libssp --disable-libffi --disable-werror --with-isl --with-python-dir=/lib/python3.8/site-packages --enable-lto --with-gcc-major-version-only
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.0 (Mageia 10.2.0-2.mga8)

What build system are you using?
CMake 3.18.4.
The full configuration which triggers the issue in Mageia packaging is the use of our %cmake macro:
  CFLAGS="${CFLAGS:--O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -fasynchronous-unwind-tables}" ; export CFLAGS ; 
  CXXFLAGS="${CXXFLAGS:--O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -fasynchronous-unwind-tables}" ; export CXXFLAGS ; 
  FFLAGS="${FFLAGS:--O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -fasynchronous-unwind-tables }" ; export FFLAGS ; 
  FCFLAGS="${FCFLAGS:--O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -fasynchronous-unwind-tables }" ; export FCFLAGS ; 
  LDFLAGS="${LDFLAGS:- -Wl,--as-needed -Wl,--no-undefined -Wl,-z,relro -Wl,-O1 -Wl,--build-id -Wl,--enable-new-dtags}" ; export LDFLAGS 
    /usr/bin/cmake -Wno-dev \
        -S . \
        -B build \
        -DCMAKE_INSTALL_PREFIX:PATH=/usr \
        -DCMAKE_INSTALL_LIBDIR:PATH=/usr/lib64 \
        -DCMAKE_INSTALL_LIBEXECDIR:PATH=/usr/libexec \
        -DCMAKE_INSTALL_RUNSTATEDIR:PATH=/run \
        -DCMAKE_INSTALL_SYSCONFDIR:PATH=/etc \
        -DINCLUDE_INSTALL_DIR:PATH=/usr/include \
        -DLIB_INSTALL_DIR:PATH=/usr/lib64 \
        -DSYSCONF_INSTALL_DIR:PATH=/etc \
        -DSHARE_INSTALL_PREFIX:PATH=/usr/share \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DCMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING="-DNDEBUG" \
        -DCMAKE_C_FLAGS_RELWITHDEBINFO:STRING="-DNDEBUG" \
%if "lib64" == "lib64" 
        -DLIB_SUFFIX=64 \
%endif 
        -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
        -DCMAKE_MODULE_LINKER_FLAGS="-Wl,--as-needed  -Wl,-z,relro -Wl,-O1 -Wl,--build-id -Wl,--enable-new-dtags" \
        -DBUILD_SHARED_LIBS:BOOL=ON \
        -DBUILD_STATIC_LIBS:BOOL=OFF

But using only -Wl,--no-undefined is sufficient to trigger the issue.
Additional context
I'm trying to package Abseil for my Linux distribution Mageia, and our packaging policies imply the use of -Wl,--as-needed -Wl,--no-undefined as linker flags (see e.g. Gentoo wiki for a rationale from a distro standpoint), which can expose underlinking issues as we see here.

Duplicate issue #816: Undefined symbols on macOS with -DBUILD_SHARED_LIBS
Created: 2020-10-13T21:43:29Z
Describe the bug
I'm trying to write a MacPorts port of abseil with a cmake build that includes shared libraries.
When I pass -DBUILD_SHARED_LIBS to cmake, I hit this error:
:info:build /usr/bin/clang++ -pipe -Os -DNDEBUG -I/opt/local/include -stdlib=libc++ -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -mmacosx-version-min=10.15 -dynamiclib -Wl,-headerpad_max_install_names -L/opt/local/lib -Wl,-headerpad_max_install_names -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -o libabsl_statusor.dylib -install_name /opt/local/lib/libabsl_statusor.dylib CMakeFiles/statusor.dir/statusor.cc.o  -Wl,-rpath,/opt/local/lib ../strings/libabsl_strings.dylib ../strings/libabsl_strings_internal.dylib ../base/libabsl_base.dylib ../base/libabsl_spinlock_wait.dylib ../numeric/libabsl_int128.dylib ../base/libabsl_throw_delegate.dylib ../types/libabsl_bad_variant_access.dylib ../base/libabsl_raw_logging_internal.dylib ../base/libabsl_log_severity.dylib
:info:build make[2]: Leaving directory `/opt/local/var/macports/build/_opt_local_ports_devel_abseil/abseil/work/abseil-abseil-cpp-bd0de71/build'
:info:build [ 68%] Built target random_distributions
:info:build Undefined symbols for architecture x86_64:
:info:build   "absl::lts_2020_09_23::InternalError(absl::lts_2020_09_23::string_view)", referenced from:
:info:build       absl::lts_2020_09_23::internal_statusor::Helper::HandleInvalidStatusCtorArg(absl::lts_2020_09_23::Status*) in statusor.cc.o
:info:build   "absl::lts_2020_09_23::Status::UnrefNonInlined(unsigned long)", referenced from:
:info:build       absl::lts_2020_09_23::BadStatusOrAccess::~BadStatusOrAccess() in statusor.cc.o
:info:build       absl::lts_2020_09_23::internal_statusor::Helper::HandleInvalidStatusCtorArg(absl::lts_2020_09_23::Status*) in statusor.cc.o
:info:build   "absl::lts_2020_09_23::Status::ToStringSlow() const", referenced from:
:info:build       absl::lts_2020_09_23::internal_statusor::Helper::Crash(absl::lts_2020_09_23::Status const&) in statusor.cc.o
:info:build ld: symbol(s) not found for architecture x86_64

Steps to reproduce the bug
cd abseil-cpp-20200923.1
mkdir build
cd build
/opt/local/bin/cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_CXX_STANDARD=11 ..
/usr/bin/make -j12 -w all

What version of Abseil are you using?
Release 20200923.1
What operating system and version are you using
macOS 10.15.7
What compiler and version are you using?
clang -v
Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

What build system are you using?
cmake version 3.18.4

Closing comment (2020-10-14T15:45:00Z): Thanks for the report. I'm doing to close this as a duplicate of #817. It should be fixed shortly.