![](/img/trans.png)
[英]LINK: fatal error LNK1104: cannot open file 'boost_program_options.lib'
[英]LNK1104 When trying to link with boost_filesystem*.lib
我正在使用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;
}
那么,為什么要嘗試鏈接“ libboost_filesystem”而不是“ boost_filesystem”呢?
這里的問題是Boost具有自動鏈接功能 ,如果您不使用CMake,該功能將非常有用。
要禁用此功能,您只需應用一個預處理器定義,如Boost.Config文檔中的表格所示:
BOOST_ALL_NO_LIB
:告訴配置系統不要自動選擇要鏈接的庫。 通常,如果編譯器支持#pragma lib,則只需選擇包含該庫頭之一的行為,即可自動選擇並鏈接正確的庫構建變體。 此宏關閉該功能。
要通過CMake添加預處理器定義,可以使用target_compile_definitions
命令。 例如,如果要將定義限制為僅使用MSVC編譯器,則可以使用生成器表達式 :
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE $<$<BOOL:${MSVC}>:BOOST_ALL_NO_LIB>)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.