Search code examples
c++windowscmakeboostvc10

Visual studio 10 fails to build a test example (32bit) of boost regex (1.47): link error 1104


Boost provides a test example (linked to boost_regex):

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

int main()
{
    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] << std::endl;
    }
}

My own CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

project(test)

set(CMAKE_BUILD_TYPE Release)

set(BOOST_ROOT "D:/test/boost_1_47_0")
set(BOOST_INCLUDE_DIRS "${BOOST_ROOT}")
set(BOOST_LIBRARY_DIRS "${BOOST_ROOT}/stage/lib")

INCLUDE_DIRECTORIES (${BOOST_INCLUDE_DIRS})
LINK_DIRECTORIES( ${BOOST_LIBRARY_DIRS} )

set(testlib_src test.cpp)
add_library(testlib SHARED ${testlib_src})

target_link_libraries(testlib boost_regex-vc100-mt-1_47)

Build (32 bit)

  1. Source Path:

cwd: D:\test\test\test.cpp

 D:\test\test\CMakeList.txt

 D:\test\test\build
  1. Boost Path:

D:\test\boost_1_47_0\

Built by

bootstrap.bat

.\b2 --build-type=complete variant=release link=shared runtime-link=shared stage
  1. Os: win10

Configure: cmake 3.24 + vs 2010 + nmake

cd build

cmake .. -G "Visual Studio 10" -G "NMake Makefiles"

nmake
  1. Error:

fatal error LNK1104: cannot open file 'libboost_regex-vc100-mt-1_47.lib'

  1. Fix

A file named boost_regex-vc100-mt-1_47.lib exists in the stage/lib directory but without the lib prefix.

Is the missing lib prefix causing the link error?

Moreover, I tried both 64-bit and 32-bit builds, and both failed.


Solution

  • The lib prefix is used for static libraries, you've only built the shared ones https://www.boost.org/doc/libs/1_47_0/more/getting_started/windows.html#library-naming. You need to define BOOST_ALL_DYN_LINK to swtich the automatic linking to use shared libraries.

    You should use https://cmake.org/cmake/help/latest/module/FindBoost.html and link to the generated targets which is both simpler and more reliable than manually specifying the includes and libraries:

    cmake_minimum_required(VERSION 2.6)
    
    project(test)
    
    set(CMAKE_BUILD_TYPE Release)
    
    set(BOOST_ROOT "D:/test/boost_1_47_0")
    find_package(Boost 1.47 REQUIRED COMPONENTS
                 regex)
    set(testlib_src test.cpp)
    add_library(testlib SHARED ${testlib_src})
    
    target_link_libraries(testlib Boost::regex)