我正在尝试编写一个生成.gch文件的GN文件。我已经浏览了GN文档这里
我只是不知道如何实现这一点,在GN方面我是新手。让我来解释一下我想要达到的目标。
我希望GN创建以下一对编译器命令:
my-clang -x c++-header pch/hello_world.h -o pch/hello_world.h.gch
my-clang -include pch/hello_world.h -o hello.out -c hello_world.cpp我希望能够在GN文件中创建一个源代码集,如下所示:
source_set("source_set0") {
precompiled_source = "//pch/hello_world.h"
precompiled_header = "../pch/hello_world.h"
cflags = [
"-include$precompiled_header"
]
sources = [
"//hello_world.cpp",
]
}然而,这实际上并不会生成预编译的头文件。
我希望我需要另一个source_set或等效于专门编译头文件,但我的理解是,编译器需要.gch后缀才能将该文件识别为预编译的头文件。
所以我想我需要说服GN根据目标创建特定的输出文件
理想情况下,我希望能够修改我的工具规范:
tool("cxx") {{
command = "\"{cxx_exe}\" ... -c {{{{source}}}} -o {{{{output}}}}"
outputs = [
"{{{{source_out_dir}}}}/{{{{target_output_name}}}}.{{{{source_name_part}}}}.o",
**SOME CONDITION**
"{{{{source_out_dir}}}}/{{{{target_output_name}}}}.{{{{source_name_part}}}}.h.gch",
]
}}然而,我对工具变量的解读表明,上述功能仅适用于linker_tools。
似乎GN需要一些令人信服的方法来将cxx工具与.h文件相关联。
有没有人有这方面的经验,或能指出我的正确方向。
TL;DR
耽误您时间,实在对不起
附注:cpp文件内容:
int main (void){
const char* greeting = "Hello world";
print_greeting(greeting);
}.h文件内容
#pragma once
#include <iostream>
void print_greeting(const char* greeting){
std::printf("%s", greeting);
}发布于 2020-06-24 13:39:09
我把事情搞清楚了,我修改了工具:
tool("cxx") {{
command = "\"{cxx_exe}\" ... -c {{{{source}}}} -o {{{{output}}}}"
precompiler_header_type = "gcc"
outputs = [
"{{{{source_out_dir}}}}/{{{{target_output_name}}}}.{{{{source_name_part}}}}.o",
**SOME CONDITION**
"{{{{source_out_dir}}}}/{{{{target_output_name}}}}.{{{{source_name_part}}}}.h.gch",
]
}}然后更新我的gn文件:
config("precompiled_header"){
precompiled_header="../pch/hello_world.h"
precompiled_source="//pch/hello_world.h"
}
source_set("source_set1") {
public_configs = [
":precompiled_header"
]
sources = [
"//hello_world.cpp",
]
}事后看来,这实际上是文档说要做的。GN创建的命令行现在如下所示
ninja.exe -t命令
"my-clang++.exe" ... -O2 -g -x c++-header -c ../pch/hello_world.h -o obj/pch/source_set1.hello_world.h-cc.gch
"my-clang++.exe" ... -O2 -g -include obj/pch/source_set1.hello_world.h-cc -c ../hello_world.cpp -o obj/source_set1.hello_world.o希望这对将来的人有帮助。
https://stackoverflow.com/questions/62536104
复制相似问题