代码块运行错误:构建失败

时间:2017-01-20 16:45:53

标签: c++

我是C ++的新手,现在正在使用代码块学习它(版本:codeblocks-16.01mingw-setup.exe)。我的测试代码如下:

#include<iostream>
#include<stdlib.h>

int main()
{
    int sum = 0, val = 1;
    // keep executing the until val is greater than 10

    while (val <=10 ) {
        sum += val; // short-cut assignment
        ++val; // add 1 to val
    }
    std::cout << "Sum of 1 to 10 inclusive is "
              << sum << std::endl;
    system("pause");
    return 0;
}

这些代码写在名为ex1.cpp的空文件中。然后我点击“构建并运行”进行测试。结果,另一个文件main.cpp(我没有写这个)弹出:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

附上屏幕截图,以便更好地检查:

enter image description here

2 个答案:

答案 0 :(得分:0)

您收到此错误的原因是您的编译器设置不正确。您需要确保使用GNU GCC MinGW Compailer.Go To Settings - &gt; Compiler并确保每件事情都与屏幕截图。enter image description here

解决常见的代码块问题:Link

答案 1 :(得分:0)

我在编码时确实做错了什么:

当我在项目中创建一个空文件时,它将在该项目中产生两个主要功能,其中一个是自动生成的“hello world”文件,这是C ++不允许的。

为了成功构建它,我所做的是覆盖main.cpp中的代码。

相关问题