1

CMake can find my version of boost installed (1.67.0) but whenever I try to link a library I get a ton of "undefined reference".

This is the program that I want to run (the example found on boost's website):

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main(int, char *[])
{
    std::string line;
    boost::regex pat("^Subject: (Re: |Aw: )*(.*)");

    while(std::cin) {
        std::getline(std::cin, line);
        boost::smatch matches;
        if(boost::regex_match(line, matches, pat)) {
            std::cout << matches[2] << '\n';
        }
    }

    return 0;
}

This is my CMake script:

cmake_minimum_required( VERSION 3.5 )

project( REGEX )

set( BOOST_ROOT D:/Dev/boost/boost_1_67_0/boost_1_67_0 )
set( Boost_LIBRARY_DIR D:/Dev/boost/boost_1_67_0/boost_1_67_0/stage/lib )
set( Boost_USE_STATIC_LIBS ON )
set( Boost_USE_MULTITHREADED ON )

find_package( Boost REQUIRED )

include_directories( ${Boost_INCLUDE_DIRS} )

add_executable( regex ${PROJECT_SOURCE_DIR}/main.cxx )

target_link_libraries( regex ${Boost_LIBRARIES} )

What do I need to do to successfully link boost libraries with cmake?

EDIT: I've changed the script so now it looks like this:

cmake_minimum_required( VERSION 3.5 )

project( REGEX )

set( BOOST_ROOT D:/Dev/boost/boost_1_67_0/boost_1_67_0 )
set( BOOST_INCLUDEDIR D:/Dev/boost/boost_1_67_0/boost_1_67_0/boost )
set( BOOST_LIBRARYDIR D:/Dev/boost/boost_1_67_0/boost_1_67_0/stage/lib )
set( Boost_USE_STATIC_LIBS ON )
set( Boost_USE_MULTITHREADED ON )
set( Boost_DEBUG ON )

find_package( Boost COMPONENTS regex REQUIRED )

include_directories( ${Boost_INCLUDE_DIRS} )

add_executable( regex ${PROJECT_SOURCE_DIR}/main.cxx )

target_link_libraries( regex ${Boost_LIBRARIES} )

After enabling debugging for boost I saw cmake didn't have any directory to search for the libraries so I've tried to set it manually. I've just installed the 3.11.1 version of CMake but it still doesn't recognize the regex library.

Alexandru Ica
  • 137
  • 2
  • 8
  • What CMake version are you using? See if this [answer](https://stackoverflow.com/questions/50030212/cmake-unable-to-find-boost-librariesvs-2017/50032021#50032021) to a different thread helps. CMake before version 3.11 cannot handle the new naming scheme that was introduced in boost 1.66. – vre May 04 '18 at 15:20
  • Thank you very much for the suggestion, I will install the latest version of cmake to see if it works – Alexandru Ica May 04 '18 at 16:28
  • For `CMake 3.11.0` or `3.11.1` to work you may also need to add `set(Boost_ADDITIONAL_VERSIONS "1.67.0")` to your `CMakeLists.txt` before the `find_package` call to Boost, because `Boost 1.67.0` is not contained in the `_Boost_KNOWN_VERSIONS` variable of shipped FindBoost.cmake file. – vre May 04 '18 at 17:40
  • I've just done that but it still gives me the same error: "Could not find the following static Boost libraries: boost_regex" – Alexandru Ica May 04 '18 at 18:19
  • Did you cleared (deleted) the CMakeCache.txt after doing those changes? It's a common issue not doing so. Did you know that you can download prebuilt binaries for a lot of platforms from this [link](https://sourceforge.net/projects/boost/files/boost-binaries/1.67.0/) ? The Boost guys provide those binaries. Just to make sure problems in building Boost yourself was not the real issue. – vre May 04 '18 at 18:56
  • Yes I've deleted the cache file but it still can't find the library. How is downloading pre-built binaries different? Won't I have to link these libraries in CMake the same way anyway? – Alexandru Ica May 04 '18 at 19:37
  • The structure of Boost installation may be different from your build tree. So I always recommend to start from prebuilt binaries to make the CMake process work. Later you can try to figure out how to adapt the Boost build process to your needs. – vre May 04 '18 at 20:51

2 Answers2

4

Apparently, Boost_LIBRARIES is empty. You should explicitly specify boost components you want to link with in find_package(Boost REQUIRED [COMPONENTS components]) for them to appear in Boost_LIBRARIES.

In your case it should be find_package(Boost REQUIRED COMPONENTS regex)

See cmake-boost documentation: https://cmake.org/cmake/help/v3.8/module/FindBoost.html

k.v.
  • 1,193
  • 8
  • 11
0

1. Install CMake

cd ~
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
tar xf cmake-3.14.5.tar.gz
cd cmake-3.14.5
./bootstrap --parallel=10
make -j4
sudo make -j4 install

2. Install Boost

cd ~
wget https://boostorg.jfrog.io/artifactory/main/release/1.69.0/source/boost_1_69_0.tar.gz
tar xf boost_1_69_0.tar.gz
cd boost_1_69_0
./bootstrap.sh
./b2 ... cxxflags="-std=c++0x -stdlib=libc++" linkflags="-stdlib=libc++" ...
sudo ./b2 toolset=gcc -j4 install

3. CMakeLists.txt

# Defines AppBase library target.
project(recipe_01)
cmake_minimum_required(VERSION 3.5)

include(GNUInstallDirs)

set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})

if(CMAKE_CXX_STANDARD EQUAL 98 OR CMAKE_CXX_STANDARD LESS 14)
   message(FATAL_ERROR "app requires c++14 or newer")
elseif(NOT CMAKE_CXX_STANDARD)
   set(CMAKE_CXX_STANDARD 14)
   set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

find_package(Boost 1.60 REQUIRED COMPONENTS regex)

add_executable(main main.cpp)

target_link_libraries(main Boost::regex)

How to build

mkdir build
cd build
cmake ..
cmake --build .

How to run

./bin/main

sun1211
  • 1,219
  • 10
  • 15