Ubuntu 中编译boost 1.74.0 & 测试验证 & cmake & Conan设置

 

下载编译

apt-get update
apt-get install -y mpi-default-dev libicu-dev python-dev python3-dev libbz2-dev zlib1g-dev

wget https://dl.bintray.com/boostorg/release/1.74.0/source/boost_1_74_0.tar.gz

tar -zxvf boost_1_74_0.tar.gz
cd boost_1_74_0
./bootstrap.sh

#./b2 -j10 cflags=-fPIC cxxflags=-fPIC install --build-type=complete --layout=versioned threading=multi --prefix="/usr/lib/boost-1.74"

./b2 -j10 cflags=-fPIC cxxflags=-fPIC install --build-type=complete --layout=versioned threading=multi --prefix="/usr/lib/boost-1.74" --without-python --without-graph --without-graph_parallel --without-mpi --without-wave --without-test --without-log

测试程序

#include 

#include 
#include 
#include 

boost::mutex mutex;
void wait(int seconds)
{
    boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}


void print_block(int n, char c)
{
    // critical section (exclusive access to std::cout signaled by locking mtx):
    mutex.lock();
    for (int i = 0; i < n; ++i)
    {
        wait(1);
        std::cout << c;
    }
    std::cout << '\n';
    mutex.unlock();
}

int main(int argc, char* argv[])
{
    int num = 10;
    boost::thread thread1(&print_block, num, '*');
    boost::thread thread2(&print_block, num, '$');

    thread1.join();
    thread2.join();

    return 0;
}

编译:

c++ -I /usr/lib/boost-1.74/include main.cpp -o main -L /usr/lib/boost-1.74/lib/release -lboost_thread-gcc7-mt-x64-1_74 -lpthread

结果:

root@c5987ac554e3:/app/boost/example# ./main
**********
$$$$$$$$$$

去掉mutex的结果:

 

root@c5987ac554e3:/app/boost/example# ./main
*$*$*$*$*$*$*$*$*$*
$

 

注意:

安装编译好库,存在版本冲突:

apt-get install libboost-all-dev
root@c5987ac554e3:/app/boost/example/cmake-test/build# ls -lh /usr/lib/x86_64-linux-gnu/libboost_thread.so
lrwxrwxrwx 1 root root 25 Mar  6  2018 /usr/lib/x86_64-linux-gnu/libboost_thread.so -> libboost_thread.so.1.65.1
export BOOST_ROOT=/usr/lib/boost-1.74/

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(mutex_project)

SET(CMAKE_CXX_FLAGS "-g -w -O2")
#set (Boost_NO_BOOST_CMAKE ON) #区别在于使用cmake FindBoost.cmake 还是Boost自带的

#default binary and lib path
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR})
#begin to set boost static library
INCLUDE_DIRECTORIES(/usr/lib/boost-1.74/include)
set(Boost_USE_STATIC_LIBS ON)
set(BOOST_COMPONENTS thread)
find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS})

ADD_EXECUTABLE(helloboost main.cpp)
TARGET_LINK_LIBRARIES(helloboost ${Boost_LIBRARIES})

注意:lib需要存在 /usr/lib/boost-1.74/lib/路径下

export BOOST_ROOT=/usr/lib/boost-1.74/

cmake 3.10版本错误

set (Boost_NO_BOOST_CMAKE ON)的情况下 cmake错误

vim /usr/share/cmake-3.10/Modules/FindBoost.cmake

最高支持1.64.1版本

 set(_Boost_KNOWN_VERSIONS ${Boost_ADDITIONAL_VERSIONS}
    "1.65.1" "1.65.0" "1.65"
    "1.64.0" "1.64" "1.63.0" "1.63" "1.62.0" "1.62" "1.61.0" "1.61" "1.60.0" "1.60"
    "1.59.0" "1.59" "1.58.0" "1.58" "1.57.0" "1.57" "1.56.0" "1.56" "1.55.0" "1.55"
    "1.54.0" "1.54" "1.53.0" "1.53" "1.52.0" "1.52" "1.51.0" "1.51"
    "1.50.0" "1.50" "1.49.0" "1.49" "1.48.0" "1.48" "1.47.0" "1.47" "1.46.1"
    "1.46.0" "1.46" "1.45.0" "1.45" "1.44.0" "1.44" "1.43.0" "1.43" "1.42.0" "1.42"
    "1.41.0" "1.41" "1.40.0" "1.40" "1.39.0" "1.39" "1.38.0" "1.38" "1.37.0" "1.37"
    "1.36.1" "1.36.0" "1.36" "1.35.1" "1.35.0" "1.35" "1.34.1" "1.34.0"
    "1.34" "1.33.1" "1.33.0" "1.33")

升级cmake版本:

https://askubuntu.com/questions/829310/how-to-upgrade-cmake-in-ubuntu

 

Conan设置

tree

root@ubuntu:/app/boost/example/mutex# tree
.
|-- CMakeLists.txt
|-- conanfile.txt
`-- mutex
    |-- CMakeLists.txt
    `-- main.cpp

install

conanfile.txt

[requires]
boost/1.74.0@Common/stable
[generators]
cmake
conan install conanfile.txt -s arch=x86_64 -s os=Linux -r cloud --update

项目CMakeLists.txt 

cmake_minimum_required(VERSION 3.1.0)
project(mutexRoot)

cmake_policy(SET CMP0043 NEW)

add_definitions("-std=c++11")
add_definitions(-DCHARLS_STATIC)

if (UNIX)
        set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif (UNIX)

include(conanbuildinfo.cmake)
conan_basic_setup()

add_subdirectory(mutex)

工程CMakeLists.txt

# cmake needs this line
cmake_minimum_required(VERSION 2.8)

# Define project name
project(mutex_project)


FILE(GLOB SOURCE_FILES "*.cpp")

## System dependencies are found with CMake's conventions
find_package(Boost REQUIRED COMPONENTS
    thread
)

if(NOT Boost_FOUND)
    message("NOT found Boost")
endif()

include_directories(${Boost_INCLUDE_DIRS})
message("${Boost_INCLUDE_DIRS}")
message("${Boost_LIBRARIES}")

# Declare the executable target built from your sources
add_executable(${PROJECT_NAME} ${SOURCE_FILES})

target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})

 

 

你可能感兴趣的:(Linux,c++,boost)