0

I'd like to be able to view preprocessor output in order to make sure my preprocessor directives run correctly. Dev-C++ has an option in Tools > Compiler Options... > General to add commands when calling the compiler, and I've added the command -E C:\Personal\preprocessed.cpp. I got a compiler error saying the file didn't exist, but shouldn't the compiler just create the file in that case? I created the file, and now I'm getting this error: cannot specify -o with -c, -S or -E with multiple files.

Why am I using Dev-C++ instead of Visual Studio? Since I'm still learning, I'd like to be able to test just a few lines of code without having to create an entire new project.

Yes, I've seen this question and no adequate answer was given. Please don't mark this as a duplicate.

Thanks in advance for your help!

Community
  • 1
  • 1
Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66

1 Answers1

1

I've added the command -E C:\Personal\preprocessed.cpp. I got a compiler error saying the file didn't exist, but shouldn't the compiler just create the file in that case?

No, because the -E option takes no argument, filename or otherwise. It simply instructs the compiler to do nothing but preprocessing. The preprocessed code is written to the standard output. Thus:

Thus:

g++ -E C:\Personal\preprocessed.cpp foo.cpp

tells the compiler that you want run g++ -E with the pair of input files C:\Personal\preprocessed.cpp and foo.cpp, which as you've discovered is not allowed.

The simple thing that you want to do is absurdly difficult with your IDE of choice. Assuming the source file you want to preprocess is C:\Personal\foo.cpp and the g++ is in your PATH, just open a command window in C:\Personal and run:

g++ -E foo.cpp > foo.ii

I suggest the output file foo.ii - though you can call it whatever you like - because g++ recognizes the extension .ii as denoting C++ source code that has already been preprocessed. You can run:

g++ -Wall -o prog foo.ii

and foo.ii will be compiled and linked as program prog without being preprocessed again.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • That's very helpful, thank you. When I remove the argument from -E, the preprocessor output from Test.cpp goes right into Test.exe, and then I can change that to Test.txt to read it. That doesn't seem to be any more different than the command line solution you gave, which doesn't work for me anyway. I get 'g++' is not recognized etc. I don't have a good understanding of how to use the command line and I try to avoid it because it seems excruciating to have to navigate to the correct folder every time. – Kyle Delaney Aug 21 '16 at 00:38
  • I'll see if Visual Studio gives a better solution. I hope you don't mind if I leave this question unanswered for now to give other people a chance to give insight into how to make Dev-C++ do this. – Kyle Delaney Aug 21 '16 at 00:50