LNK1104尝试与boost_filesystem * .lib链接时

时间:2015-08-21 22:38:38

标签: c++ visual-studio boost cmake boost-filesystem

我使用CMake使用一些Boost库创建跨平台项目。我使用以下命令创建库。

.\b2.exe --prefix=C:/Boost install --with-python --with-system --with-filesystem address-model=32 toolset=msvc-12.0 link=shared threading=multi --layout=tagged --build-type=complete

该项目与python和系统库链接良好,但当它尝试与文件系统库链接时,它会失败。它正在寻找" libboost_filesystem * .lib" (它不存在)然后使用" boost_python * .lib"和" boost_system * .lib"文件没有问题。

我的顶级CMakeLists.txt如下:

file(GLOB SOURCES src/*.cpp)

add_subdirectory(shape)
add_subdirectory(py_shape)

#define sources and executable
set(EXECUTABLE_NAME "renderer2d")
add_executable(${EXECUTABLE_NAME} ${SOURCES})

#find python
find_package(PythonInterp)
find_package(PythonLibs 2.7 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})

#detect and add SFML
#this line checks a cmake file for hints on where to find cmake
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules"     ${CMAKE_MODULE_PATH})
#find any version 2.x of SFML
#see the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics network audio)
include_directories(${SFML_INCLUDE_DIR})

#find and include Boost python libraries
set(Boost_USE_STATIC_LIBS OFF)
find_package(Boost COMPONENTS python system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIR})

#link all found libraries to the executable
target_link_libraries(${EXECUTABLE_NAME} ${PYTHON_LIBRARIES}     ${SFML_LIBRARIES} ${Boost_LIBRARIES} shape)

#install target
install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin)

这个exe的唯一源文件:

#include <iostream>
#include <SFML/Graphics.hpp>
#include "../shape/inc/Shape.hpp"
#include <boost/python.hpp>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::python;
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
  try
  {
    Py_Initialize();

    //need to insert working directory into path
    path working_directory = absolute("./").normalize();
    PyObject* sys_path = PySys_GetObject("path");
    PyList_Insert(sys_path, 0,     PyString_FromString(working_directory.string().c_str()));

    sf::RenderWindow window(sf::VideoMode(800, 600), "renderer2d");
    Shape shape;
    shape.Initialize();

    while(window.isOpen())
    {
      sf::Event event;
      while(window.pollEvent(event))
      {
        if(event.type == sf::Event::Closed)
          window.close();
        if(event.type == sf::Event::KeyPressed)
        {
          if(event.key.code == sf::Keyboard::Q)
            window.close(); 
          else if(event.key.code == sf::Keyboard::U)
            shape.Update();
          else if(event.key.code == sf::Keyboard::S)
            shape.SetGreen(255);
        }
      }

      window.clear();
      window.draw(*shape.GetShape());
      window.display();
    }
  } catch(error_already_set){PyErr_Print();}

  return 0;
}

那么,为什么要尝试链接&#34; libboost_filesystem&#34;而不是&#34; boost_filesystem&#34;?

1 个答案:

答案 0 :(得分:4)

这里的问题是Boost有an auto-linking feature,如果您不使用CMake,这可能很有用。

要禁用此功能,您只需应用预处理器定义,如docs for Boost.Config中的表格所示:

  

BOOST_ALL_NO_LIB:告诉配置系统不要自动选择要链接的库。通常,如果编译器支持#pragma lib,那么将自动选择并链接正确的库构建变体,只需通过包含其中一个库头的行为即可。此宏将关闭此功能。

要通过CMake添加预处理器定义,可以使用target_compile_definitions命令。例如,如果要将定义限制为仅限MSVC编译器,则可以使用generator expression

target_compile_definitions(${EXECUTABLE_NAME} PRIVATE $<$<BOOL:${MSVC}>:BOOST_ALL_NO_LIB>)