FAQ – How to Compile C/C++ in Windows and Linux

1.How to Compile for C and C++
1.1 For Linux Compile
+We use gcc program to compile for C source
+We use g++ program to compile for C++ source
For example:
gcc hello.c //this produce a.out and object file
g++ hello.cpp //this produce a.out and object file
gcc -o hello hello.c //this produce hello and object file
g++ -o hello.out hello.cpp //this produce hello.out and object file
gcc -o hello hello.c myheader.h bye.c //this produce hello and object file
Note: You must list all of the header files and sources file dependency to complete a compile

1.2 For Window
+We use the cl program in Microsoft Visual C++ to compile for C and C++ source
+We can download Microsoft Visual C++ Express on Microsoft site. It is FREE

+Start the cl Command Line
+Use cd command to go to the source C folder
cl hello.c //this will produce hello.exe and object file
cl hello.c myheader.h bye.c //list all the header and relevant source file to compile

2.Makefile for C and C++ in Linux
+Makefile is the file contain gcc/g++ commands to compile the source files in an order
+The Makefile name is “Makefile”. NO EXTENSION.
+We must place the Makefile in the same folder with the source file
+To start compile > type “make” to compile

+Sample Makefile for C/C++ EXECUTABLE program:
//——————————
all: myprog.c
   gcc -g -Wall -o myprog myprog.c
clean:
   $(RM) myprog
//——————————

+Sample Makefile for C/C++ Shell Script Linux Kernel:
//——————————
obj-m+=assignment.o

all:
   make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
   make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
//——————————

Add Comment