GNU C编译器无法在OST的QT Creator上运行 [英] GNU C Compiler is not working on QT Creator for OSX

查看:63
本文介绍了GNU C编译器无法在OST的QT Creator上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 brew 在OSX上安装了 gcc .我需要使用gcc而不是clang来执行clang不支持的特定命令.在shell屏幕中, gcc example.c 命令用clang编译example.c代码,而 gcc-9 example.c 命令用原始gcc(GNU C编译器)编译代码..

I installed gcc on OSX via brew. I need to use gcc instead of clang for a specific command which clang does not support. In shell screen, gcc example.c command compiles example.c code with clang whereas gcc-9 example.c command compiles the code with the original gcc (GNU C Compiler).

在QT中,我需要使用 pipe()函数来获取GCC命令的输出.如果我将带有gcc的命令字符串放在管道函数中,则会产生错误,因为clang不支持我一开始提到的特定命令.如果我在管道函数中输入了带有gcc-9的命令,则这次QT给出了类似"gcc-9:找不到"的错误.

In QT, I need to use pipe() function to get output of a GCC command. If I put a command string with gcc to the pipe function, it gives error because clang does not support the specific command I mentioned about in the beginning. If I put a command with gcc-9 to the pipe function, this time QT gives error like "gcc-9: not found".

为什么 gcc-9 命令在外壳屏幕上有效,但在QT中不起作用?如何在QT中使用 gcc-9 命令?

Why does gcc-9 command works on shell screen but not in QT? How can I use gcc-9 command in QT?

代码段如下:

std::string removeComments(const std::string &path)
{
    std::array <char, 256> buffer;
    std::string result, cmd = "gcc-9 -fpreprocessed -dD -E '" + path + "'";
    std::unique_ptr <FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);

    if (!pipe)
        throw std::runtime_error("popen() failed!");

    fgets(buffer.data(), buffer.size(), pipe.get());

    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
        if (!skipLine(buffer.data()))
            result += buffer.data();

    return result;
}

推荐答案

您用于编译代码的编译器与您试图从代码中调用的代码无关.IDE中的所有编译器设置都不相关.为避免混淆,请将它们恢复为默认值.

The compiler you are using to compile your code has nothing to do with the compiler you are trying to call from your code. None of the compiler settings in your IDE are relevant. To avoid confusion, return them to their default values.

您需要

  1. 设置您的 PATH 环境变量,使其包含/usr/local/bin ,并且可以找到命令 gcc-9 靠壳或
  2. 使用完整路径,例如"/usr/local/bin/gcc-9"
  1. set up your PATH environment variable such that /usr/local/bin is included, and the command gcc-9 can be found by the shell; or
  2. use a full path like "/usr/local/bin/gcc-9"

这篇关于GNU C编译器无法在OST的QT Creator上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