locale
locale copied to clipboard
Can't statically link Boost Locale
Trying to statically link Boost Locale with cmake returns an error.
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(main)
#set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS locale REQUIRED)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Some Boost libraries missing in the system")
endif()
add_executable(main main.cpp)
target_link_libraries(main ${Boost_LIBRARIES})
main.cpp
#include <iostream>
#include <locale>
#include <boost/locale.hpp>
int main() {
boost::locale::generator gen;
std::locale loc1 = gen("pt_BR.UTF-8");
return 0;
}
Dynamic linkage works fine but uncommenting set(Boost_USE_STATIC_LIBS ON) will result in an error.
The boost library was installed via package manager with sudo apt install libboost-all-dev.
As we can see in the prints I have Locale installed and also the icu stuff.

ldd of the binary if not statically linked.

I can statically link other boost libraries, but locale in specific gives me an error.
Output of the make command: log.txt
Ubuntu version: 20.04 and 22.04
Cmake version: >= 3.16
Boost version: 1.71 and 1.74
g++ version: 10.3 and 11.3
As the errors are all of "undefined reference to <ICU name>" you need to link to the ICU target. In particular icudata, icui18n, icuuc, e.g. by using find_package(ICU ...) and the targets ICU:data ICU::i18n ICU::uc
This is done automatically for shared libraries in general. Boost 1.75 config files add those dependencies too in all cases. So upgrading to Boost 1.75 or newer will also solve this.
Side note:
find_package(Boost COMPONENTS locale REQUIRED)
# This is redundant due to the above REQUIRED
if(NOT Boost_FOUND)
message(FATAL_ERROR "Some Boost libraries missing in the system")
endif()