Basic Commandline Commands
Ok, so most of you are probably new to the command line and as such have no idea how to do basic tasks like moving from one directory to another; let alone how to compile. So on this page, I will try to put down some of the basic commands that you will need as a reference. Most of these commands are the same if you are on Windows or Linux, but some are specific to one OS and I'll try to let you know.
Basic Directory Commands
- pwd - print working directory - this gives you the path to where you currently are.
- cd - change directory - this will move you from your current directory to the directory you list: e.g. cd documents - this will move you into the documents directory assuming you are 1 directory away.
- mkdir - make directory - this will make a directory or folder: e.g. mkdir c++ - this will make a directory called c++ in your current directory.
- ~ - this isn't a command so much as a short cut referring to your home directory, or C:\Users\dave\ in my case. So cd ~ will move me to my home directory, or cd ~\Documents will move me from where ever I am into my Documents directory
- ls - list files - this will list the contents of the current directory
- dir - see ls
Compilation Commands on Windows
- cl - this is the basic command to compile a single file and to compile multiple files.
- suppose you have a file called main.cpp and you want to compile it and make sure you have the syntax right, this will do it for you:
- cl is the compiler /c means just compile it, /EHs is the Error Handling protocol to use, if you leave it off it will complain. This should produce a file called main.obj if it works.
- Now to link this main.obj into an executable you would use this command
- /Fe names the executable, note there is not a space between /Fe and the name you choose, e.g. /FemyProgram, main.obj is the input to make the executable from
- Suppose you want to compile main.cpp and name the output executable myProgram, this will do it for you:
- cl /EHs /FemyProgram main.cpp
- cl is the compiler, /EHs is the same as above /FemyProgram names the executable myProgram. Notice there is no space between /Fe and myProgram. This should produce a file called myProgram.exe if it works.
Running your programs
If you have an executable that you have built and you want to run it, then all you have to do is type the name of the executable. So from our last example, to run myProgram, I would type myProgram.exe since on Windows executables end in a .exe extension.
At any point along the way for most of these commands you can press the tab key and it will autocomplete for you. If it picks the wrong completion, pressing tab again, will pick the next completion, if there is one. So to run myProgram.exe, I would type my then press the tab key and it will fill in .\myProgram.exe for me and then I press enter
Compilation Commands on Linux/Mac
If you are lucky enough to be running Linux, then you will need to use a different set of commands to compile.
- g++ -this is the basic compiler for c++ programs on Linux. Mac also uses g++; however with Mavericks, they have opted for clang++ but left the command g++.
- So to compile a file named main.cpp, you would use this command:
- g++ -c main.cpp -o main.o
- g++ is the compiler, -c means to compile the code, main.cpp is the name of the input file, -o main.o is the name for the output file.
- To then create the executable you would do this:
- This would then link main.o into the output file, -o, myProgram