简体   繁体   中英

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 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.

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.

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

Under Visual C++, you can use /P [/EP]

See reference: http://msdn.microsoft.com/en-us/library/8z9z0bx6%28v=VS.71%29.aspx

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

cat foo.h foo.cpp > fooTest.h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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