使用QT管理包含有BOOST的代码会出现种种问题,下面的代码使用qmake创建的vcproj编译通过了
string dir_name = sec_log_namespace::logfile_name;
boost::filesystem::path logFilePath(dir_name);
boost::filesystem::path logFileDir = logFilePath.parent_path();
if (!boost::filesystem::exists(logFileDir)) {
boost::filesystem::create_directory(logFileDir);
}
但是链接时出错:
1>sec_log.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl boost::filesystem::path_traits::convert(char const *,char const *,class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> > &,class std::codecvt<unsigned short,char,int> const &)" (__imp_?convert@path_traits@filesystem@boost@@YAXPBD0AAV?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@ABV?$codecvt@GDH@5@@Z) referenced in function "void __cdecl boost::filesystem::path_traits::convert(char const *,char const *,class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> > &)" (?convert@path_traits@filesystem@boost@@YAXPBD0AAV?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@@Z)
1>sec_log.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class std::codecvt<unsigned short,char,int> const & __cdecl boost::filesystem::path::codecvt(void)" (__imp_?codecvt@path@filesystem@boost@@SAABV?$codecvt@GDH@std@@XZ) referenced in function "void __cdecl boost::filesystem::path_traits::convert(char const *,char const *,class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> > &)" (?convert@path_traits@filesystem@boost@@YAXPBD0AAV?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@@Z)
把上面代码放到单独创建的vc工程中,发现没有问题,能编译,能链接。于是怀疑是编译环境问题,比较cl的command line,发现有个-Zc:wchar_t的选项。查了stackflow发现也是字符类型的问题,于是测试
打开vs中的Project->Properites,选择Configuration Properties->C/C++->Language->Treat wchar_t as Build-in type改成Yes。OK,链接成功。
然后改pro文件,加上下面一段:
win32:contains(QMAKE_CC, cl){
QMAKE_CXXFLAGS += -Zc:wchar_t
}