We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,687 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to run c++ program through command prompt?

Well i use Dev-c++ 4.9.9.2 for programming..i m using it from last two years...recently i learned java and saw that all java programs are run through command prompt. I tried tha same in c++. i saved my program in dev-cpp/bin .but i am not able to do this..what is the exact procedure please tell me.. and How to pass command line argument. Is there any limitation on the type of arguments passed to main. like i saw somewhere int main(int argc,string args[])

8
Contributors
9
Replies
4 Years
Discussion Span
10 Months Ago
Last Updated
25
Views
rajatC
Junior Poster
109 posts since Dec 2007
Reputation Points: 14
Solved Threads: 7
Skill Endorsements: 0

Here's a couple links to command line argument help:
http://malun1.mala.bc.ca:8080/~wesselsd/csci161/notes/args.html
http://www.cprogramming.com/tutorial/lesson14.html

As to running your program, be sure you opening the command window in the folder where the .exe file is located. That's the file you execute as in

c:\dev-cpp\bin\> myprog.exe 

If you are unfamiliar with working at the command prompt, here's some help:
http://www.claymania.com/dos-primer.html

Val

vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6

thanks for the links...they are really useful...
but my problem is that i dont know what are commands to compile and run the cpp program..

ok see
steps are
1.save file in bin
2.run command prompt
3. type in cd c:\dev-cpp\bin now i m in the same directory where the compiler and the program is saved...

now what next to compile my porgram named test.cpp

in java i know those commands they are

cd c:\program files\java\jdk1.5\bin
c:\program files\java\jdk1.5\bin> javac filename.java
c:\program files\java\jdk1.5\bin> java filename this is a test program

...
output

the line THIS IS A TEST PROGRAM in the third line is a command line argument..

i wanna know the cpp equivalent for this..

rajatC
Junior Poster
109 posts since Dec 2007
Reputation Points: 14
Solved Threads: 7
Skill Endorsements: 0

on linux, i would do

# g++ -o foo foo.cpp
# ./foo this is a test program
bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
Skill Endorsements: 0

thanks for the links...they are really useful...
but my problem is that i dont know what are commands to compile and run the cpp program..

ok see
steps are
1.save file in bin
2.run command prompt
3. type in cd c:\dev-cpp\bin now i m in the same directory where the compiler and the program is saved...

now what next to compile my porgram named test.cpp

in java i know those commands they are

cd c:\program files\java\jdk1.5\bin
c:\program files\java\jdk1.5\bin> javac filename.java
c:\program files\java\jdk1.5\bin> java filename this is a test program

...
output

the line THIS IS A TEST PROGRAM in the third line is a command line argument..

i wanna know the cpp equivalent for this..

You don't have to compile it from the command prompt. In fact in the dev environment click the compile button (not the compile AND run button) and that will give you an executable. Then all you need to do navigate to that path using cd c:\dev and run your program from there.

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046139290&id=1043284392

iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 33

@iamthwee

i know how to do it through dev environment...i just want to learn how to do it through command prompt

rajatC
Junior Poster
109 posts since Dec 2007
Reputation Points: 14
Solved Threads: 7
Skill Endorsements: 0

After creating the command prompt window you have to add the dev-cpp\bin to your PATH environment then execute make.exe

c:\Dev-C++:>make -f makefile.win <Enter>

The makefile.win should have been generated by the Dev-C++ IDE. If not, then you will either have to learn how to create it manually (its a big pain in the a**) or let the IDE do it for you. Alternatively you can type all the commands in the makefile yourself each time you want to compile the program -- which is another big pain in the rear.

Ancient Dragon
Achieved Level 70
Team Colleague
32,109 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 68

Another way,(I'm in a programming class) is to download MinGW for your computer. I don't have the link ready on hand. But regardless, download MinGW and make sure you know which harddrive it downloads into (Commonly in C:\) then when in the Command Prompt, type in:

set PATH=C:\MinGW\bin

Exactly(PATH I'm not so sure about) go to the area where your file is located(in the command prompt). type in:

g++ (nameoffile.cpp)

Or just type the first few letter of the file name and hit TAB on the keyboard. The file name should appear and then just hit Enter/Start above the Shift Key.
Your program should start.

AlexOverLord
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks for your response to a three-year-old thread :)

For anyone else who stumples upon this thread, I don't recomment Dev-C++ any more because its a dead IDE and uses an old compiler. Get Code::Blocks.

Ancient Dragon
Achieved Level 70
Team Colleague
32,109 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 68

I was browsing for answers but just thought hmm....What about just add

/* "My Code...." */

This just comments everything between /* ..... */

Then remove /* .....*/ if I want to run the specific code later on.
That way you do not have to eliminate your old code by using Exclude From Project :)

joaquince
Newbie Poster
1 post since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0807 seconds using 2.69MB