Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How Do I Run C++ in Command Prompt?

I've searched google for hours, can't find an answer.

I'm taking a c++ class, I did a homework assignment, but I'm trying to test it. I don't know how to run a .cpp program in command prompt.

I've taken Java before, and I remember having to type javac filename.java >enter filename >enter (or something like that) in order for the Java file to run.

What is the equivalent of that for c++?

I found something saying things like cl/SHsc or something (I entered it correctly at the time, I just don't remember it now).

However, when I tried the command it didn't work. Only thing I can figure is the instructions were not detailed enough for me.

Please someone help me. Its so frustrating not being able to do the simplest part of c++ that I'm practically ripping my hair out.

3 Answers

Relevance
  • ?
    Lv 5
    7 years ago

    Depends on the compiler you are using, and the OS you are running on.

    On my Linux system, the command to compile hello.cpp is:

    > gcc -lstdc++ hello.cpp -o hello

    The executable is written to "hello" (indicated by -o hello).

    The "-lstdc++" says to include the standard C++ libraries.

    To run the hello program from the command line:

    > hello

    If you are using an integrated development environment, you need to look at how to create an application which can be invoked from the command line.

    On Visual Studio, when I create a new project it gives me options for which type of project I want. I think the "Win32 Console Application" is what you select to have it do the setup to create an executable (with .exe extension) that you can execute from a command line. Or you might try a "CLR Console Application"

  • 7 years ago

    There are so many options you often don't see lists. For example, sometimes the executable file is called "a.out" by default, other times it is "programname.exe" or simply "programname", depending on the environment.

    You also need to know what kind of command window/prompt you need. Sometimes you need admin access to get to the directories where programs are stored by default.

    So *read the documentation that came with your compiler*. It is the best source of information. If you don't have a copy you should be able to find it online. If the instructions are lacking, as you noted, then find a support group for that compiler or IDE. You should include that information in questions, too, as we have no way of knowing what library/IDE you are using to compile your code.

    Note that many IDEs allow you to specify how to run the program, as in a command/terminal window, inside the IDE itself, etc. Sometimes code will not execute the way you think it will unless you run it in it's own window.

Still have questions? Get your answers by asking now.