Foreword
Take a command of sorting and averaging in C language as an example to realize the functions of the three parameters s of the input sort
-average command. The average command puts all source codes and makefiles under /usr/local/src/sort-average 1. Write source code main.c, sort.c, average.c 2. Write makefile 3. Compile and install test sort -average command 4. Uninstall all related files of sort-average command 1. Write source code main.c, sort.c, average.c, write three source files as follows mkdir /usr/local/src/sort-average cd /usr /local/src/sort-average 1.vi main.c My main.c source code looks like this: #include<stdio.h> int main(void) { void inputcommand(void); char ch='\0' ; inputcommand(); for(;;)
{
scanf("%c",&ch);
if(ch=='s')
{
sort();
inputcommand();
}
else if(ch=='a')
{
average();
inputcommand();
}
else if(ch=='q')
{
printf("exit...\n");
return 0;
}
}
}
void inputcommand(void)
{
printf("Please input 's' or 'a or 'q':");
}
2.vi sort.c
我的sort.c源码大概是这样:
#include<stdio.h>
void sort(void)
{
int num[10];
int i;
int j;
int temp;
printf("Please input 10 integers to sort:\n");
for(i=0;i<10;i++)
{
scanf("%d",&num[i]);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("after sort:\n");
for(i=0;i<10;i++)
{
printf("%d ",num[i]);
}
printf("\n");
}
3.vi average.c
我的average.c源码大概是这样:
#include<stdio.h>
void average(void)
{
int num[5];
int average=0;
int i;
printf("Please input 5 integers to average:\n");
for(i=0;i<5;i++)
{
scanf("%d",&num[i]);
}
printf("average:\n");
for(i=0;i<5;i++)
{
average = average + num[i];
}
printf("%d\n",average/5);
}
二.编写makefile文件
vi makefile
makefile文件里面的代码大概是这样:
LIBS = -lm
OBJS = main.o sort.o average.o
main: ${OBJS}
gcc -o sort-average ${OBJS}
clean:
rm -f sort-average ${OBJS}
install:
install -m 755 sort-average $(RPM INSTALL ROOT) /usr/local/bin/sort-average
三.Compile, install and test sort-average command
1. Compile the sort-average command make according to the makefile
2. Install the sort-average command
make install
3. Test the sort-average command
sort-average
Please input 's' or 'a or 'q':a
Please input 5 integers to average :
1 2 3 4 5
average:
3
Please input 's' or 'a or 'q':s
Please input 10 integers to sort:
5 6 4 3 2 1 54 6 3 4
after sort:
1 2 3 3 4 4 5 6 6 54
Please input 's' or 'a or 'q':q
exit...
4. Uninstall the files related to the sort-average command
because the installation only writes a binary file sort-average to /usr/local/bin inside, so we just need to delete this file
rm -f /usr/local/bin/sort-average
centos7 writes a C language tarball package
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326677618&siteId=291194637
Recommended
Ranking