tags:

views:

517

answers:

10

I would like to know what I should use to compile in C. I am brand new to programing in general and would greatly appreciate a comprehensive explanation of this process. I am on Windows Vista.

I heard about something called "djgpp" that is free and effective for windows.

+5  A: 

Depends on which platform, but on Linux, Mac OS X and similar (including Cygwin), gcc is most common.

Say I have a script named myapp.c:

#import <stdio.h>

int main(){
        printf("Hi!\n");
        return 0;
}

To compile it from the command line using gcc is very simple, just run:

gcc myapp.c

..that outputs the binary as "a.out" (the default filename). To run the compiled app, just do..

./a.out
#which outputs: Hi!

Or put it somewhere within your $PATH (for example, /usr/bin or ~/bin/) and run a.out

You can specify the output filename with the -o flag:

gcc myapp.c -o myapp
./myapp
# output: Hi!

As for more complete guides, "Learn C for Cocoa" is a good, quick guide on the C language, and covers compiling. If you're interested in learning ObjC, the next part of the guide is also great.

On Windows, I would recommend looking into Visual Studio, specifically the Express editions (which are free).

There's no "Visual C", but the Visual C++ edition will compile C code (since C++ is an extension of C). There's also an option to "Compile as C code" somewhere in the build options.

Finally, The C Programming Language book covers, well, everything.

dbr
+5  A: 

For the answer to this question and many others you may have as you start out, try this website which has beginner tutorials. Here's the page on compilers and getting set up. An excerpt on what compilers you can use:

Windows/DOS

  • Code::Blocks and MINGW
  • Borland
  • DJGPP
  • Dev-C++ and Digital Mars

Windows Only

  • Microsoft Visual C++
  • nix
  • g++ is a C++ compiler that comes with most *nix distributions.
  • gcc is a C compiler that comes with most *nix distributions.

Macintosh - XCode

~~~

Personally, I'd also recommend you strongly to read The C Programming Language by Kernighan and Ritchie. It will really help you understand both core principles of programming as well as C-specific details.

mandaleeka
That Mac information is incredibly out of date. For Mac OS X, you would use Xcode, available from http://developer.apple.com/ (account required, but free). Even if you end up using gcc/g++ from the command line, you would install those same developer tools to get them.
smorgan
Yikes! This document if *far* out of date for macs. MPW belongs to the pre-OS X era. Modern Mac OS's use XCode (or make/gcc from the command line like any other unix).
dmckee
Yes, I just noticed after pasting the data. Shame on me; I'm actually typing this response with XCode running.
mandaleeka
@smorgan: XCode comes on the Developer Tools DVD with the operating system these days, though you do want to download the most up-to-date version available, of course...
dmckee
Xcode uses gcc (and the Developer Tools install the command line version)
dbr
Though xcode is switching to clang, sometime in the future if I'm not mistaken.
Wergan
+2  A: 

If you're on Unix (i.e. Linux, Mac OS X, FreeBSD, Windows under Cygwin, etc.) I recommend the GNU Compiler Collection (gcc). To compile a file hello.c, run this at the command prompt:

gcc hello.c

This compiles hello.c to the executable a.out. If you want a better-named executable, try this:

gcc -o hello hello.c

This compiles hello.c to the executable hello. Some more helpful flags:

gcc -Wall -Wextra -o hello hello.c

This turns on lots of helpful warnings that will probably help you catch bugs in your code.

On Windows, many people use Microsoft Visual C++, but I have no real experience with Windows. As I referenced above, you can install Cygwin, and then use the above steps, but many prefer pure-Windows compiling. Also, MinGW is a native-Windows port of gcc. It's all a matter of preference, though.

Chris Lutz
+2  A: 

Welcome to programming!

You might want to start in the right track and install a copy of Linux in your box. Try Ubuntu cuz it's easy to install and learn

Next learn to use the console! It will help you so much in the next years.

Once ubuntu is installed you need to install C compiler and basic libraries just typing in the console

sudo apt-get install gcc build-essential

Then just create a C file and run

gcc myprogram.c -o myprogram

This will generate an exec file called yourprogram, run it using

./myprogram

Here are some useful links:

