Ubuntu下gyp簡單入門執行個體_Ubuntu

來源:互聯網
上載者:User

安裝工具: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





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.