简体   繁体   English

有没有一种自动的方法来合并C ++实现(.cpp)和标头(.h)文件

[英]Is there an automated way to merge C++ implementation(.cpp) and header (.h) files

I am trying to create a unit test framework using CPPUnit for a large code base. 我正在尝试使用CPPUnit创建大型代码库的单元测试框架。 I need to be able to test individual modules, all of which are part of a module tree that begins with a specific root module. 我需要能够测试各个模块,所有这些模块都是以特定根模块开头的模块树的一部分。

Due to a non-technical reason, I cannot touch the production file (my original approach involved adding an ifdef to the root module). 由于非技术原因,我无法触摸生产文件(我的原始方法涉及向根模块添加ifdef)。 So I thought of another approach, which is to have create copies of the root module headers as well as copies of headers belonging to modules in the intermediate inheritance hierarchy. 因此,我想到了另一种方法,即创建根模块头的副本以及属于中间继承层次结构中模块的头的副本。 Because of the number of number of modules involved as well as the size of each module's source. 由于涉及的模块数量以及每个模块的来源大小。 I'm looking for a way to automatically do that merging for me. 我正在寻找一种自动为我合并的方法。

So for foo.h, and foo.cpp, I'm looking for a some kind of a tool that'll output fooTest.h, where fooTest.h contains the declaration AND definition of everything that is in foo.cpp/foo.h 因此,对于foo.h和foo.cpp,我正在寻找一种可以输出fooTest.h的工具,其中fooTest.h包含foo.cpp / foo中所有内容的声明和定义。 H

EDIT: Thanks for the answers, one thing I forgot to mention is that, the contents of fooTest.h is not supposed to be the merged result of foo.cpp and foo.h . 编辑:感谢您的回答,我忘记提及的一件事是,fooTest.h的内容不应该是foo.cpp和foo.h的合并结果。 I need to make minor changes to the root fooTest.h in order to make it a suitable mock-module for testing. 我需要对根fooTest.h进行细微更改,以使其成为适合测试的模拟模块。 Thus, simply using includes won't work. 因此,仅使用include将无法正常工作。 I'll look into concatenating the files and see if that solves my problem. 我将研究串联文件,看看是否能解决我的问题。

gcc -E

runs just the preprocessor: 只运行预处理器:

  -E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. 

This will have the effect of inlining all #include directives. 这将内联所有#include指令。 It will, however, also grab standard libraries - but these shouldn't be harmful to include multiple times. 但是,它也会抓取标准库-但是,多次包含这些库不会有害。

Write a simple tool that processes only #include "" directives. 编写一个仅处理#include ""指令的简单工具。 Note, it should not process "#include <>" directives, and don't touch any other preprocessor directive you encounter. 请注意,它应该处理“的#include <>”的指令,不碰你遇到任何其他预处理指令。

You'll want to support -I arguments in a gcc like way (or do something equivalent for your compiler if running in a different environment). 您将希望以类似gcc的方式支持-I参数(如果在不同的环境中运行,则可以对您的编译器执行等效操作)。 Should be a afternoon job, and would make a nice open source project (leave a pointer if you do it). 应该是一个下午的工作,并且将成为一个不错的开源项目(如果这样做,则要留个指针)。

This will result in dragging in the whole tree of "non-standard" headers used by each source file you work on, but that should be harmless. 这将导致拖拽您要处理的每个源文件使用的“非标准”头文件的整个树,但这应该是无害的。

This isn't a C++ question - you are asking how to manipulate files in some sort of script. 这不是C ++问题-您在问如何用某种脚本来操作文件。 The immediate answer that comes to mind is: 我想到的直接答案是:

cat foo.h foo.cpp > fooTest.h

Under Visual C++, you can use /P [/EP] 在Visual C ++下,您可以使用/ P [/ EP]

See reference: http://msdn.microsoft.com/en-us/library/8z9z0bx6%28v=VS.71%29.aspx 请参阅参考: http : //msdn.microsoft.com/zh-cn/library/8z9z0bx6%28v=VS.71%29.aspx

在* nix下,您可以只使用cat。

cat foo.h foo.cpp > fooTest.h

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM