How can we compile a C program using GCC C compiler and command prompt in windows 7?
Do somebody have any links for such a tutorial?
This question may seem so stupid, because I do not have much experience in programming.
How to compile a C program using command prompt?
Re: How to compile a C program using command prompt?
MinGW or Minimalist GNU for Windows is the best for this purpose. You may read following tutorials to get a clear idea on installing that and compile programs under Windows.
HOWTO Install the MinGW (GCC) Compiler Suite
Getting Started
MinGW for First Time Users HOWTO
On another topic I have seen you use CodeBlocks IDE. This is a good option to work with MinGW gcc.
HOWTO Install the MinGW (GCC) Compiler Suite
Getting Started
MinGW for First Time Users HOWTO
On another topic I have seen you use CodeBlocks IDE. This is a good option to work with MinGW gcc.
- SemiconductorCat
- Major
- Posts: 455
- Joined: Mon Aug 22, 2011 8:42 pm
- Location: currently in hyperspace
Re: How to compile a C program using command prompt?
Yes install mingw or cygwin as neo told. Cygwin would be my choice because it's not minimalistic like mingw. But if you concern the space used and the download bandwidth then it's oky to chose mingw. Mingw means minimalistic.
And this is how I compile using the mingw or cygwin prompt.
1. Make an empty Makefile.
then to make a single executable from a file. for a example you got first.c like bellow.
So just to compile your first.c you just only need to command ,
Where Make have built in rules so Make is smart enough to figure out the it for you.
And to make the executable,
The make will build the executable out of first.c invoking the correct command line.
When your executable does build from more than one source module, then you may use $(OBJS). Bellow is an example Makefile.
Suppose that you need to build textedit.exe executable.
You could also use $CFLAGS to pass compile time flags, and $LDFLAGS to pass different link flags for a example,recently I was needed to link with openssl library to build specific executable in my project called 'md5_password_gen'.[In linux you could see there is no extension to the executable].And bellow is how it's line look like.
Where you could see that I've override the LDFLAGS variable for a specific rule.Where libssl would only be linked with the 'md5_password_gen' program, but not with the main program in the project.
Make is a tool and please feel free on using it. No matter you need to compile a single file for a test case you could use Make.And recursive make would invoke in the ./test directory Makefile recursively and link it with the test program.
It's like a powertool, where it make your life easy and quick. Use it but learn what it does behind the scene. IDE is also like that , there's no crime to use an IDE if you know what's going behind.
Good luck and thaks for your interest.
And this is how I compile using the mingw or cygwin prompt.
1. Make an empty Makefile.
Code: Select all
#touch Makefile
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char ** argv)
{
printf("hello World.\n");
return 0;
}
Code: Select all
#make first.o
And to make the executable,
Code: Select all
#make first.exe
When your executable does build from more than one source module, then you may use $(OBJS). Bellow is an example Makefile.
Code: Select all
.PHONEY: textedit.exe
LINK:=gcc
OBJS:=keyboard.o parser.o main_window.o mouse.o about_dialog.o
textedit.exe: $(OBJS)
$(LINK) -o $@ $^
You could also use $CFLAGS to pass compile time flags, and $LDFLAGS to pass different link flags for a example,recently I was needed to link with openssl library to build specific executable in my project called 'md5_password_gen'.[In linux you could see there is no extension to the executable].And bellow is how it's line look like.
Code: Select all
md5_password_gen: LDFLAGS+= -lssl
md5_password_gen: md5_password_gen.o
$(CPP) $(LDFLAGS) $^ -o $@
Make is a tool and please feel free on using it. No matter you need to compile a single file for a test case you could use Make.And recursive make would invoke in the ./test directory Makefile recursively and link it with the test program.
It's like a powertool, where it make your life easy and quick. Use it but learn what it does behind the scene. IDE is also like that , there's no crime to use an IDE if you know what's going behind.
Good luck and thaks for your interest.
Re: How to compile a C program using command prompt?
Thanks Neo, Thanks SemiconductorCat and you are welcome. I will try to follow what you recommended.