code in notepad/run from command prompt

Hello guys.i have some sort of question,and it may sound stupid or pointless,but i am curious and willing to try it.especially because i like using the console.

So i read somewhere that you can write the source code in notepad for a program of any kind,then u need a compiler(i think it needs to be installed or something) then u can run you're program straight from the cmd prompt.

I now today everyone is using a IDE (me to :)) because it is the most simple way of coding and it has everything that u need.it compiles,it link edit's,all that..but if someone knows or tried this "method" and can give me some help on what do i need to do(because i couldn't find anything solid on google) i would appreciate it.TY
There's nothing magic about using an IDE. Just download and install one, create a new project and the rest should be self-explaining.
Good way to start: http://www.codeblocks.org/downloads/26
I'm doing exactly what you're talking about. I'm using Notepad to code my programs, and then I compile directly in the console window by using MinGW. Try going to mingw.org and installing the latest version. If you have any problems, let me know.. =)
closed account (Lv0f92yv)
IDE's are great.... Though if for some reason you still want to do it oldschool;

Linux systems are easy to use the shell to compile/run code...

g++ *.cpp -Wall will compile all .cpp files in the current directory and produce a .out file. ./a.out at the shell will run your program.

Linking libraries and such is more complicated, and I don't know how to do it on the commandline.

Windows probably has something similar (or use minGW GNU compiler on windows for similar behavior).
It's a bit offtopic but you do linking with gcc/g++ aswell, Desh. Example:

g++ -c -o something.o something.cpp //-c switch for only compiling
gcc -o something -lSOMELIBRARY something.o /will link something.o to something using SOMELIBRARY. On linux /usr/lib/libSOMELIBRARY.so ...

Back to topic: On Windows it works very similar with MinGW, Cygwin or Windows Services for UNIX by Microsoft. All three come with the gcc compiler.
closed account (Lv0f92yv)
Thanks for that, JoR.
khris i tried to install a MinGW (GCC) Compiler Suite just like u said from that website,but after reading all the instructions i soon got confused :)))..i did this
 
Download the mingw-get installer and extract/rename the files to the C:\MinGW directory.

then it asks me to put some PATH is some place,but i did not find it.and it keeps me redirecting to other pages where there are other instructions.
1
2
3
4
Add C:\MinGW\bin; to the PATH environment variable.
Use the mingw-get command to install MinGW as explained in the mingw-get release notes.

Following these instructions will install GCC version 4.5. See also Getting Started for more detailed instructions.


so i am starting to loose my courage :P.
isn't some other way,or some package that i can install a little more easy??..
if not,,then i will just go back to my visual studio,like many people out there, TY
Last edited on
Mihay07 wrote:
so i am starting to loose my courage :P.
isn't some other way,or some package that i can install a little more easy??..TY

http://tdm-gcc.tdragon.net/

Or you can just download Code::Blocks, it already comes with Mingw Compiler Suite. You can still compile your code in the command line if you want to.
This is like running a linux shell on top of windows, actually it just has a bunch of dll's it uses (it still runs off windows, not a linux kernel)

http://cygwin.com

The cygwin window command prompt is the same as windows cmd. However it contains all the standard unix tools (1000+), plus other benifits such as X windows, gnome, and kde as well as all other compilers, java, pearl, python, QT, C, ddd, gdb, and the like. As well as all the typical Oracle and PostgreSQL databases/libraries.

And you can get the source for about 97% of these 1000's of programs.

Additionally if you add the C:\Cygwin\bin folder to your MS windows environment variable path then your 'windows command prompt' has the same functionality as the cygwin command prompt(minus a couple little things).


But IDE's are the way to go, why program in Notepad or Vi (Linux's Notepad) when you can take the easy road.
MS Visual studio is ok (it's not great, learning to use it can be painful, as well as it likes to tie nubs to creating windows only applications)
Codeblocks is great and works well. Eclipse is also good, however getting DDD (debugger) to run on it can be painful sometimes.
Last edited on
ok ty all..tried code blocks and works great,but i still need to get used to the interface,the debugger,watch variables,and the colors.blue for text :) that's strange ..and another thing i liked about it is that finally someone was able to make a ide that stops the console from closing down and showing the execution time for it,without putting getchar() and system things that are "good" in some evil way..So ty again and keep up the good work.


1
2
"REAL PROGRAMMERS don't comment their code. 
If it was hard to write, it shouldn't be hard to understand."


i still miss the words in capital letters....the rest is just fine :)
Last edited on
Mihay07 wrote:
then it asks me to put some PATH is some place,but i did not find it.and it keeps me redirecting to other pages where there are other instructions.


This is what I was also talking about: Here's a step by step guide. It's very easy, and it's IMPORTANT TO KNOW

1. Open control panel. (start menu -> control panel) or (start menu -> accessories -> system tools -> control panel) or (My Computer -> Control panel) or (right click My Computer -> Properties. If you do this last one, skip step 2)

2. Click "System"

3. Click "Advanced System settings" on left hand panel.

4. The "System properties" window is now open. And you should have the "Advanced" tab already focused. (This happens by default when you click advanced system settings from system from control panel). If not, then click the Advanced tab. (tabs are near top of window, below menu).

5. The "Environment Variables" button is located on the bottom of the "Advanced" tab, click it.

6. This opens the "Environment Variables" window.
6.1 The top section is "User variables for Admin", you can safely ignore this.
6.2 The bottom half is "System variables". Scroll the scrollbar down until you see "Path" or "PATH". (note: not 'pathext' or 'winPath' or anything else, just "path")

