GN_1_在Ubuntu22.04安装GN 中已经介绍过GN的安装。下面我们使用GN来编译自己写的helloworld程序。
复制例子simple_build中的 .gnbuild到你自己新建的目录假设为test。

cp -rvf .gn buld  /path/to/test

创建hello.cc

#include <iostream>
int main(void)
{std::cout << "Hello World" << std::endl;return 0;
}

创建BUILD.gn

executable("hello") {sources = ["hello.cc",]
}

编译:

gn gen out
ninja -C out

执行程序:

./out/hello

执行过程:
gn gen out 命令会去找当前目录下的.gn文件。
.gn内容

# The location of the build configuration file.
buildconfig = "//build/BUILDCONFIG.gn"
  • // 表示.gn所在路径,这里因为.gn和build目录在同一路径下, 所以在build前面加//

看一下BUILDCONFIG.gn

# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.if (target_os == "") {target_os = host_os
}
if (target_cpu == "") {target_cpu = host_cpu
}
if (current_cpu == "") {current_cpu = target_cpu
}
if (current_os == "") {current_os = target_os
}is_linux = host_os == "linux" && current_os == "linux" && target_os == "linux"
is_mac = host_os == "mac" && current_os == "mac" && target_os == "mac"# All binary targets will get this list of configs by default.
_shared_binary_target_configs = [ "//build:compiler_defaults" ]# Apply that default list to the binary target types.
set_defaults("executable") {configs = _shared_binary_target_configs# Executables get this additional configuration.configs += [ "//build:executable_ldconfig" ]
}
set_defaults("static_library") {configs = _shared_binary_target_configs
}
set_defaults("shared_library") {configs = _shared_binary_target_configs
}
set_defaults("source_set") {configs = _shared_binary_target_configs
}set_default_toolchain("//build/toolchain:gcc")

可以看到里面指定编译时的系统,CPU,编译默认选项,源文件,可执行文件,静态库和动态库 的配置;当然还有吧编译工具//build/toolchain:gcc

先看一下编译默认选项它是在//build/BUILD.gn里面定义的:

# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.config("compiler_defaults") {if (current_os == "linux") {cflags = ["-fPIC","-pthread",]}
}config("executable_ldconfig") {if (!is_mac) {ldflags = ["-Wl,-rpath=\$ORIGIN/","-Wl,-rpath-link=",]}
}

那再看一下 //build/toolchain:gcc,看了toolchain里面只有一个BUILD.gn文件,内容如下:

# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.toolchain("gcc") {tool("cc") {depfile = "{{output}}.d"command = "gcc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"depsformat = "gcc"description = "CC {{output}}"outputs =[ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]}tool("cxx") {depfile = "{{output}}.d"command = "g++ -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"depsformat = "gcc"description = "CXX {{output}}"outputs =[ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]}tool("alink") {command = "rm -f {{output}} && ar rcs {{output}} {{inputs}}"description = "AR {{target_output_name}}{{output_extension}}"outputs =[ "{{target_out_dir}}/{{target_output_name}}{{output_extension}}" ]default_output_extension = ".a"output_prefix = "lib"}tool("solink") {soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".sofile = "{{output_dir}}/$soname"rspfile = soname + ".rsp"if (is_mac) {os_specific_option = "-install_name @executable_path/$sofile"rspfile_content = "{{inputs}} {{solibs}} {{libs}}"} else {os_specific_option = "-Wl,-soname=$soname"rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}"}command = "g++ -shared {{ldflags}} -o $sofile $os_specific_option @$rspfile"description = "SOLINK $soname"# Use this for {{output_extension}} expansions unless a target manually# overrides it (in which case {{output_extension}} will be what the target# specifies).default_output_extension = ".so"# Use this for {{output_dir}} expansions unless a target manually overrides# it (in which case {{output_dir}} will be what the target specifies).default_output_dir = "{{root_out_dir}}"outputs = [ sofile ]link_output = sofiledepend_output = sofileoutput_prefix = "lib"}tool("link") {outfile = "{{target_output_name}}{{output_extension}}"rspfile = "$outfile.rsp"if (is_mac) {command = "g++ {{ldflags}} -o $outfile @$rspfile {{solibs}} {{libs}}"} else {command = "g++ {{ldflags}} -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group {{libs}}"}description = "LINK $outfile"default_output_dir = "{{root_out_dir}}"rspfile_content = "{{inputs}}"outputs = [ outfile ]}tool("stamp") {command = "touch {{output}}"description = "STAMP {{output}}"}tool("copy") {command = "cp -af {{source}} {{output}}"description = "COPY {{source}} {{output}}"}
}

看这个文件可以大概知道它是做编译,链接用的。

