安裝工具:sudo apt-get install gyp
1. 簡單一實例
hello.c
#include <stdio.h>int main(){ printf("hello gyp\n"); return 0;}main.gyp
{ 'targets': [ { 'target_name': 'hello', 'type': 'executable', 'sources': [ 'hello.c', ], }, ], }構建
gyp --depth=./ main.gyp
編譯
make
運行
./hello
hello gyp
2. 改進
執行個體1中整個檔案夾都是比較淩亂的,所以做一個genPrj.sh指令碼
#!/bin/bashgyp --depth=./ --generator-output=./build main.gypif [ -d build ]; then cd build makefi
所有產生非源碼檔案都給產生到了build目錄下,看起來就比較乾淨了, 再做一個清除指令碼,清除就更省事了,do_clean.sh如下:
#!/bin/bashrm build -rf
3. c++執行個體
目錄結構:
├── do_clean.sh├── genPrj.sh└── src ├── hello.cc ├── main.gyp └── my_class ├── my_class.cc └── my_class.h
genPrj.sh
#!/bin/bashgyp --depth=. --generator-output=build src/main.gypif [ -d build ]; then cd build makefi
hello.cc
#include <stdio.h> #include "my_class/my_class.h" int main(int argc, char** argv) { printf("hello world\n"); MyClass my_class(100); my_class.Fun1(); }main.gyp
{ 'targets': [ { 'target_name': 'my_class', 'type': 'executable', 'sources': [ 'hello.cc', 'my_class/my_class.h', 'my_class/my_class.cc', ], }, ], } my_class.cc
#include "my_class.h" #include <stdio.h> void MyClass::Fun1() { printf("the value is %d\n", value_); }
my_class.h
class MyClass { public: MyClass(int value) : value_(value) {} void Fun1(); private: int value_; };
4. 使用ninja編譯
注意:使用sudo apt-get install ninja安裝的沒法用法,需要使用depot_tools, 解壓後配置depot_too路徑:
$ export PATH=`pwd`/depot_tools:"$PATH"
$ vim ~/.bashrc // 在檔案最後添加 export PATH=`pwd`/depot_tools:"$PATH" 儲存在退出。 $ ninja --version //查看版本號碼
目錄結構:
├── do_clean.sh├── genPrj.sh├── hello.c└── main.gyp
genPrj.sh
#!/bin/bashgyp --depth=. --format=ninja --generator-output=build main.gypif [ -d build ]; then cd build ninja -C out/Default echo "run app:" ./out/Default/hellofi
運行結果:
./genPrj.sh
ninja: Entering directory `out/Default'
[2/2] LINK hello
run app:
hello gyp
總結:
gyp 類似於cmake, 而ninja則類似make, 現在使用cmake和make的要多於使用gyp和ninja