我想做的是为下面的句子创建一个别名:
g++ -g -pedantic -Wall -o executablefilenamehere pathoffiletocompilehere
因为我要经常编译单个文件。
为此,我正在尝试使用输出文件名和 要编译的文件的路径。所以我有一个名为 tocompile.txt 的 txt 文件,其中包含:
test /home/me/test.cpp
然后,我分配给一个 var,我调用它来编译 tocompile.txt 的内容:
tocompile=`cat tocompile.txt`
这是可行的,因为如果我执行 echo $tocompile 我会得到:test/home/me/test.cpp
那么,我正在尝试创建别名:
alias gxxcomp='g++ -g -pedantic -Wall -o $tocompile'
当我这样做时,它不起作用:
gxxcomp
我得到:
g++: error: missing filename after ‘-o’
g++: fatal error: no input files
compilation terminated.
正确的做法是什么?
最佳答案
是
alias gxxcomp='g++ -g -pedantic -Wall -o `cat tocompile.txt`'
为你工作?至少在我的 bash 版本中有效。
但正如我的评论中已经提到的,我会为这项工作推荐一个简单的 Makefile。使用内容创建一个名为 Makefile 的文件
test: test.cpp
g++ -g -pedantic -Wall -o $@ $<
并运行make。查看教程,例如 this one .
关于linux - 如何创建一个 linux shell 别名来给 g++ 编译器编译文件的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54785015/