0

Looking at the code below, it simply converts a c or c++ file, e.g. main.cpp into its state after preprocessing, e.g. main_pp.cpp.

However, running the command it generates, g++ -E "main.cpp" > "main_pp.cpp" works just fine on the command line.

The exact error is g++: error: >: Invalid argument, so apparently it doesn't see that the > is not part of the arguments list.

cd "$(CURRENT_DIRECTORY)"
g++ -E "$(FILE_NAME)" > "$(NAME_PART)_pp$(EXT_PART)"

Does anyone know why the behaviour is different in NppExec compared to standard command line?

2 Answers 2

1

Output redirection via > and piping via | is something that the command line interpreter is responsible of. In Windows, cmd.exe is the command line interpreter. So, instead of

g++ -E "$(FILE_NAME)" > "$(NAME_PART)_pp$(EXT_PART)"

use this:

cmd /C g++ -E "$(FILE_NAME)" > "$(NAME_PART)_pp$(EXT_PART)"
1

The short answer is that command line interpreter does a lot more than just executing other programs, and things like redirecting the output of a program using > or pipe the output with | are features provided by the command line interpreter, so NppExec would need to simulate it in order to work.

A more precise answer is that you usually start new processes in Windows using CreateProcess() and related functions, and although you can redirect the output of the new process is not as straightforward as using >. You need to open a handle to the file and pass it in the STARTUPINFOA using the appropriate flags.

So in order to support > for redirection NppExec would need to parse the line to separate that part from the application and parameters, which can be tricky, and also it would need to take into account you can redirect to other things that are not files like to a printer.

It's not impossible, and it seems that NppExec do some basic parsing because in your example you use cd which is also provided by the command line interpreter. But this can be as simple as just checking if the first two non space characters of a line are "cd" and then use the rest of line to change the current directory.

I also suspect that NppExec don't even parse the line to separate the application from the parameters. I think that it just replace the variables and take advantage of the fact that CreateProcess() allows omitting the application parameter, so it can just pass the whole line as the command line parameter (at least that is what I would do :-).

Finally, programs that just output their result to the standard output expecting that you redirect it to a file or pipe it to another program are more typical in Unix/Linux (g++ seems to be part of GNU which was created originally for Unix). In Windows is more common using a parameter to specify the output file, so supporting redirection is not something that you would think as a top priority (or even think at all about it) if you don't use "Linux style" programs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .