Skip to content
Advertisement

how to create a linux shell alias to give to g++ compiler the path of the file to compile?

What I want to do it’s to create an alias for the sentence below:

g++ -g -pedantic -Wall -o executablefilenamehere pathoffiletocompilehere

Because I have to compile single files frequently.

To do it, I’m trying creating a txt file with the output file name and the path of the file to compile. So I have a txt, called tocompile.txt, file that contains:

test /home/me/test.cpp

then, I assign to a var that I call tocompile the content of tocompile.txt:

tocompile=`cat tocompile.txt`

This is working, because if i do echo $tocompile I’m getting: test /home/me/test.cpp

So, then, I’m trying to create the alias doing:

alias gxxcomp='g++ -g -pedantic -Wall -o $tocompile'

It doesn’t work, when I do:

gxxcomp

I get:

g++: error: missing filename after ‘-o’
g++: fatal error: no input files
compilation terminated.

What’s the right way to do that?

Advertisement

Answer

Does

alias gxxcomp='g++ -g -pedantic -Wall -o `cat tocompile.txt`'

work for you? At least in my bash version that works.

But as already mentioned in my comment, I would recommend a simple Makefile for this job. Create a file called Makefile with the contents

test: test.cpp
        g++ -g -pedantic -Wall -o $@ $<

and run make. Check out a tutorial, e.g., this one.

Advertisement