GN_2_使用GN编译自己写的程序相关推荐

  1. ubuntu 安装libusb 编译自己写的程序 发现很多undefined(排除包含头文件的问题)

    链接的头文件(贴了部分) /usr/include/libusb-1.0/libusb.h \/usr/lib/gcc/x86_64-linux-gnu/9/include/stdint.h /usr ...

  2. F28379D烧写双核程序(在线离线)

    F28379D烧写双核程序(在线&离线) 文章信息 开发环境 烧写双核程序前需知 1. 在线 1.1 编译烧写CPU1程序到F28379D中 1.2 编译CPU2程序,把生成的``.out`` ...

  3. python写小程序-用python写个简单的小程序,编译成exe跑在win10上

    每天的工作其实很无聊,早知道应该去IT公司闯荡的.最近的工作内容是每逢一个整点,从早7点到晚11点,去查一次客流数据,整理到表格中,上交给素未蒙面的上线,由他呈交领导查阅. 人的精力毕竟是有限的,所以 ...

  4. 开源RISC-V处理器(蜂鸟E203)学习(五)A100T-FPGA 移植蜂鸟Hbirdv2,实现Centos下调试器USB识别以及程序编译烧写,并进行C语言仿真

    1.简述 最近购买了一块适合做原型验证FPGA板卡,板卡接口和外设比较丰富,十分适合跑一些小型的SOC工程,比如蜂鸟E203:板卡自带FPGA烧写器和软核CPU的JATG调试器,还有USB接口的UAR ...

  5. 在Linux系统下编译并执行C++程序

    引言 为什么要在Linux下写程序? 首先要问一下自己,为什么要写这个程序. 如果我们写一个运行在windows平台的应用程序,那么用Linux写肯定是傻瓜或者强迫症,因为在windows平台调试wi ...

  6. 代码编辑神器VIM(附我写acm程序时的配置)(转)

    本文转自:http://blog.csdn.net/dinosoft/article/details/6330121 有些牛X的人性格会比较古怪,VIM就是这么一位特立独行,难以琢磨的怪客.但如果熟悉 ...

  7. Discuz验证码识别(上线篇)-写给程序员的TensorFlow教程

    经过前两篇文章的开发,咱们今天终于要进入令人激动的上线篇了.(最近刚刚发布的TensorFlow lite其实也是部署上线的工具集之一)话说我在学习TensorFlow的时候,发现这部分的教程是尤其少 ...

  8. 控制台编写JAVA程序教程_写一个java程序的步骤是什么?写java程序技巧

    写Java程序是要按照步骤来的,这样才能写好一个java程序,那么接下来,我们就来给大家讲解一下写一个java程序的步骤是什么? (1)创建Java项目:"FileàNewàProjectà ...

  9. linux系统怎样写单片机程序,单片机知识是Linux驱动开发的基础之一以及如何学单片机...

    这是arm裸机1期加强版第1课第2.3节课程的wiki文字版. 为什么没前途也要学习单片机? 因为它是个很好的入口. 学习单片机可以让我们抛开复杂的软件结构,先掌握硬件操作,如:看原理图.芯片手册.写 ...

最新文章

  1. 转载 CreateWaitableTimer和SetWaitableTimer函数
  2. 常用WebService一览表
  3. 0048-三角形的判断
  4. POJ - 1743 Musical Theme(二分+后缀数组+差分数组)
  5. [洛谷P1439]排列LCS问题
  6. 超过1w的Github Star大佬和他们的公众号,太强了!
  7. 【lua学习】2.数据类型
  8. c++整理--虚函数
  9. 怎样用Java 8优雅的开发业务
  10. Wpf ListBox数据绑定实例1--绑定字典集合
  11. java求立方体表面积和体积_有关正方体、长方体表面积和体积的计算题
  12. 狮子鱼社区团购小程序独立版 安装教程及后台设置小程序APPID,微信支付,腾讯地图AppKey
  13. 【STM32】红外对射计数器计数原理和代码
  14. React组件通信-父子组件间的通信
  15. lr各种问题以及解决办法
  16. INI配置——《跟我学Shiro》
  17. while、dowhile和for循环
  18. 在 Mac 上的 Safari 中下载时遇到问题?故障排除和修复的方法
  19. 集成 Jira 与钉钉和企业微信通知
  20. 搜狐畅游2021届校园招聘简章

热门文章

  1. 与、或、非、异或,运算符巩固
  2. 如何收看2020年夏季东京奥运会?
  3. [洛谷]P2404 自然数的拆分问题
  4. web分享QQ空间api接口
  5. 函数分离常数法 oracle,函数值域之《分离常数法》正确打开方式
  6. 什么是游戏美术设计?3D游戏建模常用的软件有哪些
  7. Unity 3D 中的专业“术语表”。
  8. Centos 7 怎么都连不上手机阿阿阿阿Android Studio 怎么都检测不到真机啊还有关于git本地提交就缺少文件啊啊啊啊
  9. 公司发的电脑无法登录OneNote账户
  10. Django解决css样式失效问题最终方法