【发布时间】:2020-12-05 01:49:08
【问题描述】:
我正在使用 CMake 构建一个 C++ 项目,并且最近使用 Boost C++ 库添加了临时文件创建。这就是我在 CMake 文件中包含 Boost 的方式:
# get boost
SET(Boost_USE_STATIC_LIBS ON)
SET(Boost_USE_MULTITHREADED ON)
SET(Boost_USE_STATIC_RUNTIME OFF)
FIND_PACKAGE(Boost REQUIRED COMPONENTS system filesystem)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(${exe_name} ${Boost_LIBRARIES})
ADD_LIBRARY(${lib_name} ${common_SOURCES})
TARGET_LINK_LIBRARIES(${lib_name} ${CMAKE_DL_LIBS} ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(${exe_name} ${lib_name})
这就是我在 C++ 中的称呼:
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
...
input_file *Utils::get_input_file_from_string(const std::string &inp) {
std::string real_inp(inp);
boost::filesystem::path temp_path = boost::filesystem::unique_path();
const std::string temp_pathstr = temp_path.native();
boost::filesystem::path temp_folder = boost::filesystem::temp_directory_path();
const std::string temp_folderstr = temp_folder.native();
const std::string temp_fname = temp_folderstr + "/" + temp_pathstr;
const char *temp_pathchar = temp_fname.c_str();
...
FILE *temp = fopen(temp_pathchar, "wb+");
...
input_file *ret = new input_file;
loadInput(ret, temp);
fclose(temp);
return ret;
}
当我运行程序时,我收到以下错误:
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
Aborted (core dumped)
但是,通过运行 bash 命令...
unset LANG
...程序运行成功(注意echo $LANG返回C.UTF-8)
有没有办法编辑程序,这样我每次加载新的 shell 实例时就不需要运行unset LANG 命令?
编辑:我知道可以在 c++ 中取消设置环境变量,但我相信在 Boost 中必须有一个与语言环境相关的解决方案,以确保不会引发错误。
(PS 重申一下,在运行可执行文件之前调用unset LANG 时,程序成功运行并获得所需的输出)
【问题讨论】:
-
这能回答你的问题吗? Remove environmental variable programmatically 在程序开始时取消设置环境变量可能会解决问题。
-
它作为一种解决方法很有帮助,但我正在寻找一些特定于 boost 的代码,以确保不会引发此错误 - 我的想法是我在 cmake 文件中缺少导入或命令确保在 boost 中正确设置语言环境
-
您可能应该设置所需的区域设置 - 或者如果它需要依赖于系统,则传递正确的区域以使用(作为命令行参数?)。在后一种情况下,您可能会争辩说将
LANG设置为所需的值也可以。 -
我相信,如果您包含 LANG 的当前值,将会有所帮助。我感到你的痛苦——不幸的是,i18n 环境变量的错误使用已经很长时间了,但至少有一些 C++ 库允许你在调用中显式指定语言环境;无论环境如何。
-
@HansOlsson 当前的
LANG是C.UTF-8- 问题已编辑以说明这一点