victor hugo
Note that installing any new operating system is potentially harmful, and should not be taken lightly.
Liran Orevi
"You might want to start in the right track and install a copy of Linux in your box." - Fanboi ;) You can learn programming just fine without ever seeing a Linux installation..
cwap
+5  A: 

Visual Studio Express is a free version of Microsoft's leading IDE for Windows, and it includes a C development environment (editor, compiler, debugger).

RichieHindle
+1  A: 

Ok Josiah, first of all, be sure to have a C compiler installed on your system. On Linux, Mac, there's a compiler installed just out of the box, called the GCC compiler. And on Windows, you need to instal one, I recommend you to instal an IDE (like devshed) that includes an editor, compiler, debugger... all that you need to develop a C/C++ application.

In order to compile over Linux/Mac, first of all you'll need a source file (a text file with your code) and save it with a .c or .cpp extension. Then open your terminal and type:

gcc <fileName.c>

You should replace <fileName.c> with your filename (the full path, or first go to the files location and just type the file name).

This will create a .o file (and object file) in the same path of your source file, this is the executable file for your computer!

On windows, if you already installed an IDE, is easier, you just click the compile button and there ya go!

I hope this to be helpful to you

Rigo Vides
Actually, on Mac OS X you need to install the developer tools, which is a separate install from the OS itself.
smorgan
OS X doesn't have a compiler installed by default - it comes with the developer's kit, which does come free with most Macs but is an optional install.
Chris Lutz
+1  A: 

For windows now you can use gcc as mentioned above but also MSC. You can get the compiler for free with the Visual Studio Express bundle for Visual C++ Express

Eugenio Miró
+3  A: 

I would recommend using gcc and getting comfortable with the command line, if you plan on doing C for any extended amount of time. *nixes probably have it built in, and on Windows you can install MingW GCC and use it the same way.

I find this helpful because I feel like C was designed hand-in-hand with Unix, so it's better to stick with the command line than use an IDE off the bat. Most tutorials will probably assume you are using gcc. Also, if you're just learning C with gcc, you just type gcc -o outputfile sourcefile.c, whereas if you're starting with an IDE you'll have to figure out how the IDE works.

On the learning front, definitely pick up Kernighan and Ritchie's "The C Programming Language" as it's the definitive book on C. Rather slim volume, and easy to read.

Just a note, another helpful compiler flag for gcc is -g, as in gcc -o output -g -Wall sourcefile.c. -g enables debugging symbols, which will be useful in the future when you want to start debugging with gdb. This is another reason why you should start with gcc rather than an IDE, it makes it easier to move on to gdb.

You could always use MS Visual C++, but I think it's too complicated for a beginner. I personally recommend Notepad++ for editing as it doesn't do too much for you, but it formats the code nicely. Of course, once you become more experienced you might want to move on to a full-blown IDE with project management and such (or better yet, emacs ;)) but when you're just starting out simpler is probably better.

Ibrahim
+1  A: 

Invoking the compiler from the command line is fine for small apps as you're getting used to the C syntax, but as you move along, you will find that entering the myriad options gets tedious, and you will want to use a build tool. make is a basic but powerful way of compiling your projects easily. Editing makefiles by hand can be tedious, and will get in the way of your learning, so I'd suggest skipping right over that and go for the autotools toolchain.

TokenMacGuy
Autotools is such a bitch (pardon my language) to use, though. I wouldn't recommend it to a beginner.
Chris Lutz
+2  A: 

I would recommend downloading Microsoft Visual C++ Express Edition. http://www.microsoft.com/express/vc/ DJGPP will work as a compiler but does not provide an editor or coding environment. Compiling your code involves going to the command line. Some have suggested using gcc or MinGW but both of those programs behave similarly to DJGPP. Using a separate editor and compiling your code from the command line is not really the way that software is typically written in a Windows environment. Visual C++ will allow you to write and compile either C or C++ code.

Microsoft Visual C++ is a free of cost development environment which includes an editor and a compiler. Simply download the installer executable and run it to install. The installer will download the necessary software from the Internet and install it on your computer. The installer will download about 200MB of software so it may take up to an hour to download depending on your connection speed.

Once you have Visual C++ installed, start a new project and write your code. You can then compile and build it right from inside Visual C++ and see the output. Start by writing a simple command line program and then work up to more interesting programs.

John Scipione