【问题标题】:.out Permission denied using -c directive with g++.out 权限被拒绝使用带有 g++ 的 -c 指令
【发布时间】:2017-01-18 10:22:44
【问题描述】:

我正在使用 makefile 进行一些练习,并注意到我尝试编译的 .cpp 文件在尝试执行相应的 .out 文件时出现 Permissione Denied 错误。我还注意到使用-c 指令似乎是个问题。这是因为当我这样做时:

singleton_template_main.o: singleton_template_main.cpp singleton.h
       g++ -c singleton_template_main.cpp -o singleton_template_main.out

我得到了错误,但是当我这样做时:

singleton_template_main.o: singleton_template_main.cpp singleton.h
     g++ singleton_template_main.cpp -o singleton_template_main.out

我没有错误。可能是什么问题?

我正在使用 macOS Sierra。

【问题讨论】:

    标签: c++ macos makefile compilation g++


    【解决方案1】:

    -c 标志编译代码(禁止链接),生成中间目标文件 - 这是不可执行的。还值得注意的是,在这种情况下,将输出文件命名为 .out 可能是不可取的,目标文件的约定是扩展名,例如 .o.obj

    删除 -c 标志会导致编译器根据需要编译和链接代码,从而生成可执行文件 - 从而按预期运行。

    【讨论】:

      【解决方案2】:

      看起来您在命令中输入了错误的输出文件(-o singleton_template_main.out 而不是 -o singleton_template_main.o)。最好在命令中使用$@,避免出现此类错误:

      singleton_template_main.o: singleton_template_main.cpp singleton.h
          g++ singleton_template_main.cpp -o "$@"
      

      甚至

      singleton_template_main.o: singleton_template_main.cpp singleton.h
          $(COMPILE.cpp) $(OUTPUT_OPTION) $<
      

      您可能还需要显式链接为 C++:

      singleton_template_main: LINK.o = $(LINK.cc)
      

      【讨论】:

        猜你喜欢
        • 2020-11-25
        • 2023-01-26
        • 1970-01-01
        • 2021-01-26
        • 2014-05-29
        • 1970-01-01
        • 2016-09-26
        • 2012-05-01
        • 1970-01-01
        相关资源
        最近更新 更多