Purpose
- Learn to use gcc, make, gdb tools
- familiar with c language
content
- Write a C language program: output two lines of text "C under Linux is not too difficult!". Edit, compile, and run the program under Linux.
Step 1: Start the vim editor and write code in it;
#include<stdio.h>
int main(){
printf("Linux下的c也不是太难嘛!\n");
return 0;
}
Step 2: Compile the program gcc text.c -o text;
Step 3: Run the program ./text to get the result.
2. Write a c language program: average two integers input and output at the terminal, and generate its assembler file through the gcc compiler.
Step 1: Start the vim editor and write code in it;
#include<stdio.h>
int main(){
float avg;
int a,b;
scanf("%d %d",&a,&b);
avg=(a+b)/2.0;
printf("%f",avg);
return 0;
}
Step 2: Compile the program gcc -o avg avg.c;
Step 3: Run the program ./avg to get the result.
3. Use the gdb debugger to debug the program in question 2 above, check the value of the variables in each step of the program execution, and be familiar with the use process of gdb.
4. Build a simple program and practice the creation method of function library. The function library contains two functions and is called in a sample program (prom.c), these two functions are fast.c (quick sort), insert.c (insertion sort), which implements ascending sorting of one-dimensional arrays . Follow the steps below to generate the function library.
1) Write prom.c, fast.c, insert.c respectively;
Start the vim editor to write the fast.c program;
#include<stdio.h>
int fast(int a[],int n){
int t;
for(int i=1;i<10;i++)
for(int j=i+1;j<11;j++)
if(a[i]>a[j])
{ t=a[i];
a[i]=a[j];
a[j]=t;
}
return 0;
}
Start the vim editor and write the insert.c program code;
#include<stdio.h>
int insert(int a[],int n){
int i,j;
for(i=2;i<=n;i++){
a[0]=a[i];
j=i-1;
while(a[0]<a[j]){
a[j+1]=a[j];
j--;
}
a[j+1]=a[0];
}
return 0;
}
Start the vim editor and write the prom.c program;
int main()
{
int a[11],i;
for (i=1;i<=10;i++)
scanf("%d",&a[i]);
fast(a,10);
insert(a,10);
printf("插入数据排序后顺序:\n");
for(i=1;i<11;i++)
printf("%2d",a[i]);
printf("\n");
return 0;
}
2) Compile these two programs to generate target files fast.o and insert.o
gcc -c fast.c insert.
According to the prompt, the code must be compiled under std=c99.
3) Create the library file libfoo.a
ar crv libsort.a fast.o insert.o
4) Compile prom.c without library
gcc -c prom.c
gcc prom.o fast.o insert.o -o prom1
5) Compile prom.c with the library file libsort.a
gcc -o prom2 prom.o libsort.a
6) Use the library file lib sort .a to compile prom.c, use the l option to access the function library, and use the L option to indicate the directory where the library file is located
gcc -o prom3 prom.o -L. -lsort
7) Try to write a Makefile rule file to manage the project file, and use make to compile it as prom4;