Mencoba Cmake ( a Cmake intro )

Visual Studio adalah favorite ide pemrograman saya. Sudah sejak lama saya pakai, kira-kira sejak 4 tahun yang lalu. Mungkin menjadi favorite karena kebiasaan :) dan juga visual studio mudah dipakai. Walaupun kadang ada juga bugs ‘aneh’ yang terjadi selama nge-build project dengan VS. Sayangnya, ‘build file’ dari VS tidak bisa dipakai di environment lain, misalnya mau build pakai gcc di Linux. Oleh karena itulah harus ada cara lain yang harus dipakai. Berikut sebagai contoh case saya :

Misalnya saya memiliki _super advanced top secret _project dengan informasi sebagai berikut :

[1] struktur file-file project :

              ->project_root_dir

              ->HelloSource.cpp file

              ->PrintLib dir

                      ->PrintHello.h file

             ->PrintHello.cpp file

[2] project diatas akan di-build dengan output sebagai berikut :

           -HelloSource.cpp —> executable file , yang menggunakan PrintLib (static linked)

           -PrintLib —> static library

          -Penggunaan PrintLib bisa di ON-OFF melalui macro ( o yeah, macro define, I like it )

[3] source code : HelloSource.cpp

          /* hello apps project */

          #include

          #include “HelloVersion.h”

          #ifdef USE_PRINTLIB

          #include “PrintHello.h”

          #endif

          using namespace std;

          int main(char* argc, char** argv)

          {

                   cout << “hello ” << endl;

                   cout <<” hello version : “<< Hello_VERSION_MAJOR <<“-”<<Hello_VERSION_MINOR<<endl;

                   #ifdef USE_PRINTLIB

                   PrintHelloFunc();

                   #endif

                   return 0;

          }

[4] source code : PrintHello.h

          #include

          void PrintHelloFunc();

[5] source code : PrintHello.cpp

          #include “PrintHello.h”

          void PrintHelloFunc()

          {

                   std::cout << “ hey hello i’m here. printed from hellolib ” << std::endl;

          }

Pertanyaannya, Bagaimana agar project diatas bisa build-able beberapa platform dengan beda compiler ?

Banyak cara, dari cara manual ( buat build script untuk masing-masing platform ) atau dengan tools misalnya : autotools, scons, jam, waf, cmake.yang terakhir: cmake, saya sering mem-build project dari internet yang menggunakan cmake, jadi lebih kenal dengan yang satu ini. Kenyataannya cmake sudah secara luas digunakan di opensource project sejak lama. Build sistem yang lain yang pernah saya coba, dan sangat mudah (dari segi user yang akan mem-build project tersebut) adalah build sistem dari boost library : boost-jam . sangat mudah nge-buildnya.

Kali ini saya tertarik mempelajari bagaimana membuat cmake file. cmake file itu semacam “template” make file atau meta make file. cmake file adalah file text yang berisi deskripsi project dengan syntax cmake dan disimpan dengan nama CMakeLists.txt. Kalau cmake file suatu project sudah dibuat, file ini bisa digunakan untuk meng-generate real make file yang bisa dipakai compiler. tergantung compiler apa yang dipakai . beberapa yang didukung :

          -Visual C++ ( sln & vcxproj ),

          -Kdevelop3, Eclipse, XCode,

          -makefiles (Unix,NMake, Borland, Watcom, MinGW, MSYS,Cygwin)

          -Code::Blocks

Dari daftar diatas, yang pernah saya coba adalah generate VS (solution & project) dan makefile untuk linux.

Dari deskripsi project diatas saya bisa membuat cmake file dengan struktur :

          ->project_root_dir

          ->CMakeLists.txt file                   cmake ke-1 (main)

          ->PrintLib dir

                   ->CMakeLists.txt file          cmake ke-2

          ->HelloVersion.h.in file untuk setting Define

 source file CMakeLists.txt ke-2 : ( hanya 1 baris )

          add_library(PrintLib PrintHello.cpp)

source file CMakeLists.txt ke-1 :

          #this is a cmake comment

          cmake_minimum_required (VERSION 2.6)

          #project name

          project (Hello)

          #version

          set (Hello_VERSION_MAJOR 1)

          set (Hello_VERSION_MINOR 0)

          #option can be turned on/off when generate real make file

          option ( USE_PRINTLIB “use print library” ON )

          configure_file (

                   “${PROJECT_SOURCE_DIR}/HelloVersion.h.in”

                   “${PROJECT_BINARY_DIR}/HelloVersion.h”

          )

          include_directories (“${PROJECT_BINARY_DIR}“)

          if (USE_PRINTLIB)

                   include_directories (“${PROJECT_SOURCE_DIR}/PrintLib”)

                   add_subdirectory(PrintLib)

                   set (EXTRA_LIBS ${EXTRA_LIBS} PrintLib)

          endif (USE_PRINTLIB)

          #tell cmake that Hello is executable

          add_executable(Hello HelloSource.cpp)

          #tell cmake that Hello linked with extralibs ( printlib )

          target_link_libraries(Hello ${EXTRA_LIBS})

          install(TARGETS Hello DESTINATION bin)

source file HelloVersion.h.in :

          // version for Hello project

          #define Hello_VERSION_MAJOR @Hello_VERSION_MAJOR@

          #define Hello_VERSION_MINOR @Hello_VERSION_MINOR@

          #cmakedefine USE_PRINTLIB

file tambahan adalah HelloVersion.h.in untuk mengatur version dan macro define. Kalau dibaca deskripsi output project saya diatas dan apa yang ada di cmakelist.txt semuanya tampak self explanatory. Dari CMakeLists.txt ini kita bisa meng-_g_enerate solution & project di windows & makefile di linux ( sebagai 2 test case untuk mencoba ) . di Ms-Windows bisa memakai cmake GUI.

[caption id=“attachment_970” align=“aligncenter” width=“300”]cmake_flow cmake_flow[/caption]

Cmake file yang dipakai diatas adalah yang paling sederhana. Terdapat beberapa perintah yang lain, misalnya find_package, find_library, export, dan lainnya .

edie // 13022015 // Jakarta

comments powered by Disqus