7. Click on path and click edit. (Note: don't delete anything in this field, read the next part.)

8. This opens up a new window called "Edit system variable". Now the name of the variable will be "path" and this is the top field, the second field will be "value" / "variable value" and this will be highlighted text, press the 'right arrow key' on your keyboard, or simply click in the text field and move the cursor to the end of the text string.

9. Now if you look over the text string you will notice that each field is separated by the field delimiter ";" semi-colon. So if there is not already a ';' (minus the commas) at the end of the string put one in.

10. Now open up a separate folder/window, and navigate to the MingW, or Cygwin, or java, or you home path, or whatever binary files ( .exe's ) in a folder you want to be able to use in the command prompt. Click on the folder address bar, and copy the address (Ctrl C).
No go back to the "edit environment variable" window again, and paste (Ctrl V) the address of the folder into the end of the text string. You want this at the end, you don't want this to replace what is already there. Then place a semicolon ( ; ) at the end again if you like, so you don't need to add it for next time. Press "OK", if you have pasted it correctly, press "cancel" if you deleted the text string by accident.


edit: Want to start up world of warcraft from the command line???
add the WOW folder to the environment variables window, and bob's your uncle.
Want to run your own programs you have written from command line without being in the same folder? guess what?..... yep
Last edited on
you can write the source code in notepad for a program of any kind


Why bother using notepad at all? Notepad is basically nothing more than a barebones IDE, minus the compiler. Why not do what I do?

Try the following from command prompt:

1
2
3
4
5
6
7
copy con hw.cpp
#include <iostream>
using namespace std;
int main(){cout << "Hello Waldo! " << endl; return 0; }
^Z
g++ hw.cpp
a

Where ^Z means ctrl + z + return.
gcampton you are great at explaining things,with patience.very few out there.so after that path it means that i can run everything that is in that folder?..and in the folder what i must have: the .txt file or the cpp with the source code?..and after that i open command prompt (start/run cmd) and what do i need to write so i can run it?..i see some interesting things "wtf" wrote above..and it may work but writing code in the console,and many instructions on a single line,i don't think is not the best way to go.so i stick with notepad..if i can make the damn thing to work :)
(i now there are too many questions in so few lines,but i am at lvl 0)
Last edited on
I did it that way for simplicities' sake. Alternatively you could have:
1
2
3
4
5
6
7
8
9
copy con hw.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello Waldo! " << endl; 
return 0; 
}
^Z
gcampton you are great at explaining things,with patience.very few out there.so after that path it means that i can run everything that is in that folder?..and in the folder what i must have: the .txt file or the cpp with the source code?..and after that i open command prompt (start/run cmd) and what do i need to write so i can run it?..i see some interesting things "wtf" wrote above..and it may work but writing code in the console,and many instructions on a single line,i don't think is not the best way to go.so i stick with notepad..if i can make the damn thing to work :)
(i now there are too many questions in so few lines,but i am at lvl 0)


Yes it means you can run/use EXECUTABLES (binary files, dll's, etc) included in the PATH that you give to MS Windows, Environment Variables.

So lets say (This is an example don't actually do it), you delete everything in PATH, and you then create a folder on your desktop named "Files" and in that you put some exe's that you have made: eg, "Notepad.exe", "Paint.exe" and "Mihays_UnZipp.exe".
If you go and place the directory path in the environment variables PATH.
C:\Users\Mihay\Desktop\Files
Then from the command line, if you type:
Notepad
Notepad will start up.

Now obviously you want the folder of the compiler's path in the PATH variable. So that way you can use the compiler in command prompt. so if you copy the folder path for the MingW compiler into the PATH variable everything should be fine.

Now you don't have to do this for your .cpp and exe files because what you do is simply "change directory" within command prompt to go to those files.

The same can be done for above, if you didnt put the compiler in the PATH, then you could also just change directory to the compiler and use it from there.

OK, Now what do I mean about changing directory? Really you should look up a tutorial on how to use command prompt, simple and common used commands.

But it goes like this, The command prompt does not see the whole system, it only see's the current folder it is in, and it's environment variables:
CD C:
this will change current directory to C drive.
CD C:\MingW\bin
will change the current directory to the binary directory for your compiler.

It's best to stick that directory in PATH though, because this then means you can use the compiler from any folder.

Lets say we have done that, now lets say you have some .cpp files you have created and saved in My Documents.

From command prompt you want to change to that directory, so::
C:\Users\Mihay\My Documents
This should and I say should lightly, as it has been many many years since I've used command prompt. This should change to your documents, and from here now you can compile your programs.

g++ myFile.cpp
Will turn that into an executable named a.exe if it contains a main method, any other files you compile in the same fashion will override a.exe.
to see the list of commands you can use with GNU you will need to read the manual, unfortunately this cannot be done within the CMD prompt, unless you took my advice and installed the unix tools (cygwin).

So you will need to google/ goto the website.
to name your program:
g++ myFile.cpp -o NotePad.exe

will change the executable name to NotePad when it compiles.

-c creates object files only.
-Wall, -ANSI -pedantic give maximum amount of warnings.

But you can look all this up yourself.


PS If you do decide to install cygwin, read how to do the full install it's about 7gb, normal version is about 1.2gb. Then search for unix terminal eBooks, linux help eBooks, etc.
"How to use Linux terminal".
(It's not called command prompt in linux)
Topic archived. No new replies allowed.