c++visual-studio-2008c-preprocessor

Ignore missing .obj files or "/P switch + external tool + linker = hell"


I am trying to make a setup as described in my previous question: Any way to parse preprocessed source through external tool before it compiles?

All of my .cpp files are set to compile with /p, which generates .i (preprocessed) files for all of them, but no object files. Those generated .i files are also added to my project, and an external build tool option is set to my tool that modifies those files, and saves them under new extension, .obfuscated.cpp All those .obfuscated.cpp files are also added to the project, and are set to compile normally, producing object files.

Now the problem is that Visual Studio (or the linker, someone of them) for some reason want the obj files both from .cpp files (which now are just saved to .i files, no object files produced), and from .obfuscated.cpp (which are created normally).

I would assume that the linker would not require .obj files from sources that are set to compile with /P option, because, well that option prevents object files from being created.

Now I only see two ways to solve this: 1) Do the build in two steps. In the first one make sure all the .cpp files are preprocessed and saved to .i files. This build does not have to complete, just has to save .i files. Then after that, I select all the .cpp files and set them to "Exclude from build", then everything compiles as it should. 2) Instead of adding the files to the project and using the external build tool option, make a pre-link step instead, in which my own tool would automatically find all the .i files (could take all *.i in a certain directory), process them to *.obfuscated.cpp, and then manually call cl.exe on all of those files to produce object files, rename them to proper names (so that the linker thinks they are object files created from original sources) and put into intermediate directory. But in this case I would have to keep track of all the compiler arguments and change them accordingly if something changes in my project... Both of these solutions don't seem very beatuful. Is there some other way to do this in Visual Studio? Can't I just tell the linker to ignore missing .obj files? (All the symbols will be found anyway...)


Solution

  • maybe this can help you : instead of the extension .obfuscated.cpp give it an simpler extension like .icpp and add those .icpp files (after first compile) to your project (in a separate folder in your project) ,then for each of those .icpp files goto their property-page and set the correct build-tool (C\C++ compiler) and your .obj files retain the same name-part as your original .cpp files so linking should automatically be done correctly.