\ 跳到主要內容

CMake vs Make

 在認識modmesh的過程中,包括modmesh本身以及延伸的小實作,幾乎處處可見make及CMake的身影;雖然對它們的認識甚少,但操作的過程中可以隱約感受到make與cmake似乎存在某種相依性,擁有類似的機制。同樣仰賴導演(make, cmake)與腳本(Makefile, CMakeList.txt),但卻做著不太一樣的事情,好像不只是差了一個C這麼簡單。

~/git-repo/importation$ ls
build  CMakeLists.txt  pybind11  test.cpp
~/git-repo/importation/build$ cmake .. 
~/git-repo/importation/build$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile  pybind11
~/git-repo/importation/build$ make

  • CMake vs make?

這裡對於make做了些許的整理,make可以用來自動化shell script。CMake之於Make,與make之於shell script,存在了一些相似性。

「CMake 用來產生跨平台的專案建置文件,在 windows 下它會生成 visual studio 的專案檔 (.sln) codeblock eclipse,在 linux 下它會生成 Makefile。」在cmake之前的環境如下:

~/git-repo/importation$ ls
CMakeLists.txt  pybind11  test.cpp
~/git-repo/importation$ ls build/
(empty)
然後創建build這個directory,這能夠幫助我們將原始文件以及CMake過程的中間產物做做區分,再進行CMake:
~/git-repo/importation$ mkdir build
~/git-repo/importation$ cd build/
~/git-repo/importation/build$ cmake ..
執行完成後,build底下就會有Makefile:
~/git-repo/importation/build$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile  pybind11


留言

這個網誌中的熱門文章

Command Line 與 Makefile

在認識到Linux以前,我鮮少接觸到CLI(Command Line Interface),也花了些許時間去熟悉。一開始看它也許會感到有些震懾,但並沒有想像中的複雜,單純是將我們習慣使用的GUI(Graphical User Interface)改成CLI,滑鼠改成鍵盤,簡單做了轉換。 sudo apt-get install qt6-default //以superuser身份安裝qt6-default mkdir build/install -p //新增資料夾 cd build/install //切換目錄到build/install rm –rf build //remove文件, recursively. makefile make 以及 makefile讓我們得以自動化我們想執行的command line。比如說modmesh裡面中clean這個target,若是藉由一行一行的command line我們會需要輸入以下的指令, rm -f $(MODMESH_ROOT)/modmesh/_modmesh$(pyextsuffix) make -C $(BUILD_PATH) clean 但如果已經在Makefile中寫好了clean這個target,將一行行的command line寫成了shell script。 .PHONY: clean clean: rm -f $(MODMESH_ROOT)/modmesh/_modmesh$(pyextsuffix) make -C $(BUILD_PATH) clean 我們就可以去藉由make,直接輸入 make clean 去執行我們想要去執行的內容,不用逐步命令。 make clean

A brief introduction to Object file

從 這篇 可以看到在vim中開啟main.o,而上圖充斥著1&0,看似截然不同的兩個東西。 main.o in vim ^?ELF^B^A^A^@^@^@^@^@^@^@^@^@^A^@>^@^A^@^@ ^@^@^@^@^@^@^@^@^@^@X^B^@^@^@^@^@^@^@^@^@^ @@^@^@^@^@^@@^@^L^@^K^@ó^O^^úUH å¸^@^@^ @^@]Ã^@GCC: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0^@^@^@^@^@^@^D^@^...... 理解兩者差異的原因,可以從兩個面向去釐清: 1. 所有的檔案都是由0&1組成的 電腦中的檔案皆是藉由0&1組成的,包括source file, object file(.o), binary file等等,但vim會將所有的binary藉由ASCII轉換成為我們所看到的plain text,這裡以一main.cpp作為例子: main.cpp in vim #define N 81 int main(){ return 0;//this is the end } main.cpp, configured with  :%! xxd -b 00000000: 00100011 01100100 01100101 01100110 01101001 01101110 #defin 00000006: 01100101 00100000 01001110 00100000 00111000 00110001 e N 81 0000000c: 00001010 01101001 01101110 01110100 00100000 01101101 .int m 00000012: 01100001 01101001 01101110 00101000 00101001 01111011 ain(){ 00000018: 00001010 00001001 01110010 01100101 01110100 01110101 ..retu 0000001e: 01110010 01101110 00100000 00110000 00111011 00101111 rn 0;/ 0000...

Write a module in C++ and use it in python

 做這個小實驗的當天看到這個梗圖,相當呼應這個主題: 「其它的程式語言對你作力學是沒用的,作計算就三本柱 Python、C++、Fortran 足矣,連 C 都沒什麼用處。」Python在計算的速度上相較於C++非常的緩慢,這個 demo 很清楚地呈現了這件事。 以計算力學為題,python在tool的開發,input的整合上具有優勢,但是在計算效能上的表現不佳,於是就有了許多與python互相支援的foreign function interface(FFI),如 swig, cython, pybind11。 截python所長,補C++所短 (or vice versa) ,似乎是做出更好的力學程式不可或缺的元素,pybind扮演的角色於是顯現。接下來就簡單用C++寫一個python的module: 1. 寫 .cpp/.h file。 #include <pybind11/pybind11.h> double square(double i){ return i*i; } PYBIND11_MODULE(test, handle){ //Module 的attributes 會在這裡做定義 handle.doc()="This is my doc"; handle.def("sqrt", &square); } 2. 寫 CMakeList.txt。 cmake_minimum_required(VERSION 3.4) PROJECT(pybind_vid) add_subdirectory(pybind11) pybind11_add_module(test test.cpp) //test為建構的module的名稱 test.cpp是它所依據的檔案 3. Build the module。 mkdir build cd build/ cmake .. make 4. 在python中使用前面所建立的module。 ~/git-repo/importation/test$ python3 >>> import test >>> dir(test) ['doc', 'file...