folly
folly copied to clipboard
Linking Folly library causes /usr/bin/ld: cannot find -lgflags_shared
OS: Ubuntu 20.04 Folly version: v2023.02.06.00
I've built Folly using instructions from the readme:
./build/fbcode_builder/getdeps.py install-system-deps --recursive
python3 ./build/fbcode_builder/getdeps.py --allow-system-packages build
Then I added Folly in Cmake:
find_package(Folly CONFIG REQUIRED)
When I'm trying to link Folly library to an executable build process fails:
/usr/bin/ld: cannot find -lgflags_shared
However, gflags
was installed (libgflags-dev) and found by Cmake as gflags::gflags_shared
I looked at ${install_dir]/folly/lib/cmake/folly/folly-targets.cmake
and found that gflags_shared
is linked to folly_deps
:
set_target_properties(Folly::folly_deps PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "/usr/include;${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "... ;gflags_shared; ..."
)
I changed gflags_shared
to gflags::gflags_shared
and the issue has gone.
Is it correct that gflags_shared
lib is linked to Folly without gflags::
prefix?
I observe the same issue with ubuntu 22.04 and an older Folly version.
I faced the same issue recently. Adding this helped:
find_package(folly CONFIG REQUIRED)
find_package(gflags CONFIG REQUIRED)
This was introduced by this commit: https://github.com/facebook/folly/commit/0682178745e25aa739a9fccee44e7475dedd03f0
During cmake invocation I see log:
-- Found gflags from package config /usr/lib/x86_64-linux-gnu/cmake/gflags/gflags-config.cmake
-- Found Glog: /usr/lib/x86_64-linux-gnu/libglog.so
-- Found gflags as a dependency of glog::glog, include=/usr/include, libs=gflags_shared
That is where it comes from. I don't fully understand this cmake magic.
This happens on Ubuntu 22.04 with gflags and glog packages from its repository.
Also the bad thing is that according to this log glog is going to link against its shared variant.
Upd: this is wrong. This commit adds just log. gflags_shared was chosen as a link target in earlier versions too.
I've got it. This is issue of Ubuntu's package I guess.
cmake exports for gflags which come with the package define target gflags_shared. And then cmake exports have to define include path, library path and other properties based on chosen build configuration (Release, Debug etc.).
But package containts definitions for gflags_shared target only for Release configuration. So when you trying to build your project in Debug gflags_shared is not expanded to the library name and linker tries to find -lgflags_shared.
To fix this issue you need to modify CMakeLists.txt of your project. Find the place where your call find_package(folly) and insert following code BEFORE this line.
find_package(Gflags CONFIG REQUIRED)
set_target_properties(gflags_shared PROPERTIES
MAP_IMPORTED_CONFIG_DEBUG Release
)
This will map gflags_shared target to Relase configuration even if you are building Debug.