0

Question

I have three files in my current working directory:

  • hello.cpp
  • goodbye.cpp
  • prog.cpp

I would like to only preprocess hello.cpp and goodbye.cpp and dump the output in files hello.i and goodbye.i. Is there a way to achieve this using g++ in a Ubuntu Linux command line using one command? The reason I would like to call g++ only once is because I would eventually like to include this command in my own Makefile.

What I've tried

I basically did the following:

g++ -E -o goodbye.i hello.i goodbye.cpp hello.cpp

Which, unsurprisingly, failed with the following error:

g++ : fatal error: cannot specify '-o' with '-c', '-S' or '-E' with multiple files compilation terminated

I also tried g++ -E goodbye.cpp hello.cpp, which only reminded me that the preprocessor dumps to stdout by default. I do, for the purposes of this exercise, need for g++ to dump the result into an actual *.i file...

What I'm trying to avoid

From the comments, it seems to me that I can provide a further clarification. I'm trying to avoid having multiple commands in my Makefile, as each separate .cpp file would generate a separate command:

all: preprocess_hello preprocess_goodbye

preprocess_hello:
    g++ -E -o hello.i hello.cpp

preprocess_hello:
    g++ -E -o goodbye.i goodbye.cpp

Obviously, this is not ideal, because every new file I add would require adding a new command and updating the all target.

19
  • 2
    "[B]ecause I would eventually like to include this command in my own Makefile" seems like a strange reason. Why does it have to be one command because of that? Commented May 31, 2022 at 9:08
  • What prevents you from writing two lines of command to preprocess each of file? Makefiles accepts multiple lines of command. Commented May 31, 2022 at 9:09
  • 1
    What is the purpose of the .i file? Commented May 31, 2022 at 9:16
  • 1
    I believe what @user253751 meant is that what would you like to do with *.i files. Are you parsing inside makefile or having another script triggered to parse...etc. Also you might want to add a tag for makefile. Commented May 31, 2022 at 9:21
  • 1
    If the point is to just generate .i files for all .cpp files, only one time, then you don't even really need make and you can probably do it with a shell script, although it's not difficult with make. Commented May 31, 2022 at 9:23

1 Answer 1

3

You can use a pattern rule so Make knows how to generate a .i file from a .c file:

%.i: %.cpp
    g++ -E -o $@ $<

and then if your makefile ever requires hello.i, Make will know that it can use the command g++ -E -o hello.i hello.cpp

E.g. if you have all: hello.i goodbye.i and run make all it will know that it needs hello.i and goodbye.i and it will make them.


If you have a list of .cpp files and you need to convert it to a list of .i files (e.g. if you want to do all: $(CPPFILES) but it doesn't work because those are the .cpp files and not the .i files) you can use patsubst:

all: $(patsubst %.cpp,%.i,$(CPPFILES))

You can even use wildcard to get the list of cpp files. To make all depend on all the .i files for all the .cpp files in the current directory, you could use

all: $(patsubst %.cpp,%.i,$(wildcard *.cpp))
Sign up to request clarification or add additional context in comments.

1 Comment

This smells like the right answer. I'll do some research and accept it when I get it to work. Thanks for taking the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.