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. 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). 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开发者_如何学C 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
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 . I need to make minor changes to the root fooTest.h in order to make it a suitable mock-module for testing. Thus, simply using includes won't work. 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. It will, however, also grab standard libraries - but these shouldn't be harmful to include multiple times.
This isn't a C++ question - you are asking how to manipulate files in some sort of script. The immediate answer that comes to mind is:
cat foo.h foo.cpp > fooTest.h
Write a simple tool that processes only #include ""
directives. Note, it should not process "#include <>" directives, and don't touch any other preprocessor directive you encounter.
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). 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.
Under Visual C++, you can use /P [/EP]
See reference: http://msdn.microsoft.com/en-us/library/8z9z0bx6%28v=VS.71%29.aspx
精彩评论