Writing and Compiling C Program in Linux Part 3: Using Makefile

March 19, 2011

Compiling C Program using Make

  • Using the example in Part 2, instead of compiling line by line, we can create a make file that will follow certain compiling instructions.
  • The format for the make file is:

target: dependencies

command

  • The file is usually called Makefile
  • The Makefile of previous tvm program will be as follows:
#Creating object
tvm2: tvm2.o finlib.o
gcc tvm2.o finlib.o -lm -o tvm2
tvm2.o: tvm2.c fin.h
gcc -c tvm2.c
finlib.o: finfn.c fin.h
gcc -c finfn.c -o finlib.o
clean:
rm -f tvm2 tvm2.o finlib.o
  • This is the more explicit way to instruct the make program how to compile line by line.
  • Please note that the final program must come first before compilation of object. It is a drill down approach.
  • After the make file is written, you can package all the program and Makefile together.
  • To compile the program just type #make
  • To remove the compiled program and object type #make clean
  • However, we need not be so explicit because make program know how to compile programs.
  • See the example below:
CFLAGS=-Wall
LDFLAGS=-lm
#Creating object
tvm: tvm.o finfn.o fin.h
clean:
rm -f tvm tvm.o finfn.o
  • CFLAGS refers to compile flags
  • LDFLAGS refers to linking flags
  • By indicating our target program and the dependencies, the make program will compile the program and link them appropriately using the flags indicated.
  • Since the custom library need not compile every time, we can include the main C program and the object file of the library.
  • The Makefile will be slightly different, as follows:
CFLAGS=-Wall
LDFLAGS=-lm
#Creating object
tvm: tvm.o finlib.o fin.h
clean:
rm -f tvm tvm.o
  • We don’t supply finfn.c but we supply a precompiled object file finlib.o.

Writing and Compiling C Program in Linux Part 2: Using Custom Library

March 18, 2011
  • Instead of building custom function in a C program, we can include any custom function into a custom library. In this way, we could reuse the custom function between different program.
  • First, we maintain the same main(), but we remove all custom financial functions. Instead we included a statement included “fin.h” so that the compiler will search for the header file.
#include <stdio.h>
#include "fin.h"
int main (void)
{
double myrate;
int myterm;
double myprincipal;
double myreturn;
printf ("Please enter the following:\n");
printf ("Rate of return:(In decimal 50%% is 0.5)");
scanf ("%lf", &myrate);
printf ("\n");
printf ("Number of terms:(No decimals): ");
scanf ("%d", &myterm);
printf ("\n");
printf ("The principal amount: ");
scanf ("%lf", &myprincipal);
printf ("\n");
myreturn = tvm (myrate, myterm, myprincipal);
printf ("Your return after %d term is $%.2lf.\n", myterm, myreturn);
return 0;
}
  • Next, we create a header file (“fin.h”) that will include all financial functions in the future.
double tvm (double rate, int terms, double principal);
  • Finally, we create a new program call finfn.c (as in financial function). The custom function will be place here.
#include <math.h>
#include "fin.h"
double tvm (double rate, int terms, double principal)
{
double dn;
double pp;
dn = terms;
pp = pow (1+rate, dn);
return principal * pp;
}
  • The purpose splitting the program is such that tvm function can be reuse as a custom library in another program.
  • We can compile all the program #gcc -Wall tvm.c finfn.c -o tvm –lm
  • However, we don’t have to compile finfn.c every time of there is no changes.
  • One way is to create an object file during compilation instead of create executable.
  • We compile finfn.c first: #gcc -Wall -c finfn.c -o finlib.o
  • With the object file available, we can create another program tvm2 using the same tvm function:
#include <stdio.h>
#include "fin.h"
int main (void)
{
double rate = 0.025;
int term = 10;
double principal = 1000;
double result = 0;
result = tvm (rate, term, principal);
printf ("The return of $1000 investment with a rate of 2.5 percent in 10 years will give you $%.2lf.\n", result);
return 0;
}
  • Then we compile tvm2.c first before linking:
