RealPath:
WebPath:
2017/02/12 02:24 (JST) 更新
MinGW on MSYS >>

BoostをリンクするCMakeプロジェクト

Contents

概要

Boost に依存(ライブラリにリンクする)する CMake プロジェクトを作成する。

前提

Boost 設置およびビルドを済ませておく。環境変数 BOOST_ROOT の設定を忘れないこと。

サンプルプロジェクト

main.cpp
#include <stdio.h>
#include <unistd.h>
#include <boost/thread.hpp>

void ThreadFunc(const char *str)
{
    for(int i = 0; i < 5; i++){
        printf("%s %d, ", str, i);
        sleep(0.3);
    }
}

int main() {
    boost::thread threadA(boost::bind(&ThreadFunc, "A"));
    boost::thread threadZ(boost::bind(&ThreadFunc, "Z"));
    threadA.join();
    threadZ.join();
    std::cin.get();
    return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.7)

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED thread)
if(Boost_FOUND)
    message(STATUS "!!! BOOST FOUND !!!")
else()
    message(STATUS "!!! BOOST NOT FOUND !!!")
endif()

ADD_EXECUTABLE(ThreadSample main.cpp)
TARGET_LINK_LIBRARIES(ThreadSample ${Boost_LIBRARIES})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})

cmake 実行

dos
> cmake -G "MSYS Makefiles"
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Boost version: 1.63.0
-- Found the following Boost libraries:
--   thread
--   chrono
--   system
--   date_time
--   atomic
-- !!! BOOST FOUND !!!
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Projects/ThreadSample

make 実行

dos
> make
Scanning dependencies of target ThreadSample
[ 50%] Building CXX object CMakeFiles/ThreadSample.dir/main.cpp.obj
[100%] Linking CXX executable ThreadSample.exe
[100%] Built target ThreadSample

make 成果物の実行確認

dos
> ThreadSample.exe
A 0, Z 0, A 1, Z 1, A 2, Z 2, A 3, Z 3, A 4, Z 4,

cmake 作業のやり直しについて

前回の作業キャッシュが残ってると変な参照しちゃうことがあるので以下のようにキャッシュファイルは適宜削除すると良い。

dos
> rm -f CMakeCache.txt && rm -rf CMakeFiles

cmake 変数内容の出力

以下のように cmake 引数に -LAH オプションを付けると変数内容を詳細に確認できる。

> cmake -LAH -G "MSYS Makefiles"
....
-- Build files have been written to: C:/Projects/ThreadSample
-- Cache values
// Boost atomic library (debug)
Boost_ATOMIC_LIBRARY_DEBUG:FILEPATH=C:/boost_1_63_0/stage/lib/libboost_atomic-mgw53-mt-d-1_63.a

// Boost atomic library (release)
Boost_ATOMIC_LIBRARY_RELEASE:FILEPATH=C:/boost_1_63_0/stage/lib/libboost_atomic-mgw53-mt-1_63.a

....

// Path to a file.
Boost_INCLUDE_DIR:PATH=C:/boost_1_63_0

....

// Boost thread library (debug)
Boost_THREAD_LIBRARY_DEBUG:FILEPATH=C:/boost_1_63_0/stage/lib/libboost_thread-mgw53-mt-d-1_63.a

// Boost thread library (release)
Boost_THREAD_LIBRARY_RELEASE:FILEPATH=C:/boost_1_63_0/stage/lib/libboost_thread-mgw53-mt-1_63.a

// Path to a program.
CMAKE_AR:FILEPATH=C:/MinGW/bin/ar.exe

....

// Flags used by the compiler during debug builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=-g

// Flags used by the compiler during release builds for minimum size.
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG

// Flags used by the compiler during release builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG

// Flags used by the compiler during release builds with debug info.
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

// Libraries linked by default with all C++ applications.
CMAKE_CXX_STANDARD_LIBRARIES:STRING=-lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32

// C compiler
CMAKE_C_COMPILER:FILEPATH=C:/MinGW/bin/gcc.exe

....

// Flags used by the compiler during release builds with debug info.
CMAKE_C_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

// Libraries linked by default with all C applications.
CMAKE_C_STANDARD_LIBRARIES:STRING=-lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32

....

// Path to a program.
CMAKE_LINKER:FILEPATH=C:/MinGW/bin/ld.exe

// Path to a program.
CMAKE_MAKE_PROGRAM:FILEPATH=C:/MinGW/msys/1.0/bin/make.exe

....

// Path to a program.
CMAKE_RANLIB:FILEPATH=C:/MinGW/bin/ranlib.exe

....

make 経過の冗長出力

以下のように make 引数に VERBOSE=1 オプションを付けるとビルド経過を詳細に確認できる。

dos
> make VERBOSE=1
"/C/Program Files/CMake/bin/cmake.exe" -H/C/Projects/ThreadSample -B/C/Projects/ThreadSample --check-build-system CMakeFiles/Makefile.cmake 0
"/C/Program Files/CMake/bin/cmake.exe" -E cmake_progress_start /C/Projects/ThreadSample/CMakeFiles /C/Projects/ThreadSample/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/c/Projects/ThreadSample'
make -f CMakeFiles/ThreadSample.dir/build.make CMakeFiles/ThreadSample.dir/depend
make[2]: Entering directory `/c/Projects/ThreadSample'
"/C/Program Files/CMake/bin/cmake.exe" -E cmake_depends "MSYS Makefiles" /C/Projects/ThreadSample /C/Projects/ThreadSample /C/Projects/ThreadSample /C/Projects/ThreadSample /C/Projects/ThreadSample/CMakeFiles/ThreadSample.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/c/Projects/ThreadSample'
make -f CMakeFiles/ThreadSample.dir/build.make CMakeFiles/ThreadSample.dir/build
make[2]: Entering directory `/c/Projects/ThreadSample'
[ 50%] Building CXX object CMakeFiles/ThreadSample.dir/main.cpp.obj
/C/MinGW/bin/g++.exe    @CMakeFiles/ThreadSample.dir/includes_CXX.rsp  -o CMakeFiles/ThreadSample.dir/main.cpp.obj -c /C/Projects/ThreadSample/main.cpp
[100%] Linking CXX executable ThreadSample.exe
"/C/Program Files/CMake/bin/cmake.exe" -E remove -f CMakeFiles/ThreadSample.dir/objects.a
/C/MinGW/bin/ar.exe cr CMakeFiles/ThreadSample.dir/objects.a @CMakeFiles/ThreadSample.dir/objects1.rsp
/C/MinGW/bin/g++.exe     -Wl,--whole-archive CMakeFiles/ThreadSample.dir/objects.a -Wl,--no-whole-archive  -o ThreadSample.exe -Wl,--out-implib,libThreadSample.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles/ThreadSample.dir/linklibs.rsp
make[2]: Leaving directory `/c/Projects/ThreadSample'
[100%] Built target ThreadSample
make[1]: Leaving directory `/c/Projects/ThreadSample'
"/C/Program Files/CMake/bin/cmake.exe" -E cmake_progress_start /C/Projects/ThreadSample/CMakeFiles 0

make 成果物のクリーン

dos
> make clean