Linux Embedded Development - C Programming

Linux Embedded Development - C Programming

1. Write a C program

We are currently using the VIM editor to write programs. Of course, we can also use vscode, but we will introduce vscode later.

1.1, set vim editor

First of all, we need to set a TAB=4 bytes of the vim editor to make it similar to our programming habits. The specific operation is as follows:

First enter the following command:

sudo vi /etc/vim/vimrc

Then enter set ts=4 in the last line, that's it. Then set the vim compiler to display the line number for our observation, just add set nu after the file just now. The end result looks like this:

insert image description here

1.2, write C program

Our main.c file enters the following content:

#include<stdio.h>

int main(void){
    
    
    printf("hello world!\r\n");
    return 0;
}

2. Compile the C program

For compilation, we use the gcc compiler to compile the C program. The specific operation is as follows:

  • -c: only compile but not link to executable file, the compiler compiles the input .c file into .o object file.
  • -o: <output file name> is used to specify the output file name after compiling. If this option is not used, the name of the executable file compiled by GCC is a.out by default.
  • -g: Add debugging information. If you want to use a debugging tool (such as GDB), you must add this option. This option indicates that the symbol information required for debugging will be generated during compilation.
  • -O: Compile the program with optimization. If this option is used, the entire source code will be optimized when compiling and linking, so that the execution efficiency of the generated executable file will be high.
  • -O2: Greater optimization than -O, the generated executable is more efficient, but the entire compilation process will be very slow.

When we directly enter gcc xxx, an a.out executable file will be generated by default, as follows:

gcc main.c

insert image description here

It is not a solution to call every file a.out. For the sake of beauty, we specify a name we want, but we need to use the -o command. Let's take a look at the specific usage method:

gcc main.c -o main

insert image description here

What deserves our attention is his error report, we need to understand it!

When we were on the fourth line, we didn't put our "\r\n" in the quotation marks, let's see what effect it will have.

#include<stdio.h>

int main(void){
    
    
    printf("hello world!"\r\n);
    return 0;
}

It is madly reminding us of the fourth line, and the specific location is given, similar to dev-c++, we just pay attention.

main.c: In function ‘main’:
main.c:4:30: error: stray ‘\in program
    4 |         printf("hello world!"\r\n);
      |                              ^
main.c:4:30: error: expected ‘)’ before ‘r’
    4 |         printf("hello world!"\r\n);
      |               ~              ^~
      |                              )
main.c:4:32: error: stray ‘\in program
    4 |         printf("hello world!"\r\n);
      |                                ^

3. Make tool and Makefile

In the process of programming, there are often many .C files and .H files. If it is still like the above, it will be cumbersome to compile them one by one with gcc. And when one of the files is modified, the entire file needs to be recompiled, which is a waste of time when the project contains many files. To sum up, an automated compilation tool is needed at this time, and they are our make and Make file, let's take a look next!

make: Generally speaking, GNU Make is a command tool that interprets the instructions in the makefile. It is used to compile source code files into executable binary files. The make tool is mainly used to complete automatic compilation. When the make tool is compiled, the Makefile file is required to provide the compiled file.

Makefile: The file used by the make tool, and the Makefile specifies the compilation rules.

3.1, write C program

Next, let's write a small file, which contains 3 .c files and 2 .h files. Then take a look at how make and Makefile work, the specific code is as follows:

C file

// main.c  
  #include <stdio.h>
  #include "input.h"
  #include "calcu.h"
   
  int main(void){
    
    
      int a, b, num;
      input_int(&a, &b);
      num = calcu(a, b);
      printf("%d + %d = %d\r\n",a, b, num);
      return 0;
  }
// input.c
  #include <stdio.h>
  #include "input.h"
  
  void input_int(int *a, int *b){
    
    
      printf("input two num:");
      scanf("%d %d", a, b);
      printf("\r\n");
  }
// calcu.c 
  #include "calcu.h"
   
  int calcu(int a, int b){
    
    
      return (a + b);
  }

H-file

// calcu.h   
  #ifndef _CALCU_H
  #define _CALCU_H
  
  int calcu(int a, int b);
  #endif
// input.c
  #ifndef _INPUT_H
  #define _INPUT_H
  
  void input_int(int *a, int *b);
  #endif

Next, let's take a look at the specific use of the make tool.

3.2, do not use the make tool

If we don't use the make tool, we can only compile the files together. The commands used are as follows:

gcc main.c calcu.c input.c -o main

But in this case, whenever we modify the code and compile it again, all our source code will be recompiled. As mentioned earlier, this takes a lot of time. Is there a good way to improve it? The answer is yes, but not many. That is to generate all .o files and then connect them together. Every time a file needs to be modified, recompile this file and connect it again, but this is actually very troublesome, so we can only use our make tool up.

gcc -c main.c
gcc -c input.c
gcc -c calcu.c
gcc main.o input.o calcu.o -o main

# 当有文件需要修改
gcc -c input.c
gcc main.o input.o calcu.o -o main

3.3, use the make tool and Makefile to compile

To use our make tool, we first need to create a file named "Makefile" in the project directory, and then enter the following statement in the Makefile folder. (The specific syntax will be introduced later)

# 要得到main目标,需要main.o input.o calcu.o,需要执行
# gcc -o main main.o input.o calcu.o命令,后面的都是这样的。
main: main.o input.o calcu.o
	gcc -o main main.o input.o calcu.o
	
main.o: main.c
	gcc -c main.c
	
input.o: input.c
	gcc -c input.c
	
calcu.o: calcu.c
	gcc -c calcu.c

# clear需要执行的命令
clean:
	rm *.o
	rm main

Then enter the make command.

make

The running results are as follows:

gcc -c main.c
gcc -c input.c
gcc -c calcu.c
gcc -o main main.o input.o calcu.o

When we modify one of the files, let's see what happens when we execute it again.

gcc -c input.c
gcc -o main main.o input.o calcu.o

We can see that only the modified files are recompiled, which is more automatic. Then enter the clear command, and you will find that the files we need to clear are also cleared.

mclear

operation result:

rm *.o
rm main

`

When we modify one of the files, let's see what happens when we execute it again.

gcc -c input.c
gcc -o main main.o input.o calcu.o

We can see that only the modified files are recompiled, which is more automatic. Then enter the clear command, and you will find that the files we need to clear are also cleared.

mclear

operation result:

rm *.o
rm main

Guess you like

Origin blog.csdn.net/weixin_66578482/article/details/128888624