0
votes

I'm a C++ new learner and want to set my PC to learn coding. But the compiler doesn't work after all the MinGW packages were installed, and it doesn't show what goes wrong. How can I get it work?

I'm using Windows 10 (64 Bit).

All the MinGW packages were installed: enter image description here

The Path was setted: enter image description here

Use g++ -v to test, it's Ok, on the cmd it shows:

C:\Users\shaun\Documents\cpp>g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe Target: mingw32 Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls Thread model: win32 gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)

But it doesn't work: C:\Users\shaun\Documents\cpp>g++ 1.cpp

C:\Users\shaun\Documents\cpp>g++ 2.cpp

C:\Users\shaun\Documents\cpp>

1.cpp is just a HelloWorld:

#include <iostream>

int main() 
{
std::cout << "Hello, World!";
return 0;
}

2.cpp is a simple loop:

#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n ;
    cout<<"please input the height"<<endl;
    cin >> n; 

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n - i -1; j++)
        {
            cout<<" ";
        }
        for (int j = 0; j <= 2 * i; j++)
        {
            if (j == 0 or j == 2 * i)
                cout<<"*";
            else
                cout<<" ";
        }
        cout<<endl;
    }

    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            cout<<" ";
        }
        for (int j = 0; j <= 2 * ( n - i - 2 ); j++)
        {
            if (j == 0 or j == 2 * ( n - i - 2 ))
                cout<<"*";
            else
                cout<<" ";
        }
        cout<<endl;
    }
    return 0;
}
1
How do you know it didn't work? What did you do to run the program? What was the output of that?Galik
Don't post images of text! Copy-paste it as text into the question itself instead. Also please read about how to ask good questions. Lastly please read this question checklist.Some programmer dude
As for your problem, how do you know it didn't work? Have you checked that there's no a.exe file generated? What if you list all files in the directory after building?Some programmer dude
@Galik cause it shows no output it the cmd, normally it daoes on the other computers.Shaun 2049
You have not shown the command to run the program. In your examples you only compile the program. Doing g++ 1.cpp simply creates the program. You then need to actually run it.Galik

1 Answers

0
votes

Building and running a C++ program is a multi-step process:

  1. Edit code
  2. Compile code into object file(s)
  3. Link object file(s) (and libraries) into executable program
  4. Run executable program.

For simple programs (like yours) step 2 and 3 can be combined, like you do.

The problem you're having is that you don't do step 4, you only build the executable file but you never run it.

If you don't explicitly specify an output file name, the executable program should be named a.exe, which you need to run:

> g++ 1.cpp
> a.exe

Note that when you then build 2.cpp you will overwrite a.exe with the new program.

If you want to name the executable file to something else, you need to use the -o option:

> g++ 1.cpp -o 1.exe
> g++ 2.cpp -o 2.exe

Now you have two different programs, 1.exe and 2.exe, each created from different source files.