0) Create a simple project with three files: file1.c file2.c and main.c. Define func1() in file1.c and func2 in file2.c. Declare the func1() in file1.h and func2() in file2.h. Include file1.h and file2.h in main.c and call the two functions. Write a makefile for this project. Extra: Make changes in file1.h and file2.h in such a manner that including twice inside main.c will not give compilation errors.
1) Download wget source code, compile and install it on the system
2) Understand what configure, automake and autoconf are for
3) Download and compile the linux kernel
4) Understand phony targets inside a makefile
5) Browse mozilla firefox source code using mxr.mozilla.org. - Access search functions - Browse for variables declarations and function declarations - Browse for variable and function usages
6) Download, read and understand the source of a small and simple free software project as much as possible for some time (such as wget).
7) Read the GNU coding standards document and try to analyze the importance of each guideline there.
Puzzle: What does the following function do:
int what_do_i_do(int a)
{ a = ((a & 0xAAAAAAAA) >> 1) + (a & 0x55555555); a = ((a & 0xCCCCCCCC) >> 2) + (a & 0x33333333); a = ((a & 0xF0F0F0F0) >> 4) + (a & 0x0F0F0F0F); a = ((a & 0xFF00FF00) >> 8) + (a & 0x00FF00FF); a = ((a & 0xFFFF0000) >> 16) + (a & 0x0000FFFF); return a; }
The more you invest in quality, the less time it takes to develop working software.
Quality is not just testing
Trying to improve the quality of software by doing more testing is like trying to lose weight by weighing yourself more often. (Steve McConnell)
Quality is:
This lecture looks at basic things every developer can do to maintain quality.
o Then you move on to the second function…
A unit test exercises one component in isolation
An integration test exercises the whole system
Regression testing is the practice of rerunning tests to check that the code still works
Any test can have one of three outcomes:
o Don't know anything about the system being tested
A specification is something that tells you how to classify a test's result
How to write tests so that:
A test consists of a fixture, an action, and an expected result
o A fixture is something that a test is run on o Can be as simple as a single value, or as complex as a networked database
Every test should be independent
o I.e., the outcome of one test shouldn't depend on what happened in another test o Otherwise, faults in early tests can distort the results of later ones
So each test:
o Creates a fresh instance of the fixture o Performs the operation o Checks and records the result
Find the exercises at http://software-carpentry.org/3_0/qa.html
Read the following code and test!
void main() { char str[40]; int i,flag=0,len; clrscr(); printf("\n Enter A string : "); gets(str); len=strlen(str); for(i=0;i<(len/2);i++) { if(str[i]!=str[len-1-i]) { flag=1; break; } } if(flag==1) { printf(" \n The string is not palindrome"); } if(flag==0) printf("\n String is palindrome"); getch(); }
void main() { char main_str[40],str[40]; int i,j,len1,len2; clrscr(); printf("\n Enter the main String :"); gets(main_str); printf("\n enter the String you wanna search : "); gets(str); len1=strlen(main_str); len2=strlen(str); i=0; j=0; while(str[i]!='\0') { while(main_str[j]!='\0') { if(str[i]==main_str[j]) { i++; if(len2==i) { j=j-len2+2; printf("\n found at %d location",j); break; } }//if else j++; }//inner while if(j==len1) { printf("\n not Found"); break; }//if }//outer while getch(); }
void main() { int a,b; clrscr(); printf("\n enter First Number :"); scanf("%d",&a); printf("\n enter Second Number :"); scanf("%d",&b); swap(&a,&b); printf("\n first Number is : %d",a); printf("\n second Number is : %d",b); getch(); } swap(int *a,int *b) { *a=*a-*b; *b=*a+*b; *a=*b-*a; }
main() { int temps[31]; int index,total; float average,celsius; total=0.0; for(index=0;index<31;index++) { printf("enter temperature #%d:",index); scanf("%d",&temps[index]); } for(index=0;index<31;index++) total+=temps[index]; average=total/31.0 printf("average is:%f\n\n", average); puts9"fahrenheit\tcelsius\n"); for(index=0;index<31;index++) { celsius=(5.0/9.0)*(temps[index]-32); printf("%d\t\t%6.2f\n",temps[index],celsius); } }
#include "stdio.h" main() { FILE*fp; int letter; if((fp=fopen("MYFILE","r"))==NULL) { puts("Cannot oepn the file"); exit(); } while((letter=fgetc(fp)) !=eof) printf("%c",letter); fclose(fp); }
The make utility
If you run
make
this program will look for a file named makefile in your directory, and then execute it. If you have several makefiles, then you can execute them with the command:
make -f MyMakefile
There are several other switches to the make utility. For more info, man make.
1. Compiler takes the source files and outputs object files 2. Linker takes the object files and creates an executable
The trivial way to compile the files and obtain an executable, is by running the command:
g++ main.cpp hello.cpp factorial.cpp -o hello
The basic makefile is composed of:
target: dependencies [tab] system command
This syntax applied to our example would look like:
all: g++ main.cpp hello.cpp factorial.cpp -o hello
You can also use variables when writing Makefiles. It comes in handy in situations where you want to change the compiler, or the compiler options.
# I am a comment, and I want to say that the variable CC will be # the compiler to use. CC=g++ # Hey!, I am comment number 2. I want to say that CFLAGS will be the # options I'll pass to the compiler. CFLAGS=-c -Wall all: hello hello: main.o factorial.o hello.o $(CC) main.o factorial.o hello.o -o hello main.o: main.cpp $(CC) $(CFLAGS) main.cpp factorial.o: factorial.cpp $(CC) $(CFLAGS) factorial.cpp hello.o: hello.cpp $(CC) $(CFLAGS) hello.cpp clean: rm -rf *o hello