本文共 2109 字,大约阅读时间需要 7 分钟。
安装工具:sudo apt-get install gyp
1. 简单实例
hello.c
#includemain.gypint main(){ printf("hello gyp\n"); return 0;}
{ '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.hgenPrj.sh
#!/bin/bashgyp --depth=. --generator-output=build src/main.gypif [ -d build ]; then cd build makefihello.cc
#includemain.gyp#include "my_class/my_class.h" int main(int argc, char** argv) { printf("hello world\n"); MyClass my_class(100); my_class.Fun1(); }
{ '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" #includevoid 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
转载地址:http://kinub.baihongyu.com/