简体   繁体   English

如何使用VS编译器将预处理文件编译为目标文件

[英]How to compile a preprocessed file into object file using VS compiler

cl.exe (Visual Studio C/C++ language compiler) compiles source file to preprocessed file with the /P flag eg cl /P Source.c creates Source.i cl.exe(Visual Studio C / C ++语言编译器)使用/ P标志将源文件编译为预处理的文件,例如cl /P Source.c创建Source.i

I want to compile this Source.i (not the Source.c) into Source.obj. 我想将此Source.i(而不是Source.c)编译为Source.obj。 How should I do it? 我该怎么办? Can't a find a flag that takes preprocessed file as an input. 找不到将预处理文件作为输入的标志。

You don't need to do anything special. 您不需要做任何特别的事情。 It's a valid C++ source file that just goes to your compiler. 这是一个有效的C ++源文件,仅用于编译器。 (Though it won't be a portable source file, since all the toolchain's own standard library gubbins, and whatever else, have been expanded.) (尽管它不会是可移植的源文件,因为所有工具链自己的标准库gubbins以及其他任何东西都已扩展。)

The only trick here is that usually the "intermediate" step is done for you by the front-end, taking the preprocessed source and passing it to the "real" compiler. 这里唯一的技巧是通常由前端为您完成“中间”步骤,将经过预处理的源并将其传递给“实际”编译器。 But you can still just do that yourself. 但是您仍然可以自己做。

However, you may want to rename it so it has a nice C++-like extension. 但是,您可能需要重命名它,使其具有类似于C ++的扩展名。 Otherwise, you can pass /Tp to force your file to be treated as C++, regardless of extension. 否则,您可以传递/Tp强制将文件视为C ++,而不管其扩展名如何。 (For GCC this would be -x c++ .) (对于GCC,它将是-x c++ 。)

The other thing to note is that it's then still going to run a preprocessing step on your file, not knowing that it's no longer needed (all the preproc directives have already been expanded by your first step), so that's a little wasted additional time. 还要注意的另一件事是,它仍将在文件上运行预处理步骤,而不知道不再需要它(所有preproc指令已经在您的第一步扩展了),因此浪费了一些额外的时间。 But then this is not (or should not be) a typical workflow. 但这不是(也不应该)是典型的工作流程。

cl /P Source.c
cl /Tp Source.i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Linux中编译后以这样的方式编译c / cpp代码以提供经过预处理的汇编文件和目标文件? - how to compile c/cpp code in such a way that it gives preprocessed assembled and object file after compilation in linux? 有没有办法用clang有效地编译已经预处理的文件? - Is there a way to efficiently compile already preprocessed file with clang? MSVS C ++,如何使用* .i扩展名编译已经预处理的文件? - MSVS C++, how to compile already-preprocessed file with *.i extension? 如何在另一个 C++ 程序中使用 preprocessed.i 文件? - How to use a preprocessed .i file in another C++ program? 如何输出预处理的代码并进行编译(Visual Studio) - How to output preprocessed code AND compile it (Visual Studio) 如何以编程方式询问编译器在C ++中编译文件? - How to ask, programmatically, a compiler to compile a file in C++? 编译先前预处理的文件更改输出 - Compiling previously preprocessed file changes output C预处理文件中有哪些不熟悉的行? - What are these unfamiliar lines in the C preprocessed file? C++:在 VSCode 中生成预处理文件 - C++: Generating Preprocessed File in VSCode 如何在 cmake 工具链文件中为已知的自定义编译器设置编译功能以使用 target_compile_features - How to set compile features in cmake toolchain file for known custom compiler to use target_compile_features
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM