CMake无法链接升级库

时间:2018-05-04 14:47:24

标签: c++ windows boost cmake

CMake可以找到我安装的升级版本(1.67.0),但每当我尝试链接库时,我都会得到大量未定义的参考"。

这是我想要运行的程序(在boost&#39的网站上找到的示例):

#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;
}

这是我的CMake脚本:

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} )

使用cmake成功链接boost库需要做什么?

编辑: 我已经更改了脚本,所以现在它看起来像这样:

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} )

启用boost调试后,我看到cmake没有任何目录可以搜索库,所以我尝试手动设置它。我刚刚安装了3.11.1版本的CMake,但它仍然无法识别正则表达式库。

2 个答案:

答案 0 :(得分:1)

显然,Boost_LIBRARIES是空的。 您应该在find_package(Boost REQUIRED [COMPONENTS components])中明确指定要与其关联的促销组件,以便它们显示在Boost_LIBRARIES中。

在您的情况下,它应该是find_package(Boost REQUIRED COMPONENTS regex)

请参阅cmake-boost文档:https://cmake.org/cmake/help/v3.8/module/FindBoost.html

答案 1 :(得分:0)

1.安装 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.安装 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)

如何构建

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

如何运行

./bin/main