How does C++ preprocessor work?

守給你的承諾、 提交于 2019-12-13 09:59:23

问题


I am reading the book "C++ Primer" 5th Edition and I read that the preprocessor is a program that runs before the C++ compiler and replaces the #include, #define and #ifdefs and others with the appropriate content and then transfer control over to the compiler.

But I came across a way in cl.exe (Microsoft Compiler) to view the preprocessor output saved directly to file. I did it, and when I opened the preprocessor output file I was surprised because I did not find what I expected!

They were totally big and contained what looked like obfuscated code!

Please Explain what in reality does the Pre-Processor of C++ does.


回答1:


It is entirely possible to pre-process Java just like you do C or C++. Just use something like this:

gcc -E myjava.java > myjava.preprocesses.java

Then you can use macro expansion, #if etc to your hearts content. Of course, it does have the drawback that there is a further tool needed for the compile.




回答2:


You can roll out a JNI lib that ties in with your native C/C++ code that has all your necessary macros.



来源:https://stackoverflow.com/questions/16253977/how-does-c-preprocessor-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!