How to compile a C program using command prompt?

C, C++, Visual C++, C++.Net Topics
Post Reply
CAT77
Sergeant
Sergeant
Posts: 14
Joined: Fri Nov 15, 2013 8:18 pm

How to compile a C program using command prompt?

Post by CAT77 » Mon Feb 17, 2014 6:29 pm

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. ;)
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to compile a C program using command prompt?

Post by Neo » Tue Feb 18, 2014 10:51 pm

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.
User avatar
SemiconductorCat
Major
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?

Post by SemiconductorCat » Thu Feb 20, 2014 12:45 am

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.

Code: Select all

#touch Makefile
then to make a single executable from a file. for a example you got first.c like bellow.

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char ** argv)
{
  printf("hello World.\n");
  return 0;
}

So just to compile your first.c you just only need to command ,

Code: Select all

#make first.o
Where Make have built in rules so Make is smart enough to figure out the it for you.
And to make the executable,

Code: Select all

#make first.exe
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.

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 $@ $^
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.

Code: Select all

md5_password_gen: LDFLAGS+= -lssl

md5_password_gen: md5_password_gen.o
	$(CPP) $(LDFLAGS) $^ -o $@

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.
CAT77
Sergeant
Sergeant
Posts: 14
Joined: Fri Nov 15, 2013 8:18 pm

Re: How to compile a C program using command prompt?

Post by CAT77 » Thu Feb 20, 2014 3:39 pm

Thanks Neo, :) Thanks SemiconductorCat and you are welcome. :) I will try to follow what you recommended. 8-)
Post Reply

Return to “C/C++ Programming”