且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CMake的不能找到Boost库

更新时间:2022-05-25 18:47:37

您的配置看起来有点怪异和肮脏。尤其是这样的话:

Your configuration looks a bit weird and dirty. Especially things like:

ADD_DEFINITIONS(-DBoost_USE_STATIC_LIBS=ON)

这不是一个C / C ++ preprocessor定义!它是用来控制的CMake如何定义你的项目的联动阶段,Boost库CMake的一个变量。

It's not a C/C++ preprocessor definition! It's a CMake variable which is used to control how CMake will define the linkage stage of your project with Boost libraries.

如果您正确编译升压并没有搞砸任何事,那么目录结构通常是这样的:

If you properly compiled Boost and didn't mess up anything, then the directory structure usually looks like this:

<boost-dir>
  include
    boost
      accumulators
      ...
      aligned_storage.hpp
      ...
  lib
    libboost_atomic-mt-s.a
    ...

注意:升压的根目录,&LT;升压DIR&GT; ,似乎是 D:/ boost_1_54_0 你的情况。

NOTE: The root directory of Boost, <boost-dir>, appears to be D:/boost_1_54_0 in your case.

如果你的情况下,它看起来并不像上面的话,我会建议手动重新安排到一个以上的,因为,再一次,这是应该的。

If in your case it does not look like above, then I'd suggest to rearrange it manually to the one above since, once again, this is how it should be.

在做,让我们做一些CMake的配置。我建议让事情变得简单和干净的摆在首位,并服从CMake的约定。测试如下:

When done, let's do some CMake configuration. I suggest to keep things simple and clean in the first place, and obey the CMake conventions. Test the following:

set(BOOST_INCLUDEDIR D:/boost_1_54_0/include)
set(BOOST_LIBRARYDIR D:/boost_1_54_0/lib)

注意:您可以在顶部 FindBoost.cmake 找到这两个变量的完整描述

NOTE: You can find thorough description of both of these variables at the top of FindBoost.cmake.

set(Boost_USE_STATIC_LIBS   ON)
set(Boost_USE_MULTITHREADED ON)

注意:这是你如何正确设置CMake的变量执行静态链接,但不喜欢你做设置一个不存在的C / C ++ preprocessor定义

NOTE: This is how you enforce static linkage by setting the CMake variable properly, but not like you did by setting a non-existent C/C++ preprocessor definition.

find_package(Boost
             1.54.0
             COMPONENTS thread
                        system
                        log
                        log_setup
                        program_options
             REQUIRED)

include_directories(${Boost_INCLUDE_DIRS})

target_link_libraries(<target_name> ${Boost_LIBRARIES})

注意:而不是&LT; TARGET_NAME&GT; ,把您要构建目标的名称(可执行文件,静态/共享库等)。

NOTE: Instead of <target_name>, put the name of the target that you wish to build (executable, static/shared library, etc.).