1、在src目录下新建一个jdtest文件夹
src\jdtest
2、在jdtest文件下添加BUILD.gn jdtest.cc
build.gn 内容如下:
# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/config/compiler/compiler.gni")
executable("jdtest") {
if (is_win && current_cpu == "x64") {
# The build infrastructure needs courgette to be named jdtest64.
output_name = "jdtest64"
}
sources = [ "jdtest.cc" ]
if (is_win) {
ldflags = [ "/LARGEADDRESSAWARE" ]
}
deps = [
"//base",
"//build/win:default_exe_manifest",
]
}
其中可执行文件名是executable("jdtest")
deps 依赖引用了base库。
jdtest.cc内容如下:
#include<iostream>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <initializer_list>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/memory_mapped_file.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
int main(int argc, const char* argv[]) {
base::AtExitManager at_exit_manager;
base::CommandLine::Init(argc, argv);
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
command_line.GetCommandLineString();
std::cout<<"hello world\n";
return 0;
}
3、在\src\BUILD.gn 里面添加生成gn依赖:
if (is_win) {
deps += [
"//jdtest:jdtest",
]
4、命令行输入:gn gen out/Debug
注意一定要在在\src\BUILD.gn里面添加//jdtest:jdtest 否则无法生成jdtest.ninja文件,导致无法编译。
【F:\code\google\src\out\Debug\obj\jdtest\jdtest.ninja】
5、命令行输入:ninja -C out/debug jdtest
6、 最后在debug目录生成jdtest64.exe