【CMakeを使用したC++エンジニアリング】1:CMake Hello World

9985 ワード

前言
本文はCMakeを用いてHello WorldのC++プロジェクトを構築する方法を紹介した.
CMake使用慣例
CMakeコンストラクションプロジェクトを使用したことのある方はご存知ですが、CMakeの使用には次のような「慣例」があります.
  • プロジェクトルートディレクトリにビルドディレクトリを作成する:mkdir build && cd build
  • 実行:cmake ../
  • Makefileの生成に成功したと判断し、実行make(または、Visual StudioプロジェクトファイルまたはXcodeプロジェクトファイルなどの生成したエンジニアリングファイルを開く)
  • まねる
    次にHello Worldを構築するのもこの手順に従ってCMakeを使用して構築します.
    現在のディレクトリをtestと仮定
    ステップ1:新規Hello.cpp
    #include <iostream>
    int main() {
        std::cout << "hello cmake" << std::endl;
        return 0;
    }

    ステップ2:新規CMakeLists.txt
    add_executable(hello hello.cpp)

    現在のディレクトリ構造は次のとおりです.
    –test/
    —-hello.cpp
    —-CMakeLists.txt
    ステップ3:慣例メソッドを使用して構築
    1.buildディレクトリの作成
    –test/
    —-hello.cpp
    —-CMakeLists.txt
    —-build/
    2.buildディレクトリに入り、実行cmake ../出力:
    ➜  /Users/sunyongjian1/codes/local_codes/cmake_test/build cmake ..
    
    -- The C compiler identification is AppleClang 7.3.0.7030029 -- The CXX compiler identification is AppleClang 7.3.0.7030029 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Configuring done -- Generating done -- Build files have been written to: /Users/sunyongjian1/codes/local_codes/cmake_test/build

    ビルドディレクトリの下の内容を表示します.
    total 56
    drwxr-xr-x   6 lina  staff   204B  4  5 21:52 .
    drwxr-xr-x   5 lina  staff   170B  4  5 21:52 ..
    -rw-r--r--   1 lina  staff    12K  4  5 21:52 CMakeCache.txt
    drwxr-xr-x  12 lina  staff   408B  4  5 21:52 CMakeFiles
    -rw-r--r--   1 lina  staff   4.8K  4  5 21:52 Makefile
    -rw-r--r--   1 lina  staff   1.3K  4  5 21:52 cmake_install.cmake

    生成されたMakefileが見えますが、
    3.実行make:
    ➜  /Users/sunyongjian1/codes/local_codes/cmake_test/build make
    Scanning dependencies of target hello
    [100%] Building CXX object CMakeFiles/hello.dir/hello.cpp.o
    Linking CXX executable hello
    [100%] Built target hello

    buildディレクトリの下の内容を再度表示すると、生成されたhelloファイルが表示されます.
    ➜  /Users/sunyongjian1/codes/local_codes/cmake_test/build l
    total 88
    drwxr-xr-x   7 lina  staff   238B  4  5 21:54 .
    drwxr-xr-x   5 lina  staff   170B  4  5 21:52 ..
    -rw-r--r--   1 lina  staff    12K  4  5 21:52 CMakeCache.txt
    drwxr-xr-x  12 lina  staff   408B  4  5 21:54 CMakeFiles
    -rw-r--r--   1 lina  staff   4.8K  4  5 21:52 Makefile
    -rw-r--r--   1 lina  staff   1.3K  4  5 21:52 cmake_install.cmake
    -rwxr-xr-x   1 lina  staff    15K  4  5 21:54 hello

    4.運転./hello:
    出力:hello cmake.
    ➜  /Users/sunyongjian1/codes/local_codes/cmake_test/build ./hello
    hello cmake

    まとめ
    以上、CMakeを使用してHello World C++プロジェクトを構築する過程で、自分でMakefileを作成するよりも、CMakeを使用するのは簡単で、一言だけ必要です.add_executable(hello, hello.cpp)
    できました.
    作者のレベルは有限で、関连する知识の理解と総括に対してどうしても间违いがあって、また指摘することを望んで、とても感谢します!
    githubブログ、CSDNブログ、ようこそ