【问题标题】:cmake, boost::filesystem linking error (undefined directory_iterator.operator!=)cmake, boost::filesystem 链接错误(未定义的 directory_iterator.operator!=)
【发布时间】:2011-02-27 01:48:18
【问题描述】:

我在 Ubuntu 10.10 中使用 CMake (2.8.3)、Boost::filesystem(1.42.0)。代码编译正常,但链接时我不断收到以下错误:

CMakeFiles/sample.dir/sample.cpp.o: In function `main':
sample.cpp:(.text+0x1af8d): undefined reference to `int operator!=<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> > >(boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> > const&, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> > const&)'
collect2: ld returned 1 exit status

有问题的代码如下:

#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>

int main()
{
  string folder;
  string extension;
  fs::directory_iterator end;

  folder    = ".";
  extension = ".zip";

  for (fs::directory_iterator i(folder); i != end; ++i)
  { if (fs::is_regular_file(i->status()))
    {
      if (boost::algorithm::ends_with(i->leaf(), extension))
      {
        cout << i->leaf() << " has extension .zip" << endl;
      }
    }
  }
}

在我的CMakeLists.txt 文件中,我有:

find_package(Boost 1.4.0 COMPONENTS filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

target_link_libraries(executable
    ${Boost_LIBRARIES})

但我也尝试过:

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost 1.4.0 COMPONENTS filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

target_link_libraries(executable
    ${Boost_FILESYSTEM_LIBRARY})

以及以上的许多其他组合。

链接器抱怨!= 运算符的directory_iterator 类型。如果我查看/usr/include/boost/filesystem/path.cpp 中标题的内容,我可以看到那里定义了运算符。任何想法为什么会发生这种情况?

非常感谢您的帮助。

【问题讨论】:

    标签: c++ boost linker cmake


    【解决方案1】:

    添加缺失的包含文件后:

    #include <string>
    #include <iostream>
    

    和命名空间(std),并为您的代码的这个“fs”快捷方式定义一个宏:

    #define fs boost::filesystem
    

    然后我尝试了您提到的第一个 CMakeLists.txt 方法,并且成功了...我也有与您相同的系统。

    我建议在 CMake 中使用 MESSAGE 来检查这些 $Boost_* 变量并使用它调试编译系统...

    【讨论】:

    • 非常感谢鲁本。我希望我可以删除我的愚蠢问题。我和你一样从头开始,一切正常。
    • 不要对命名空间使用#define:namespace fs = boost::filesystem
    • 谢谢@Maik,我实际上从来没有为命名空间定义别名,我只是想让这段代码编译得越少越好:)
    【解决方案2】:

    我遇到了同样的问题,除了我的编译器(VS 2010)抱怨没有运算符!= 采用“boost::filesystem3::directory_iterator”类型的左侧操作数。原来我已经这样声明了我的结束迭代器:

    boost::filesystem::directory_iterator endDir();
    

    当我删除括号时,一切编译和链接都很好。我猜编译器将原始行解释为一个名为 endDir 的函数,它返回了一个 directory_iterator,这仍然不能完全解释为什么它抱怨左侧操作数,而不是右侧操作数。

    Scott Meyers 将此问题描述为C++'s "most vexing parse."

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 2016-06-10
      • 2017-12-23
      • 2014-01-09
      • 1970-01-01
      相关资源
      最近更新 更多