C++ 无法在 Mac 终端中运行 qmake
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22116608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot run qmake in Mac Terminal
提问by Nick Corin
I'm learning Qt for my C++ course at college. I am trying to set up the environment for my first assignment but I can't seem to get it right. I swear I have run qmake in the terminal before, but now since I installed Qt 5.2 with Qt Creator I can't create the projects in the terminal.
我正在为我在大学的 C++ 课程学习 Qt。我正在尝试为我的第一个任务设置环境,但我似乎无法做对。我发誓我以前在终端中运行过 qmake,但现在自从我用 Qt Creator 安装了 Qt 5.2 后,我无法在终端中创建项目。
None of the commands are recognised and on top of that if I try to compile a standard C++ file with
没有任何命令被识别,最重要的是,如果我尝试使用以下命令编译标准 C++ 文件
#include <QtGui>
the compiler won't find it. The g++ compiler that I'm using will not find any of the Qt Libraries that I try to import. I looked online and the solution I found was just to run
编译器不会找到它。我使用的 g++ 编译器找不到我尝试导入的任何 Qt 库。我在网上查看,我找到的解决方案只是运行
brew install qt
in Terminal and hope that it sorts itself out, although the terminal throws an error and won't install it once it's downloaded. I really need to get this working for my assignment.
在终端中并希望它自行解决,尽管终端会抛出错误并且一旦下载就不会安装它。我真的需要让它为我的任务工作。
Any help would be greatly appreciated. Thanks in advance!
任何帮助将不胜感激。提前致谢!
* EDIT *
* 编辑 *
Okay so I added the qmake bin into my PATH variable. This was the path:
好的,所以我将 qmake bin 添加到我的 PATH 变量中。这是路径:
/Users/#####/Qt5.2.1/5.2.1/clang_64/bin
The problem is that now nothing in my C++ source files are being recognized. None of the imports like QString or QTextStream or QtGui, etc. What can cause this?
问题是现在我的 C++ 源文件中没有任何内容被识别。没有像 QString 或 QTextStream 或 QtGui 等的导入。是什么导致了这种情况?
回答by andrea.marangoni
Did you set the environment variable PATH
with the path to Qt?
您是否PATH
使用 Qt 的路径设置了环境变量?
in Terminal do: echo $PATH
and look for something like /usr/local/Qt-5.x.x/bin:PATH
在终端做:echo $PATH
并寻找类似的东西/usr/local/Qt-5.x.x/bin:PATH
If there is not any path to your installed qt, set it like this:
如果您安装的 qt 没有任何路径,请按如下方式设置:
In .profile (if your shell is bash), add the following lines:
在 .profile(如果您的 shell 是 bash)中,添加以下几行:
PATH=/usr/local/Qt-5.0.2/bin:$PATH
export PATH
To help you in the process you probably would like to read here: QT mac install
为了在此过程中为您提供帮助,您可能想在这里阅读:QT mac install
ANSWER TO NEW QUESTION
回答新问题
If writing code nothing seems to be recognized you should add the link to your include
directory. The include
directory is where there are all the header file, so your IDE can give you suggestion about class method etc...
如果编写代码似乎无法识别任何内容,则应将链接添加到您的include
目录中。该include
目录是所有头文件的所在,因此您的 IDE 可以为您提供有关类方法等的建议...
回答by Reinstate Monica
It is generally not needed at allto touch the PATH on any system (Mac, Unix, Windows) merely to use Qt. You may have multiple Qt versions installed in parallel (say 4.8 from macports, 5.2, git stable, etc.).
通常根本不需要在任何系统(Mac、Unix、Windows)上触摸 PATH 仅仅为了使用 Qt。您可能同时安装了多个 Qt 版本(比如 macports 的 4.8、5.2、git stable 等)。
A way of building a Qt project on Unix is (substitute relevant paths for your setup):
在 Unix 上构建 Qt 项目的一种方法是(为您的设置替换相关路径):
mkdir ~/build-dir
cd ~/build-dir
~/Qt5.2.1/5.2.1/clang_64/bin/qmake ~/src/myproject/myproject.pro
make -j2
# were N in -jN is the number of CPU cores on your system
回答by Ruto Collins
For me, I realised that the .bashrc
file was not getting autoloaded on a new terminal session after adding the qmake PATH to the file (using echo 'export PATH="$(brew --prefix [email protected])/bin:$PATH"' >> ~/.bashrc
).
对我来说,我意识到.bashrc
在将 qmake PATH 添加到文件(使用echo 'export PATH="$(brew --prefix [email protected])/bin:$PATH"' >> ~/.bashrc
)后,该文件没有在新的终端会话中自动加载。
So I simply run source ~/.bashrc
and bam! It worked!
所以我只是跑source ~/.bashrc
和砰!有效!
回答by Teenenggr
Working on 9 APR 2020 set Path echo 'export PATH="/usr/local/opt/qt/bin:$PATH"' >> ~/.bash_profile
工作于 2020 年 4 月 9 日 set Path echo 'export PATH="/usr/local/opt/qt/bin:$PATH"' >> ~/.bash_profile
回答by Morten Kristensen
The reason why you cannot execute the program is because it's not in the $PATH
of the shell you are using. First find where it is located and then add the directory to that binary directory to your $PATH
environment variable. Then you'll be able to execute it in your terminal.
您无法执行该程序的原因是它不在$PATH
您使用的 shell 中。首先找到它所在的位置,然后将该目录添加到该二进制目录到您的$PATH
环境变量中。然后你就可以在你的终端中执行它了。
For instance, if you are using ZSH and your program resides in "/opt/local/bin" then execute the following to make it available through $PATH
:
例如,如果您使用 ZSH 并且您的程序驻留在“/opt/local/bin”中,则执行以下命令以使其可用$PATH
:
export PATH=$PATH:/opt/local/bin
After this point you would be able to run the program. And you should add this to your shell RC file.
在此之后,您将能够运行该程序。你应该把它添加到你的 shell RC 文件中。
回答by CroCo
If you've installed the Qt SDK then the path of qmake is not automatically included as an environment variable, so you have to do it manually. This is also true in windows. I found this weird. It should be included automatically unless there is a sensible reason behind it.
如果您已经安装了 Qt SDK,那么 qmake 的路径不会作为环境变量自动包含在内,因此您必须手动进行。这在windows中也是如此。我发现这很奇怪。除非背后有合理的理由,否则它应该被自动包含在内。