Run C and C++ on Bloodshed (Windows)

Download and install Dev-C++

Go here, scroll down, and download “Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2” from SourceForge.

  1. You have chosen to open devcpp-4.9.9.2_setup.exe. Save File.
  2. Please select a language. English. OK
  3. I Agree.
  4. Select the type of install: Full
  5. Destination Folder: I simply agreed with C:\Dev-Cpp.
  6. Do you want to install Dev-C++ for all users on this computer? Yes
  7. Your config files will be stored in C:\Documents and Settings\winguest\Application Data\Dev-Cpp
    Otherwise, you can pass the following parameter to Dev-C++: -c c:\config_file_directory
  8. Select your language: English (Original).
  9. Dev-C++ can retrieve information from header files.
    Yes, I want to use this feature.
    Yes, create the cache now.
  10. Close the annoying Tip of the day.
  11. FileExit

You have just created an application named Bloodshed Dev-C++ containing the GNU compilers for C and C++.

Create and configure a Windows folder

Create a folder to hold the files of your program.

  1. To create the folder, right-click on the desktop.
    NewFolder
  2. Name the folder hello.
  3. To see the filename extension for each file that you put in the in the folder, open the folder by double-clicking on it.
    ToolsFolder Options…ViewAdvanced Settings
    Uncheck the check box labeled “Hide extensions for known file types” (about halfway down the list).
    Press the OK button at the bottom of the Folder Options window.

Download a one-file C++ program into Bloodshed

We will put the program into the directory we just created.

  1. Launch Dev-C++:
    startProgramsBloodshed Dev-C++Dev-C++
    Close the annoying Tip of the day.
  2. A project is the thing that holds all the files that constitute your program.
    To create a new project to hold your program,
    FileNewProject…
  3. In the New Project window, make sure the Basic tab is selected. Press the Console Application button.
    Name the project hello.
    Press the C++ Project radio button.
    Press the Ok button.
  4. In the Create new project window, where is says Save in:, browse to the hello folder that you created.
    File name: hello.dev
    Save as type: Dev-C++ project (*.dev).
    Press the Save button.
  5. In the Dev-C++ 4.9.9.2 window, remove the existing main.cpp file from the project. Do not bother to save it.
    ProjectRemove from Project
    Select the file to remove from the project: main.cpp
    Press the Ok button.
    Don’t save changes to main.cpp.
  6. Point your web browser at the first C++ program in the in-class examples: http://i5.nyu.edu/~mm64/INFO1-CE9264/src/hello.C
    Save this file as main.cpp in the hello folder you created.
    Then open the hello folder and examine your main.cpp with Wordpad. It should look exactly like this.
    If the < characters in the original file were changed to &lt;, delete the file and download it again. This time, say
    View → Source
    in your browser, and download the source as main.cpp.
  7. Add the main.cpp file that you just downloaded to the project.
    ProjectAdd to Project
    Select main.cpp and press the Open button.
  8. Open the file main.cpp.
    File → Open Project or File…
    Insert the statement
    	system("PAUSE");
    
    before each return from the main function. If your program does not have the statement
    using namespace std;
    
    you will instead have to insert
    	std::system("PAUSE");
    

    File → Save
  9. ExecuteCompile & Run
    Hello, world!
    This is an error message.
    Press any key to continue . . .
  10. Before you “press any key to continue”, right-click on the black window and Select All. You can then paste the entire content of the window into another application. Then press any key to continue. You must close the black window before you can run your program again.

  11. If there are warnings (bad) or error messages (worse), two things you can try are
    ExecuteRebuild All
    ExecuteProgram Reset

If you would rather compile your program by typing a command line, here is how to see the command line that Bloodshed uses to invoke the compiler. Click on the Compile Log tab at the bottom of the Bloodshed window and look for the command line that starts with g++.exe. On my machine (Windows Vista), the executable file g++.exe was in the directory C:\Dev-Cpp\bin.

Run the program from the Windows Command Prompt

After your C or C++ program has been compiled by Bloodshed, you can also run it in the Windows Command Prompt without Bloodshed. This will let you get teh program’s exit status, and direct its standard output and standard error output into files.

  1. startProgramsAccessoriesCommand Prompt
  2. cd to the hello directory that you created. On my machine, the directory was C:\Documents and Settings\user1\Desktop\hello.
  3. dir
    
    hello.exe
    Press any key to continue . . .
    
    echo %errorlevel%
    0
  4. To direct the program’s standard output and standard error output into separate files,
    hello.exe > output.txt 2> errors.txt
    Press any key even though you don’t see the “any key” message.
    
    dir output.txt errors.txt
    type output.txt
    del output.txt
  5. To direct the program’s standard output and standard error output into one file,
    hello.exe > both.txt 2>&1
    Press any key even though you don’t see the “any key” message.
    dir both.txt
    

Tell the C compiler where to find the file cc1.exe

If your C compiler (gcc.exe) complains that it cannot find or execute the file cc1.exe, tell the compiler where to find this file.

  1. Discover where cc1.exe is.
    startSearchFor Files or Folders…All files and foldersAll or part of the filename: cc1.exe
    For example, the file cc1.exe is in the directory C:\Dev-Cpp\libexec\gcc\mingw32\3.4.2 on my machine.
  2. Add the directory to the list at
    ToolsCompiler OptionsDirectoriesBinaries

Run the Debugger

  1. Disable class browsing: it crashes the debugger.
    ToolsEditor OptionsClass browsing → Uncheck “Enable class browser” and press Ok.
  2. ProjectProject OptionsCompilerLinkerGenerate debugging information → Yes and press Ok
  3. FileSave
  4. ExecuteCompile
  5. Click Debug tab on the left, Debug checkbox below.
  6. Click on the left marin of your cpp file to set a breakpoint.
  7. Run to Cursor to execute up to the breakpoint and stop.
  8. Add WatchEnter variable name. You should see the variable’s name and value in the left panel under the Debug tab.
  9. Next Step to go one statement forward; or Continue to go to the next breakpoint or all the way to the end of the program. The next line to execute is in dark blue. When you reach the end of the program, remember to bring the “Press any key to continue…” window to the front and press any any key.

A C++ program that calls functions written in C (Chapter 1, p. 86, Homework 1.7.3a)

  1. Create a folder and name it term.
    Create a project, name it term, and save it in the folder.
  2. Remove the existing main.cpp file from the project.
    ProjectRemove from Project
  3. Using your web browser, download the three source files from Chapter 1, pp. 85–89, term.h, term.c, and main.C.
    Save them in the term folder, saving main.C with the name main.cpp.
  4. Add the three files to the project.
    ProjectAdd to Project
  5. To tell the computer that the term.c file is written in the language C, not C++, do the following.
    ProjectProject OptionsFilesterm.c
    Uncheck Compile file as C++ and press the Ok button.
  6. In the term.c file, uncomment the #define MICROSOFT.
    FileSave
  7. In the main.cpp file, insert the statement
    system("PAUSE");
    
    immediately before the statement return EXIT_SUCCESS;.
    FileSave
  8. ExecuteCompile & Run