1

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?

Plàcid Masvidal
  • 130
  • 1
  • 2
  • 12
  • Thanks to https://stackoverflow.com/questions/43680899/how-to-use-makefile-variables-as-files-to-include-in-g-command I fixed something but not solve the purpose. The problem was in the alias assignment: alias gxxcomp='g...' it has to be assigned with ` instead of ' alias gxxcomp=`g... Fixing this it works, if I do: gxxcomp it compiles the file. However, the problem is that the value of $tocompile it's always the same, so always compile the same file. This may be because $tocompile is a var and not a pointer. So the question is: How to declare tocompile as a pointer to tocompile.txt? – Plàcid Masvidal Feb 20 '19 at 18:15
  • Are you sure that a simple Makefile cannot do the trick? – carlosvalderrama Feb 20 '19 at 19:10

1 Answers1

0

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.

carlosvalderrama
  • 465
  • 1
  • 6
  • 22
  • Yeah! It works. I'm begginer to programming in C++ and Linux I don't know how to use makefile. I'd like to learn it. Although I want still like to know how to give to the var tocompile a changing value (the content of tocompile.txt) – Plàcid Masvidal Feb 20 '19 at 19:32
  • I added a short example. The changing value issue is resolved now? If yes, would you accept the answer? – carlosvalderrama Feb 20 '19 at 20:10
  • Your makefile solution works perfectly and is a good way to do what I wanted to do! Thank you. Anyway, I wonder how to do to assign a var with changing value and us it into another alias command... – Plàcid Masvidal Feb 20 '19 at 23:23
  • 1
    I think the only possibility is to defer the cat command to the execution time of the gxxcomp command. Otherwise, the file will already have been read and any changes to it will be ignored. I tried around but could not find another solution than the one above. – carlosvalderrama Feb 20 '19 at 23:41