![](/img/trans.png)
[英]Compiling C++ code on a system with two different Boost versions installed
[英]Use two different Versions of boost
我嘗試建立一個使用兩個不同版本的Boost的cmake。 (我使用僅在boost 1.55下運行的框架,但我的應用程序需要在boost 1.57下運行)
我的想法是制作2個Cmake構建流程
應用Cmake Boost 1.57
cmake_minimum_required (VERSION 2.6)
project (Application)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=auto -std=c++0x ")
set(Boost_DEBUG ON)
set(Boost_NO_SYSTEM_PATHS TRUE)
set(BOOST_ROOT /opt/boost/boost_1_57)
find_package(Boost 1.57 REQUIRED COMPONENTS thread filesystem log system)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
SYSTEM /opt/boost/boost_1_57/include
)
ADD_LIBRARY( AppLib SHARED testVersion.cpp ...)
框架Cmake Boost 1.55
cmake_minimum_required(VERSION 2.8.3)
project(Test)
add_subdirectory(Application)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=auto -std=c++0x ")
set(Boost_NO_SYSTEM_PATHS TRUE)
set(BOOST_ROOT $ENV{BOOST_ROOT})
find_package(Boost 1.55 REQUIRED COMPONENTS thread filesystem log system)
include_directories(
SYSTEM ${Boost_INCLUDE_DIRS}
)
add_executable(test test.cpp)
target_link_libraries( test AppLib )
TEST.CPP
#include "testVersion.hpp"
int main() {
std::cout << "Main call Using Boost "
<< BOOST_VERSION / 100000 << "." // major version
<< BOOST_VERSION / 100 % 1000 << "." // minior version
<< BOOST_VERSION % 100 // patch level
<< std::endl;
std::cout << "library : " << std::endl;
Version v;
v.callVersion();
}
testVersion
#include "testVersion.hpp"
void Version::callVersion()
{ std::cout << "Using Boost "
<< BOOST_VERSION / 100000 << "." // major version
<< BOOST_VERSION / 100 % 1000 << "." // minior version
<< BOOST_VERSION % 100 // patch level
<< std::endl;
}
testVersion.hpp
#include <boost/version.hpp>
class Version
{
public:
void callVersion();
};
如果我這樣做,它將運行良好:輸出:
Main call Using Boost 1.55.0
Using Boost 1.57.0
但是,當我消除了testVersion.cpp文件並內聯callVersion時,我得到了輸出:
Main call Using Boost 1.55.0
library :
Using Boost 1.55.0
因為僅當我在源文件中包含boost時,編譯器才將標頭用於boost 1.55,所以他采用1.57。 我該如何解決? 那可能嗎?
結論:我需要一個空標題:
all_boost_includes.hpp
與all_boost_includes.cpp
#include "boost..."
#include ...
其中將僅包括所有boost標頭。 然后,我必須在應用程序的每個標題中都包含此標題。 那是對的嗎?
這類似於預編譯的boost頭還是?
我試過包含“ boost_headers.hpp”,它是空的,並且具有一個boost_header.cpp,其中包括boost版本
我在應用程序cmake上添加了ADD_LIBRARY(AppLib SHARED boost_headers.cpp)
但是當我嘗試
#include "precompiled_boost.hpp"
#include <fstream>
#include <iostream>
class Version
{
public:
void callVersion(){
std::cout << "Using Boost "
<< BOOST_VERSION / 100000 << "." // major version
<< BOOST_VERSION / 100 % 1000 << "." // minior version
<< BOOST_VERSION % 100 // patch level
<< std::endl;
}
};
他不知道BOOST_VERSION。 我要在那里做什么? 如果我包含precompiled_boost.cpp,則會得到錯誤的輸出
編譯庫編譯庫中Version::callVersion
代碼。 當庫使用boost 1.55 Version::callVersion
將返回1.55
。
編譯庫不會編譯庫中 Version::callVersion
代碼,因為它將雙向內聯! 您的Version::callVersion
實際上將在test
側進行編譯。 當test
使用Boost 1.57時, Version::callVersion
將返回1.57
您不應該內聯您的電話。 此外,在使用不同版本的boost的應用程序和庫項目中,您都不能使用在其聲明中引用任何boost的類。 如果有必要,您應該考慮創建一些代理對象或函數,以便將所有boost內容封裝在您的庫中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.