Introduction to c language helloworld under ubuntu system and writing simple programs

Keywords: C Ubuntu vim Network Communications

1, Introduction to c language helloworld

1. Open the terminal under ubuntu system
2. Find the appropriate location and create a new folder helloworld.

mkdir helloworld

3. Open the helloworld folder

cd helloworld

As shown in the figure

4. Create and open a c language file named hello.c with vim

vi hello.c


5. Enter the following in the file
Click a to enter the editing mode and enter at the cursor

#include <stdio.h>
int main()
{
printf("hello world!");
return 0;
}


6. Compile hello.c with gcc command

gcc  hello.c -o hello

Use of basic working gcc commands

commandeffect
gcc -E hello.c -o hello. iThe program of hello.c is precompiled. i is used as the extension to generate a text file
gcc -S hello.i -o hello.s to hello. iCompile and generate an assembly language source program file. s as the extension. After compilation, it is a text file
gcc -c hello.s -o hello.oAssemble hello.s to generate a relocatable object file. o is used as the extension, and after assembly is a binary file
gcc hello.o -o helloLink multiple relocatable object files with standard library functions and the relocatable object module printf.o where printf is located to generate executable object files
gcc hello.c -o helloGenerating executable object files directly from c language programs
./helloExecute file hello
gcc -o0 -m32 -g hello. c -o hello-o0 indicates the optimization level, 0 indicates no optimization- m32 means that it is compiled into x86-32-bit instructions. Without, it is compiled into x86-64 bits- g means with debugging information. This option must be added for one-step debugging

7. Run the hello file

. /hello

2, Write simple programs under ubuntu and windows systems respectively

(1) Simple main / subroutine in ubuntu system

1. Write a main,c main program

vi main.c

2. Write the code segment in main.c and save and exit

#include<stdio.h>
#include"sub.c"
int main(){
        int a=2,b=3;
        float i;
        i=f1(a,b);
        printf("%f\n",i); 
        return 0;
}
    

3. Create a new subroutine sub.c

vi sub.c

Enter the following code in sub.c

#include<stdio.h>
float f1(int a,int b){
float i;
i=a+b;
return i;
}

4. Compile main.c and turn it into an executable file

gcc main.c -o main

5. Run the main file

. /main

(2) Write simple main / subroutine under windows system

1. Use the programming software QT
2. New project untitled

3. Create a new source file main.cpp

#include<stdio.h>
#include"sub.h"
int main(){
    int a=2,b=3;
    printf("a+b=%f\n",f1(a, b));
    return 0;
}

4. Create a new source file sub.cpp

#include"sub.h"
float f1(int a, int b){
    float i;
    i=a+b;
    return i;
}

5. Create a new header file sub.h

float f1(int a,int b);


6. Build and run

(3) Programming in Makefile mode under ubuntu system

1. Install make

sudo apt-get install make


Since I have installed it, write it directly

2. Write a main program file main.c and a subroutine file sub.c with vi, which is the same as in (II) above
main.c

#include<stdio.h>
#include"sub1.c"
int main(){
        int a=2,b=3;
        float i;
        i=f1(a,b);
        printf("%f\n",i); 
        return 0;
}

sub.c

#include<stdio.h>
float f1(int a,int b){
float i;
i=a+b;
return i;
}

3. Use gcc command to generate main.o and sub.o files from main.c and sub.c files

gcc main.c -p main.o
gcc -c sub.c -o sub.o

4. Create and compile the makefile file with getdit

touch makefile
vi makefile

makefile

 main: main.o sub.o
        gcc -o main main.o sub.o
main.o:main.c sub.h
        gcc -c main.c
sub.o:sub.c sub.h
        gcc -c sub.c
clean:
        rm *.o main

~                        

1.main is the name of the executable file finally formed, and the ". o" file after it is main, which is composed of those files.
2. Because many ". o" files will be formed in the middle, you can add this to the makefile file to delete the ". o" file.
clean:
rm *.o file name
Spaces must press Tab

5. Run makefile with make command and output the result

Posted by KashKurcura on Fri, 19 Nov 2021 20:13:48 -0800