c++ - 使用 QtCreator 构建纯 C++

标签 c++ qt qt-creator qml qt5

目前我在 Ubuntu Linux 下使用基于 Qt 5.1.1 的 QtCreator 2.8.1。我同时做 Qt Development 和编写纯 C++ 项目。因为我不想为后者安装像 Eclipse 这样的第二个重量级 IDE,所以我想为此使用 QtCreator。

到目前为止,我通过单击 File->New->C/C++ Project (without Qt)->C++-Project 创建了一个新的 C++ 项目(没有 Qt)。所以现在编码工作正常。但是在我的第一个构建中,我发现我的 main.cpp 存在一个巨大的问题

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream infile("file1.in");
    string line;

    while(infile >> line) {
        cout << line << endl;
    }

    cout << "hello world" << endl;

return 0;
}

当我在 QtCreator 中运行它时,我在控制台上只看到“hello world”,但仅此而已(file1.in 包含超过 20 行原始文本)。现在奇怪的是。在用一点 g++ main.cpp 编译后,我看到了我所有的 20 行文本和“hello world”。

有人知道为什么会这样吗?我以为我必须更改 QtCreator 中的编译器,但这种尝试没有成功。

最佳答案

QtCreator 默认使用影子构建,这意味着您的可执行文件将构建在单独的文件夹中。在您的代码中,您使用文件的相对路径:

ifstream infile("file1.in");

只需更改它的完整路径或确保您的 file1.in 存在于项目的影子构建文件夹中。

ifstream infile("C:\\file1.in");

关于c++ - 使用 QtCreator 构建纯 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19752253/

相关文章:

c++ - 如何避免辅助函数被警告? "xxx defined but not used"

qt - 使用 QGraphicsVideoItem 将 QGraphicsScene 渲染为 QImage

qt - 在 Qt 中与 libpng 链接

cmake - qtcreator cmake "No executable specified"

c++ - 插入未打开的流中的数据会怎样?

c++ - 洗牌导致两只手相等

qt - 如何更改 Qt 5.9 虚拟键盘布局/区域设置

Qt Creator 全屏运行

c++ - 当/MT 和/MD 都需要时怎么办?

c++ - 有没有办法使用 glDrawPixels 渲染单 channel 灰度图像?