Thread: command line

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    command line

    how to run C++ code in command line ?

    compiler : DEV-CPP

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Do you mean you want to compile it at the command line or create a command line program?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    yes, i want to compile and run in command line. i dont want to use IDE.

    my compiler is DEV-CPP

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hello, I think there's a little confusion: devcpp is not a compiler, is an IDE; by default devcpp uses MingW to compile, so you can have a look at www.mingw.org in the section 'Documentation', subsection 'Compiling and building with...', where you will find the easy ways to doing it, different synthax to compile C, C++, outputs and other stuff.
    Sorry to not post a sample way to do it, but I always use the IDE
    Niara

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Thanks for the information.

    anybody here compile and run in command line . please give me a sample

  6. #6
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by Stream
    Thanks for the information.

    anybody here compile and run in command line . please give me a sample
    Meh. Not too difficult.

    Code:
    g++ blah.cpp -o blah.exe
    Using a library (OpenGL for example):

    Code:
    g++ blah.cpp -lopengl32 -o blah.exe

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    20
    DevC++ uses gcc (MinGW) for compiling, so it should already be installed on your system. Just add it to your PATH or cd to the dir it's in (C:\devcpp\bin?) and run it.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    hi, i have added C:\Dev-Cpp\bin in my PATH variable.

    after that how do i compile and run my cpp code ? my code is here c:\myfolder\Prog.cpp

    please tell me the syntax of compile and run command line instructions.

    Regards

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well at your command prompt, type

    g++ prog.cpp

    That should get you an exe file (prog.exe or a.exe say), which you then just run like you would any other program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Well at your command prompt, type
    g++ prog.cpp
    ok.
    i did

    Code:
    C:\>g++ prog.cpp
    
    C:\>

    so, it compiled fine. but did not get a prog.exe in c:\.......rather i get a a.exe file !

    and when i clicked a.exe....command prompt blinked away ...i could see nothing.


    so,

    Q1. how can i run the code (a.exe )?

    Q2. i dont like my exe's name to be a.exe.........can not i get it with the name prog.exe ?

  11. #11
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Q1. how can i run the code (a.exe )?
    The same way you call any other executable. If you click the icon then a process is spawned and and console window created for the program to run in. When the program is done, the process is destroyed and the window along with it. To keep the window open, you need to keep the program running. The most common way to do that is a blocking read:
    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main() {
      // Your program here
    
      // Discard leftover characters in the stream
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      // (Hopefully) blocking read
      cin.get();
    
      return 0;
    }
    Alternatively, you can run the program from the command line. That way the process and window belong to the command prompt, not the program, so it isn't destroyed when the program ends.
    Q2. i dont like my exe's name to be a.exe.........can not i get it with the name prog.exe ?
    a.exe is the default if you don't provide a name, just like on *nix it's a.out. You can change the name with the -o switch:
    Code:
    C:\>g++ -o prog.exe prog.cpp
    While you're at it, say -Wall and -pedantic too. Those switches help you catch bugs:
    Code:
    C:\>g++ -Wall -pedantic -o prog.exe prog.cpp
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed