tags:

views:

365

answers:

10

I want to have a deeper understanding of how C programmes are run.

But IDEs stops us from doing that.

So is it possible that I manually set up the environment and write code in text editors, and finally run it in a prompt?

If the answer is yes, how?

+1  A: 

I don't know what you mean. You want to write C programs in a text editor an then, compile?? Ofcourse, you CAN do that:

1- Write a C code
2- Get a compiler such as gcc (there is a windows version)
3- Compile it
4- Run it from console

But, I am not sure if that is what you want to know.

mRt
Can you be more specific about compile and run c code without IDE?
djgpp is a windows C compiler that could be used as well
Tom
see http://www.mingw.org/wiki/MinGWforFirstTimeUsers :)
RC
@Tom: DJGPP is not a Windows compiler; it is a DOS compiler with a 32bit DOS-extender. See: http://www.delorie.com/djgpp/v2faq/faq3_6.html
Clifford
+1  A: 

Of course. You'll need something like MinGW compilers set to compile your application (or you could use the IDE-provided compiler). Then just use CMD and execute appropriate compile commands.

Actually, every IDE provides just more easier way to do the same compilation.

Dair T'arg
+2  A: 

Of course! There are a bunch of C compilers available for Windows, a few one to get you going:

  • Visual Studio has an express edition which is free and comes with a compiler
  • MinGW is the Gnu C Compiler ready-to-run for windows
  • There are plenty of others, but the ones above should be .. enough for now

As for a text editor, you might want to choose one that comes with C-syntax highlighting, like vi, Emacs or some other text editor. Mastering an editor is by the way really useful, regardless of what your language is.

moritz
IIRC, the MS C++ compiler is free to download.
Alan
that's what i meant
moritz
Right, but you don't need visual studio to use the C/C++ compiler.
Alan
@Alan: but afaik you can only get the compiler as part of the VS package - I don't think MS offers a stand-alone version...
Christoph
@Christoph: Nothing stops you from using VC++ from the command line. The build log from and IDE build even shows you what command-line was issued for each build step.
Clifford
@Christoph The free "Windows SDK" from microsoft includes the 32 and 64 bit c/c++ compilers, you don't need VS of any form to use them
John Burton
See, stackoverflow is just the best place to learn something :-)
moritz
A: 

You just need to install the MinGW compiler and set path to the gcc executable and you're ready to go. Then you can write C code in editor and compile it in command line just like you would on Linux.

Mavrik
Can you give a demo on compiling and run ?
+1  A: 

Download Cygwin and make sure to install GCC, the GNU C compiler.

You can write your C programs in a text editor, compile them with GCC, and then execute them.

Alan
You can't really recommend the Cygwin behemoth just to allow command line builds. That really is a sledgehammer to crack a nut - it has a place for porting Linux code to Windows but is otherwise ill-advised to use it for native Win32 development. *All* compilers are command line tools whether driven from an IDE or not.
Clifford
+6  A: 

As others have said, install MinGW (I'm assuming you are using Windows) and put its bin directory on your path. Open a command line windows, and create a file with a text editor, such as notepad - call it foo.c:

#include <stdio.h>
int main() {
  printf( "hello world\n" );
  return 0;
}

Then, use the gcc compiler to compile it, creating an executable called foo.exe:

> gcc foo.c -o foo.exe

lastly, run foo.exe from the command line:

> foo.exe
hello world
anon
If he has an IDE installed that can build C programs, then he already has a compiler installed. Why go to the trouble of installing another?
Joe Gauterin
Nowhere in his question does he say he has an IDE installed.
anon
That's pretty much what I want!BTW,is it possible to add additional 3rd party libraries to MinGW?
@Unknown - Yes, of course - libraries are just libraries, it doesn't make any difference where they come from.
anon
How?I see lots of *.o and *.a under MinGW\lib
The .a files are libraries. However, you shouldn't really be adding your own libraries there, and this is really not germane to your original question.
anon
OK,let me open another question:)
A: 

If you're interested in how large projects are organised, you could try downloading the source code for something like Apache httpd or PHP. These are both in C. On Linux/Mac you can compile them from the command-line using a few simple commands (see the documentation).

CMG
+7  A: 

I don't see why an IDE should prevent understanding how a C program works. An IDE usually abstracts the building process, i.e. it creates build rules for you, runs the program and the debugger. This does not have any effect on the way the program itself works, it's just more "user friendly" (depending on how you define "user friendly").

You can always build programs without the help of an IDE. You can even use Microsofts Visual Studio from the command line, and you won't see the GUI at all. Similar for XCode on Mac OS X.

If you want to build a program without using any IDE, you basically write the source code the same way you do with IDE. You can even use the IDE as editor. Afterwards, you need to compile your program. IDEs usually have functionality that makes writing the source code easier like automatic completion of structure elements, basic syntax checking and the like. If the program consists of only one single source file this is usually rather trivial. If you have more than one source file (which should be true for almost everything other than the usual "Hello World" example) you can still do that manually. However, this is a tedious and error prone process. In the Unix world, make is the tool of choice to automate this. You can use MinGW to get such an environment on Windows. Cygwin is another alternative here. You could, however, also use nmake out of MSVC and write the input for that manually (never did that myself) -- it's basically the same a Makefile is for make.

Creating a Makefile can be non-trivial. There are several generators to make that easier. That's basically the same IDEs do by abstracting the build process. Examples for Makefile generators are the autotools (sometimes also known as "GNU build system") and cmake.

Building programs from the command line also does not have any effect about the program having a GUI or not. You can (assuming Windows again) run any .exe file from the command line. If it's a GUI application the GUI will show up, if it's a console application it will run in the console window.

bluebrother
+1  A: 

You can compile programs for Windows (if that's what he meant) from the command line using all the available tools in the Windows SDK (formerly called the Platform SDK, I believe) which is free to download from Microsoft. I think it has all the same tools Visual Studio has if not most of them.

Rob
Upvoting this as most of the other comments seem to be suggesting gcc based compilers, which are fine, but it's just as possible to get the microsoft ones, and most examples etc. for the windows platform is targetted at those so it's probably easier in many ways
John Burton
A: 

If you want to see how the build works, most if not all IDE's create a build log which shows the actual command lines issued to invoke the compiler/linker etc. Nothing prevents you from issuing these commands directly on the command line.

What an IDE typically also does for you is dependency management; this is difficult to maintain manually, and best left to the IDE for large projects.

You do not need to download and install any specific compiler or toolchain as some have suggested; the one you have with your current IDE will be sufficient. For example if you have VC++2008 (or 2008 Express), the command line tools are described here.

Clifford