I’m using CMake (2.8.3), Boost::filesystem(1.42.0) in Ubuntu 10.10. The code compiles OK but I keep getting the following error when linking:
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
The code in question is the following:
#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;
}
}
}
}
in my CMakeLists.txt file, I have:
find_package(Boost 1.4.0 COMPONENTS filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(executable
${Boost_LIBRARIES})
but I’ve also tried with:
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})
and many other combinations of the above.
The linker is complaining about the != operator for the directory_iterator type. If I look to the contents of the header in /usr/include/boost/filesystem/path.cpp I can see that the operator is defined there. Any ideas why this is happening?
I’d really appreciate your help.
After adding the missing include files:
and namespaces (std), and defining a macro for this “fs” shortcut of your code:
Then I tried the first CMakeLists.txt approach that you mentioned, and it worked… I also have the same system as you.
What I would recommend is using
MESSAGEin CMake to inspect these $Boost_* variables and debug a bit the compilation system with it…