C ++ Boost:Windows中对boost :: system :: generic_category()的未定义引用

时间:2018-05-13 14:21:56

标签: c++ boost

我正在尝试在CodeBlocks中编译此代码

#include <boost/filesystem.hpp>
#include <iostream>

using namespace boost::filesystem;

int main()
{


  if ( !boost::filesystem::exists( "myfile.txt" ) )
{
  std::cout << "Can't find my file!" << std::endl;
}
}

使用此编译标志:

g++.exe -Wall -fexceptions -g -O3 -pedantic-errors -Wall -std=c++0x -lboost_system -IC:\Users\moe\Desktop\boost_1_67_0 -c C:\Users\moe\Desktop\oo\main.cpp -o obj\Debug\main.o

但我总是收到这个错误:

boost::system::generic_category()

这是我编译代码时收到的错误日志:

Untitled4.o: In function `boost::system::error_category::std_category::equivalent(std::error_code const&, int) const':
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:733: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:736: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:748: undefined reference to `boost::system::generic_category()'
Untitled4.o: In function `boost::system::error_category::std_category::equivalent(int, std::error_condition const&) const':
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:703: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:706: undefined reference to `boost::system::generic_category()'
Untitled4.o: In function `boost::filesystem::path_traits::convert(char const*, char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&)':
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path::codecvt()'
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path_traits::convert(char const*, char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&, std::codecvt<wchar_t, char, int> const&)'
Untitled4.o: In function `boost::filesystem::exists(boost::filesystem::path const&)':
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/operations.hpp:446: undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
collect2.exe: error: ld returned 1 exit status

2 个答案:

答案 0 :(得分:2)

将库放在链接器命令行的末尾:

答案 1 :(得分:-1)

由于您的错误日志还包含以下内容: C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path::codecvt()'

很明显,您也缺少-lboost_filesystem

因此仅将-lboost_system添加到g ++命令是没有用的。

您可以将-lboost_system -lboost_filesystem添加到您的g ++命令中。