【问题标题】:MacOS C++ terminal compile shortcut?MacOS C++ 终端编译快捷方式?
【发布时间】:2020-11-25 15:50:42
【问题描述】:

我对 C++ 和在 MacOS 上使用终端都是新手,我想知道是否有任何方法可以创建编译快捷方式。

每次我想编译时都必须输入g++ filename.cpp -o 'filename' 有点烦人。只是好奇是否有捷径,我只需输入g++ 'filename'

谢谢!

【问题讨论】:

  • 您可以使用Makefile。您设置一次,然后运行make filename
  • 我会试一试。欣赏它
  • 对于玩具程序,我有一个可以调用的脚本。对于更大的项目,我会选择 cmake 路线。

标签: c++ macos terminal


【解决方案1】:

最好使用 Makefile(或其他构建系统)来编译您的代码。也许可以查看以下线程,了解 C++ 项目通常是如何构建的:Difference between Cmake, gnu make and manually compiling

但是,如果您知道您将编写单个文件脚本,那么可能是 following bash file approach from this SO thread would work:

为了方便访问,这是@Some程序员老兄提供的代码示例:

output=$(g++ $PROG_NAME 2>&1)
if [[ $? != 0 ]]; then
    # There was an error, display the error in $output
    echo -e "Error:\n$output"
else
    # Compilation successfull
    ./a.out
fi

【讨论】:

  • 谢谢!看来makefile绝对是我应该习惯的东西
猜你喜欢
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 2021-01-09
  • 2013-08-19
  • 1970-01-01
相关资源
最近更新 更多