gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:55:39 25 4
gpt4 key购买 nike

目前我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com