Ubuntu下gyp简单入门实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenyefei/article/details/51898656

安装工具: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/bash

gyp --depth=./ --generator-output=./build main.gyp

if [ -d build ]; then
  cd build
  make
fi
所有生成非源码文件都给生成到了build目录下,看起来就比较干净了, 再做一个清除脚本,清除就更省事了,do_clean.sh如下:
#!/bin/bash

rm 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/bash

gyp --depth=. --generator-output=build src/main.gyp

if [ -d build ]; then
  cd build
  make
fi
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/bash

gyp --depth=. --format=ninja --generator-output=build main.gyp

if [ -d build ]; then
  cd build
  ninja -C out/Default
  echo "run app:"
  ./out/Default/hello
fi

运行结果:

./genPrj.sh 
ninja: Entering directory `out/Default'
[2/2] LINK hello
run app:
hello gyp

总结:

gyp 类似于cmake, 而ninja则类似make, 现在使用cmake和make的要多于使用gyp和ninja





猜你喜欢

转载自blog.csdn.net/chenyefei/article/details/51898656