Working from the command line.

Hello,

For several reasons, I'd like to start working from the command line rather than always having to go through the IDE. The MSDN articles were a tad short and got me nowhere. Can anyone provide a website/article with an introduction/tutorial?
What are you after in specific? Editing, compiling and running?

Last edited on
Try MingW, if you are on Windows. Sorry, I cannot give more help due to lack of info on your part. OS?
iHutch105 wrote:
Editing, compiling and running?
MSDN would indicate that it is Windows.
Last edited on
I did not notice that. Is MingW the best cmd compiler?
All compliers (barring really specialist hardware compilers on things that don't have an actual command line) run from the command line. Here's how to do it using the compiler that comes with MSVS.

http://msdn.microsoft.com/en-us/library/ms235639(v=vs.80).aspx
Last edited on
Wow, i never knew that was possible, and all these years i have avoided the "Visual" products
Basically, you just need to get used to using the command line itself. Since you are on Windows, it is also useful to set up some stuff to help you out.

MinGW comes with a nice utility called MSYS, which is, essentially, a bash shell that does its best to do the right thing under Windows. (You can use it when you need to configure and compile programs using common *nix tools, like autoconfig.)

I tend to say with the MS Console window for most tasks. I have a shortcut that has a "target" of
C:\Windows\System32\cmd.exe /k D:\Michael\bin\prompt.bat
and positions the prompt to be nice and big (instead of that little-bitty window that comes up by default.)

The file "prompt.bat" sets up my command path, doskey macros, and other things. For example, here's the (very simple) one I have set up on my wife's PC:
@echo off
set path=d:\Michael\bin;%PATH%
set dircmd=/ogn
doskey >nul
doskey cp=copy $*
doskey lesss=less -S $*
doskey deltree=rd /s/q $*
doskey vi=notepad $*
doskey dird=dir /ad /d $*
doskey wish=tclkit-8.5.7-win32.upx.exe
doskey tclsh=tclkitsh-8.5.7-win32.upx.exe

The directory I use as home, "D:\Michael\bin" contains a lot of nice utilites, such as "unzip.exe" and "tar.exe" (GNU Tar for Windows), as well as batch files that help me modify my environment for the current task.

For example, "D:\Michael\bin\g++.bat" contains:
@echo off
set PATH=C:\MinGW\bin;%PATH%
g++ %*
This has the feature that it only gets called once, where it sets-up my GCC environment. After that, the compiler gets called directly. (This doesn't just apply to the GCC, you can do this for any additional environment.)

You should make sure that the cmd.exe extensions are enabled as well. This makes doing certain things in the command prompt much easier.

I use my old Delphi 5 IDE to edit everything, then switch to the command prompt to compile and run, etc.

Well, that should be enough to get started. Hope this helps.
I'm quite ashamed to see what little information I provided. I'm sorry about that.

As iHutch has figured out, it's on Windows, and I'm mainly looking to compile and run from the command line.

I've tried using the built-in VC++ command line; I can get the compiler to accept a filename and try to compile it (like in the MSDN article Moschops linked), but since my project is a multi-file project, none of them can be compiled separately.

Is it advisable to go with MinGW?
Everything compiles separately.
Once you have object files (.o or .obj) for each source (.cpp, etc), then you use the linker to combine them into an executable.

Typically, to compile multiple files "at the same time" you just list them all on the command line, like: "g++ main.cpp foo.cpp bar.cpp".

Hope this helps.
To run, just type the executable name. Anything you type after that, space delimited, will go in as arguments to main().
Ah, the errors made me think the compîlation was unsuccessful, but linking afterwards lead to a working .exe file. Thanks!

Few more questions:
a) Is there an easier way than typing in all filenames when compiling and linking? Provided I generally wish to link all files in a directory, an "all" flag would be handy.

b) The "command line" in my IDE has quite a few flags, some of which I chose and some of them that were there long before I knew what they meant. Is it worth looking them all up and using just the important ones, or can I just assume my IDE knows better and use them all for now? Is there a way to set "standard compile settings"?

c) Is there a way to make sure the compilation process puts the .obj files into a certain directory?
a) I think you can write *.cpp instead of listing all filenames in the current directory that ends with ".cpp". I think it works on Windows.

g++ -o run.exe *.cpp
Last edited on
closed account (S6k9GNh0)
Gaminic, using a build system simplifies this. cmake, for instance, can generate nmake files and most others can work with the compiler directly.
Last edited on
hi. you can study the src in my script that i wrote a few weeks ago to do just this.

http://www.github.com/iKlsR/pclc

most intuitive and quickest is to install mingw and use
g++ -o "new filename if any" "filename .extension"
I find scripts are my friend working with any CLI.

Granted I usually use Unix, though, so my knowledge of the Windows CLI is a little limited. That said, I'm sure it'd be simple enough to knock up a few scripts with a bit of Googling.

Does Windows CLI support aliases?

EDIT: Guess it does. http://blogs.msdn.com/b/robdelacruz/archive/2004/08/22/218499.aspx
Last edited on
Thanks for all the answers and links. I've got plenty to begin with, so I'll mark this as solved, but feel free to add more sources and tips!

Thanks a lot, everyone!
The *.cpp thing is not guaranteed to work on Windows, but IIRC the GCC includes a hack to accept it. (It works on *nix because the shell automagically expands it before invoking the command.) I've never used it though.

You ought to google around "makefile"s and learn to write a basic makefile to compile your code properly. Then all you need to type at the command-line to compile everything is "make".

A simpler option is to just make yourself a little batch file to do the job. Something like:

cc.bat
@g++ -Wall -std=c++0x -pedantic main.cpp foo.cpp bar.cpp -s -o quux

or something like that (everything you would normally type at the command-line). Then, instead of having to type everything over, just type "cc".

Hope this helps.
Topic archived. No new replies allowed.