#gcc -Wall -c tvm2.c -o tvm2.o
  • We can link both program as follows:
#gcc tvm.o finlib.o -lm -o tvm
#gcc tvm2.o finlib.o -lm -o tvm2
  • In the example, we compile 2 programs using the custom function in one object file.
  • Alternatively, we can compile straight from tvm2.c to executable like below:
#gcc -Wall tvm2.c finlib.o -lm -o tvm2

Writing and Compiling C Program in Linux GCC Part 1

March 17, 2011
  • GCC compiler is usually installed together with your Linux. If not, use the command $sudo apt-get install gcc (for Ubuntu user) and #yum install gcc (for Fedora, Redhat user). For Fedora user, you need to be in su mode before installation.
  • Below is a simple C program called sick.c
#include <stdio.h>
int main(void)
{
printf ("Sick of printing hello!\n");
return 0;
}
  • To compile the program #gcc -Wall sick.c -o verysick
  • The option -Wall display all possible Warning and –o indicate to the compiler that user want the output to be name after the -o option.
  • To run the program indicate the complete path or $./verysick

Writing and Compiling C Program using Math Library

  • Below is a more complex C program called tvm.c
#include <stdio.h>
#include <math.h>
double tvm (double rate, int terms, double principal);
int main (void)
{
double myrate;
int myterm;
double myprincipal;
double myreturn;
printf ("Please enter the following:\n");
printf ("Rate of return:(In decimal 50%% is 0.5) ");
scanf ("%lf", &myrate);
printf ("\n");
printf ("Number of terms:(No decimals): ");
scanf ("%d", &myterm);
printf ("\n");
printf ("The principal amount: ");
scanf ("%lf", &myprincipal);
printf ("\n");
myreturn = tvm (myrate, myterm, myprincipal);
printf ("Your return after %d term is $%.2lf.\n", myterm, myreturn);
return 0;
}
double tvm (double rate, int terms, double principal)
{
double dn;
double pp;
dn = terms;
pp = pow (1+rate, dn);
return principal * pp;
}
  • To compile the program #gcc -Wall tvm.c -lm -o tvm
  • The additional option -lm is required so that the compiler will search for the math library in reference to including math.h.

Writing and Compiling C Program using Microsoft Visual C++ 2010 Express Edition

January 19, 2011

Update

For users wanting to compile C program in Microsoft Visual Studio 2012 Express, please refer to the link here http://www.zealfortechnology.com/2013/06/compile-c-program-using-visual-studio-2012.html For users programming in any other platform such as Max OS X or Linux, please refer to my main page here. On the main page, you can also access to my articles about creating static or dynamic library in Windows and other platform.

******************************************************************************************************************

There are two methods to write and compile a C program in Visual C++ 2010. You can compile a C program using command line or you can compile a C program using the Integrated Development Environment (IDE) from Microsoft Visual C++.

Using Command Line to Compile C Program

1. Select  [Start >> All Programs >> Microsoft Visual C++ 2010 Express Edition >> Visual Studio Tools >> Visual Studio 2010 Command Prompt]. Run with administrator privileges.

2. Once you are in the command line, you can use any editor such as edit to write a C program.

C:> edit myprogram.c

3. After written the program, to compile it, issue the command cl

C:> cl myprogram.c

4. During the compilation and linking, myprogram.obj and myprogram.exe will be created.To run the program:

C:> myprogram

Using Visual C++ 2010 IDE

1. Create an empty project by select [File >> New >> Project]. Select [General >> Empty Project] and type in the project name. Alternatively, you can select [File >> New >> Project], follow by [Win32 >> Win32 Console Application]. Type in the project name.

2. If you use Win32 Console Application, you can skip this step. Under [Solution Explorer], right click the project name, and select [Properties]. Select [Configuration Properties >> Linker >> System]. Under [SubSystem], select [Console (/SUBSYSTEM:CONSOLE)].

3. Under [Solution Explorer, Source File], right-click and select [Add >> New Item].

