I am trying to compile a simple test project to make sure all my tools, such as CMake, Boost, Clang are working.
The source code I am trying to compile is LibLinkTest.cpp:
#include <boost/filesystem.hpp>
#include <fstream>
#include <iostream>
#include <string>
#include <assert.h>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
    std::string str{"test passed"};
    std::cout << str << std::endl;
    boost::filesystem::path p{argv[1]};
    try {
        std::cout << p.string() << std::endl;
        if (exists(p)) {
            if (is_regular_file(p)) {
                std::ifstream file{p.c_str()};
                std::string line;
                if (file.good() && file.is_open()) {
                    while (file >> line) {
                        std::cout << line << std::endl;
                    }
                }
                file.close();
          } else if (is_directory(p)) {
              std::cout << p.string() << " is a directory " << std::endl;                                                 // path stream inserter
          } else
              std::cout << p.string() << " exists, but is neither a regular file nor a directory" << std::endl;
        }
        else {
            std::cout << p.string() << " does not exist" << std::endl;
        }
      }
    catch (const filesystem_error& ex) {
        std::cout << ex.what() << std::endl;
    }
}
Running:
c++ -std=c++11 -I /usr/boost_1_54_0/boost/ LibLinkTest.cpp -o LibTest -L /usr/boost_1_54_0/lib/ -lboost_filesystem -lboost_system
compiles it just fine and the code runs (with a seg fault, but I'll address that separately).
However, I want to use CMake with my other projects. Trying to compile LibLinkTest.cpp with the following CMakeLists.txt causes linking errors.
CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
PROJECT(BoostTest CXX)
SET(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
FIND_PACKAGE(Boost 1.54.0 COMPONENTS filesystem system REQUIRED)
INCLUDE_DIRECTORIES( 
    ${Boost_INCLUDE_DIR}
    /usr/include/
    /usr/include/c++/4.2.1/
)
ADD_EXECUTABLE(
    LibTest 
    LibLinkTest.cpp
)
TARGET_LINK_LIBRARIES(LibTest boost_filesystem boost_system)
The output from running cmake ../ is: 
-- The CXX compiler identification is Clang 5.0.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Boost version: 1.54.0
-- Found the following Boost libraries:
--   filesystem
--   system
-- Configuring done
-- Generating done
And finally running make gives the error: 
Scanning dependencies of target LibTest
[100%] Building CXX object CMakeFiles/LibTest.dir/LibLinkTest.cpp.o
Linking CXX executable LibTest
Undefined symbols for architecture x86_64:
  "std::string::c_str() const", referenced from:
      boost::system::system_error::what() const in LibLinkTest.cpp.o
      boost::filesystem::path::c_str() const in LibLinkTest.cpp.o
  "std::string::empty() const", referenced from:
      boost::system::system_error::what() const in LibLinkTest.cpp.o
  "std::basic_ios<char, std::char_traits<char> >::good() const", referenced from:
      _main in LibLinkTest.cpp.o
It seems like a setting in CMake is not correct, perhaps something that points to the std c++ lib. Does anyone know how I can configure CMake correctly?
I have solved this by changing the line:
SET(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
to
SET(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libstdc++")
It seems like I had just made a typo in the CMakeLists.txt.
On a side note, does anyone know why I need to set this flag? Is it a C++11 thing? It also seems to have solved my issues with the seg fault that I was getting when compiling from the command line.
You should change the line
TARGET_LINK_LIBRARIES(LibTest boost_filesystem boost_system)
to
TARGET_LINK_LIBRARIES(LibTest ${Boost_LIBRARIES})
The FIND_PACKAGE command sets the Boost_LIBRARIES variable to what you need to link with. 
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With