d-m*_*man 5 c++ boost locale cmake
我正在使用 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})
Run Code Online (Sandbox Code Playgroud)
这就是我在 C++ 中的称呼:
# 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})
Run Code Online (Sandbox Code Playgroud)
当我运行程序时,出现以下错误:
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
但是,通过运行 bash 命令...
#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;
}
Run Code Online (Sandbox Code Playgroud)
...程序运行成功(注意echo $LANG返回C.UTF-8)
有没有办法编辑程序,这样我就不需要unset LANG每次加载新的 shell 实例时都运行命令?
编辑:我知道可以在 c++ 中取消设置环境变量,但我相信 Boost 中必须有一个与语言环境相关的解决方案,以确保不会引发错误。
(PS 只是重申一下,该程序unset LANG在运行可执行文件之前调用时成功运行并具有所需的输出)
如果没有来回或访问您的计算机,就很难很好地调试它,因为我无法在本地重现这一点。无论如何我都会尝试一下。我的猜测是由于转换.native()为std::string. 试试这个:
input_file *Utils::get_input_file_from_string(const std::string &inp) {
...
boost::filesystem::path temp_path = boost::filesystem::unique_path();
boost::filesystem::path temp_folder = boost::filesystem::temp_directory_path();
boost::filesystem::path temp_file_path = temp_folder / temp_path;
// try either:
std::string temp_file_path_string = temp_file_path.string();
const char *temp_pathchar = temp_file_path_string.c_str();
// or:
const char *temp_pathchar = temp_file_path.c_str();
...
}
Run Code Online (Sandbox Code Playgroud)
string()如果您使用由于使用正斜杠而使用的行,则这在非 POSIX 平台上表现不佳。
另外,我会尝试对所显示的代码进行更多最小化。似乎有可能unique_path()抛出错误。例如,尝试对路径或路径字符串进行硬编码,然后查看错误是否仍然存在。
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |