Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

c++ - How do I create a .exe from a .cpp file in Code Blocks?

I've just started learning C++, and to display the outputs of code I found this method. This worked when I first compiled Structure of a Programme.cpp:

#include <iostream>

using namespace std;

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

It gave me a .exe, which I opened, ran, and got a lovely 'Hello World!' appearing, but when I tried compiling a second one, Variables.cpp:

#include <iostream>
using namespace std;

int main ()
{
    int a, b;
    int result;

    a=5;
    b=2;
    a=a+1;
    result=a-b;

    cout << result;

    return 0;
}

I didn't get a .exe at all, so couldn't work out how to open it. I tried re-compiling Structure of a Programme.cpp (after deleting all the associated files), but now that won't create a .exe anymore, either. The only files created are Structure of a Programme.o and Variables.o (in a sub-directory objDebug).

The only question I could find that seemed similar was this, but the problem seems to be slightly different, and I tried deleting one of the files (so there was only one of Structure of a Programme.cpp or Variables.cpp in the folder) and I still had the same result.

Also, there were no compiler errors with either file, and I don't think I changed any options in Code Blocks between Structure of a Programme working and everything not working.

Thanks,

Dalkius

edit: Build logs:

Compiling: Structure of a Programme.cpp
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

Compiling: Variables.cpp
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

edit 2: 'Full Commandline' build logs:

Build started on: 14-12-2011 at 07:57.39
Build ended on: 14-12-2011 at 08:01.03
-------------- Clean: Debug in cplusplus.com Tutorial ---------------
Done. 
mingw32-g++.exe -Wall -g -c "D:My DocumentsHOMEProgrammingC++Code Blockscplusplus.com TutorialStructure of a Programme.cpp" -o "objDebugStructure of a Programme.o"
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings
mingw32-g++.exe -Wall -g -c "D:My DocumentsHOMEProgrammingC++Code Blockscplusplus.com TutorialVariables.cpp" -o objDebugVariables.o
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From looking at your updated build log, it appears the linking step isn't being performed to generate the final executable. There are a couple of things you can check and some ideas to try out:

  • Make sure the linker executable and proper path is set so C::B can locate it. For MinGW the linker is invoked through the compiler driver named 'g++.exe'. image
  • Check that 'Console application' is selected under 'Type'. image
  • If everything looks okay but it still doesn't link try creating a new blank console project. Add the existing files to that project and try building it.
  • Try building it manually from a 'cmd' command prompt to make sure the toolchain itself is functioning. You should find a 'mingwvars.bat' script under your mingw install. Run that script to open up a proper commandline environment. Do a simple test compile using this command:

cd "D:My DocumentsHOMEProgrammingC++Code Blockscplusplus.com Tutorial"
g++.exe -Wall -g Variables.cpp -o Variables.exe

Lastly, this is what your log should approximately look like when it's building correctly: C::B build log


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...