1

Is there a way to retrieve predefined macros from the GCC preprocessor without having to use temporary files (e.g. using pipes)?

There is a similar question here on StackOverflow, but all its answers make use of the command line, while I need to read mentioned data from within a process. GCC dump preprocessor defines

Google basically returns a lot of answers to the command line version of the question.

When I try to perform the command line trick of directing output to /dev/null on Linux or NUL on Windows using pipes:

RunAndGetOutput("cpp -dM -E < NUL");

... an error occurs:

cpp.exe: error: <: Invalid argument

cpp.exe: warning: '-x c' last input file has no effect

cpp.exe: fatal error: no input files

compilation terminated.

When I execute the same command from the command line all is fine, and a list of defines it printed.

Is there any way I can fix this problem?

Community
  • 1
  • 1
Orwell
  • 1,468
  • 1
  • 13
  • 28
  • It sounds like you need a named pipe. Look up CreateNamedPipe (Windows) or mkfifo (POSIX). That way you get a special file name you can pass to GCC as an output file. The annoying bit naming it, usually based on the PID. – doynax Dec 13 '13 at 21:15
  • RunAndGetOutput is not interpreting shell metacharacters, so gcc sees a `<` on the command line and doesn't know what that means. – Raymond Chen Dec 14 '13 at 03:05

1 Answers1

1

I'm not sure exactly sure how NUL behaves under windows, but I assume it is similar to /dev/null in Unix/Linux: /dev/null in Windows?

On my Ubuntu VM I can do:

cpp -dM -E -xc /dev/null

So I assume under windows you could do something like:

cpp -dM -E -xc NUL

This assumes NUL behaves essentially like an empty file. If the output from type NUL is any indicator here, then this is hopefully the case.

Alternatively, if this doesn't work, you could do the cpp command with redirection within a cmd.exe subshell:

cmd.exe /C "cpp -dM -E < NUL"

From the point of view of RunAndGetOutput(), this is just a single cmd.exe command with a few params passed, and no pipes/redirections. But the cmd.exe shell will interpret the passed string as a command, and importantly will understand how to handle the < redirection correctly.


Since you're calling the cpp preprocessor directly, the -E and -xc options are unnecessary (at least this is the case in Linux), but leaving them there won't do any harm.

Community
  • 1
  • 1
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
  • @nos - yes, as always there is more than one way to do things. But be careful - windows `echo` is a bit different - if run alone, it will print `ECHO is on.`. To really echo a blank line, you need to do `echo.`. So `cmd.exe /C "echo. | cpp -xc -dM -E -"` should do the trick. – Digital Trauma Dec 13 '13 at 22:10