4. Under Code, Select C++ File (.cpp), however, when you type in the name use a program name with extension .c, such as myprog.c and click [Add].

5. You can proceed to type your C program. Click [Save] icon to save your file.

6. To compile the program, under [Solution Explorer, Source File], right click the source file and select [Compile or (Ctrl+F7)].

7. Please note that you cannot run the program until you have build solution.

8. To build solution select [Debug >> Build Solution or (F7)]

9. To run the program without debugging, select [Debug >> Start without debugging or (Ctrl+F5)]. (Note: If you do not have this option, you need to add the command manually under Tools >> Customize. You may add a menu option or a button in the Debug toolbar.)

10. For any runtime error, you can debug the program as follows; Select [Debug >> Start debugging (F5)].

11. Please note that during debugging, the program will show and close the console display immediately.

12. To see the console display, you must run without debugging.

Note:

The problem using Visual Studio is that many files will be created even for a very simple program. All files are created inside the project folder.

Under the project name of your program, the source code and object file is located at sub folder similar to your project name, the executable file is under sub folder <Debug>.

It is simpler to use the command line to compile and run a C program, however, you could use the IDE for writing a properly formatted C program.


Writing and Compiling C Program in Windows

January 18, 2011

Using Command Line using Visual C++ 2008

  1. Select  Start >> All Programs >> Microsoft Visual C++ 2008 Express Edition >> Visual Studio Tools >> Visual Studio 2008 Command Prompt (Run with administrator privileges)
  2. Once you are in the command line, you can use any editor such as edit to write a C program.
C:> edit myprogram.c
  1. After written the program, to compile it, issue the command cl
C:> cl myprogram.c
  1. During the compilation and linking, myprogram.obj and myprogram.exe will be created.
  2. To run the program:
C:> myprogram

Using Visual C++ 2008 IDE

  1. Create an empty project by select File >> New >> Project. Select General >> Empty Project and type in the project name.
  2. Under folder Source, right-click Select Add >> New Item.
  3. Under Code, Select C++ File (.cpp), however, when you type in the name use a program name with extension .c, such as myprog.c and click Add
  4. You can program to type your C program. Click Save icon to save your file.
  5. To compile the program select Build >> Compile (Ctrl+F7).
  6. Please note that you cannot run the program until you have build solution.
  7. To build solution select Build >> Build Solution (F7)
  8. Running the program:
  9. Select Debug >> Start without debugging (Ctrl+F5)
  10. For any runtime error, you can debug the program as follows:
  11. Select Debug >> Start debugging (F5)
  12. Please note that during debugging, the program will show and close the console display immediately.
  13. To see the console display, you must run without debugging.

Note:

  • The problem using Visual Studio is that many files will be created even for a simple program. All files are created inside the project folder.
  • Under the project name of your program, the source code and object file is located at sub folder similar to your project name, the executable file is under sub folder debug.

Using Borland C++ Compiler Command Line Tools

  • Installing and running the Command Line Tools
  • Run freecommandlinetools.exe; choose the drive and folder into which you want to install the free C++Builder 5 command line tool development system.
  • From the bin directory of your installation: Add “c:\Borland\Bcc55\bin” to the existing path
  • Create a bcc32.cfg file which will set the compiler options for the Include and Lib paths (-I and –L switches to compiler) by adding these lines:
-I"c:\Borland\Bcc55\include"
-L"c:\Borland\Bcc55\lib"
  • Create an ilink32.cfg file which will set the linker option for the Lib path by adding this line:
-L"c:\Borland\Bcc55\lib"
  • Compiling the program:
bcc32 myprogram.c

C# Programming – The Language

March 6, 2009

Below is my notes on C# 2.0. It focus mainly on the language itself. Information of .Net and Visual Studio 2005 is documented in a separate document.

View this document on Scribd

C++ Addition

March 6, 2009

I did not compiled a separate reference guide for C++. Instead I compiled this additional notes focusing on the additions of C++ made to C. Of course, the main topics in this booklet is Object Oriented Programming

View this document on Scribd