简体   繁体   English

使用QtCreator构建纯C ++

[英]Pure C++ build with QtCreator

At the moment I am using QtCreator 2.8.1 based on Qt 5.1.1 under Ubuntu Linux. 目前,我正在Ubuntu Linux下使用基于Qt 5.1.1的QtCreator 2.8.1。 I do both, Qt Development and coding pure C++ projects. 我同时做Qt开发和编码纯C ++项目。 Because I don't want to install a second heavyweight IDE like Eclipse for latter I'd like to use QtCreator for that. 因为我不想为后者安装第二个重量级的IDE,例如Eclipse,所以我想使用QtCreator。

Up to now I created a new C++ Project (without Qt) by clicking on File->New->C/C++ Project (without Qt)->C++-Project . 到目前为止,我通过单击File->New->C/C++ Project (without Qt)->C++-Project创建了一个新的C ++ Project(无Qt)。 So now coding works fine. 所以现在编码工作正常。 But with my first build I discovered a huge problem with my main.cpp 但是在我的第一个版本中,我发现了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;
}

When I run this within QtCreator I see only "hello world" on the console, but nothing more (file1.in contains more than 20 lines of raw text). 当我在QtCreator中运行此程序时,我在控制台上仅看到“ hello world”,仅此而已(file1.in包含20多行原始文本)。 Now the strange thing. 现在奇怪的事情。 After compiling this with a little g++ main.cpp I see all my 20 lines of text and "hello world". 用一点g++ main.cpp编译之后,我看到了我所有的20行文本和“ hello world”。

Does anybody have a clue why this happens? 有人知道为什么会发生这种情况吗? I thought I have to change the compiler in QtCreator, but this attempt wasn't successfully. 我以为我必须在QtCreator中更改编译器,但是这种尝试没有成功。

QtCreator by default use shadow builds, it means you executable will be build in separated folder. QtCreator默认使用影子构建,这意味着您的可执行文件将在单独的文件夹中构建。 In your code you use relativ path to the file: 在代码中,您使用文件的相对路径:

ifstream infile("file1.in");

simply change it fullpath or make sure your file1.in exist in the shadow build folder for your project. 只需更改它的全路径,或确保您的file1.in存在于项目的影子构建文件夹中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM