[Introduction to Embedded Basics] The basic process of compiling C programs under Ubuntu system

This article mainly describes the basic process of compiling C programs under Ubuntu18.04 system, including environment configuration, most basic network parameter settings, apt source replacement, classic hello world compilation method under Ubuntu, use of vim text editor , makefile use etc.

         1. Environment configuration

2. Network parameter setting

Configure the network through NetworkManager

1. Set the network adapter of the virtual machine to bridge mode or NAT mode

2. Virtual network editor settings

3. Check the network configuration

 3. Replace the apt source with Ali source

4. Write a simple C program that outputs hello world

1. Install vim, gcc

2. Brief introduction and use of vim

3. Create a new hello.c

4. Compile hello.c

 5. Perform an operation on two input integer parameters

1. Requirements

2. Write c language code under the virtual machine

3. Write c language code on windows 

Sixth, use makefile to compile the above program 

1. Introduction to makefile

2. How does make work

3. makefile

4. Target

5. Install make

6. Write makefile with vim


1. Environment configuration

Virtual Machine Software VMware Workstation Pro

Ubuntu 18.04

It should be noted here that after the virtual machine software VMware Workstation Pro is installed, two virtual network cards will appear at our network connection

 If not, it may be because you have uninstalled this software before, some files have not been uninstalled cleanly, or some parts of the registry have problems. For detailed solutions, please refer to this link. After VMware is installed, there is no virtual network card - psy_code - Blog garden

2. Network parameter setting

There are two ways to configure Ubuntu's network, one is to configure through interfaces (more complicated), and the other is to configure through NetworkManager. Here we mainly explain the NetworkManager configuration method.

Configure the network through NetworkManager

NetworkManager is a graphical network management tool in Ubuntu.
Network Manager command line tool, also called nmcl

1. Set the network adapter of the virtual machine to bridge mode or NAT mode

 Select the virtual machine --> Settings --> Network Adapter --> Bridge Mode (or --> NAT Mode)

 Note that when selecting the bridge mode, check the copy of the physical network connection status at the same time

2. Virtual network editor settings

Select Edit --> Virtual Network Editor

 Click to change settings

 Here choose to add a network, add VMnet0, set it to bridge mode , and set it to automatic (you can also directly select NAT mode )

3. Check the network configuration

Enter in the command window ifconfig -ato view your network card number

 Enter in the command line  ping 自己的IP地址 , you can see if you can ping, if the network is smooth, it will always return the data bar, stop the output and use ;ping www.baidu.comcrtl+c

 3. Replace the apt source with Ali source

Enter the command  sudo vim /etc/apt/sources.list  to edit the source file list

Change the original content to the following.

 Run the following command sudo apt-get update 更新软件列表

4. Write a simple C program that outputs hello world

1. Install vim, gcc

Enter the command to install the vim editor and install the gcc compiler
sudo apt-get install vim

sudo apt-get install gcc

2. Brief introduction and use of vim

Vim is a text editor developed from vi. Code completion, compilation, error jumping and other convenient programming functions are particularly rich, and are widely used among programmers, and are tied with Emacs to become the favorite text editor for users of Unix-like systems.

Vim has three modes: command mode, insert mode, and last line mode.

Common commands:

       switches to input mode to enter characters

:         Switch to the bottom line command mode, enter the command on the last line

x         deletes the character where the cursor is currently located

Esc    exits input mode and switches to command mode

      saves the current write content

      quit vim editing

      Exit vim editing after saving with wq

wq!    Exit vim's editor after force saving

3. Create a new hello.c

Enter the command vim hello.c

After entering the editor, press i to enter the edit mode, and write the following code

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

After editing, press the esc key, wq! exit vim editor

4. Compile hello.c

Enter the command and a hello file will be generated at this time. Enter to complete the classic compilation of hello world
gcc hello.c -o hello

./hello

 5. Perform an operation on two input integer parameters

1. Requirements

The subroutine sub1.c contains an arithmetic operation function float x2x (int a, int b), the function of this function is to perform an operation on two input integer parameters, and return the result as a floating point number; the main program main1.c defines And assign two integer variables, then call the function x2x, and printf the return result of x2x.

2. Write c language code under the virtual machine

Enter the command vim main1.c to edit the following code

#include<stdio.h>
extern float x2x(int x,int y);
int main()
{
    int x,y;
    x=3;
    y=6;
    float ans;
    ans=x2x(x,y);
    printf("%f",ans);
    return 0;
}

The compilation is complete, wq! Exit editing after force saving

Enter the command vim sub1.c to edit the following code

float x2x(int x,int y)
{
    float ans;
    ans=x+y;
    return ans;
}

The compilation is complete, wq! Exit editing after force saving

Then enter the command to run
gcc -o main main1.c sub1.c
./main1

3. Write c language code on windows 

Here I just use DEV C++ to compile

Write the main.c file

#include"sub1.h"
int main()
{
	int x,y;
	x=3;
	y=6;
	float ans;
	ans=x2x(x,y);
	printf("%f",ans);
	return 0;
}

Write  sub1.h file

#ifndef __SUB1_H
#define __SUB1_H

#include<stdio.h>

float x2x(int x,int y);

#endif

Write sub1.c file

#include"sub1.h"
float x2x(int x,int y)
{
	float ans;
	ans=x+y;
	return ans;
}

Then compile the main.c file

Sixth, use makefile to compile the above program 

1. Introduction to makefile

Use the make command in the Shell script to compile, especially in C/C++ development, the make command automatically compiles the dependencies between the source programs described in the makefile; the makefile is written in accordance with the specified format, and it needs to explain how to compile each Source files are connected to generate executable files, and the dependencies between source files are required to be defined; the compilation of many large-scale projects is organized through Makefile.

2. How does make work

1.make searches for the "Makefile" or "makefile" file in the current directory
2. If it finds it, it searches for the first object file .o in the file
3. If the object file does not exist, it searches for the .s file according to the dependencies
4. If If the .s file does not exist, search for the .i file according to the dependency relationship
5. If the .i file does not exist, search for the .c file according to the dependency relationship. At this time, the .c file must exist, so generate a .o file and execute it

3. makefile

The Makefile file consists of a series of rules, each of which has the following form:

<target>: <prerequisites>
[Tab]<commands>

The first line is the goal before the colon, and the precondition after the colon; the second line must start with a Tab key, followed by the command; the goal is mandatory and cannot be omitted; the precondition and command are optional, but the two or must exist at least one.

4. Target

The target can be a file name, indicating the object to be built by the make command; it can also be an operation name, called "pseudo-target";

clean:
		rm *.o

The target of the above code is clean, and the command is rm *.o;

Execute the make clean command to delete the object file;

5. Install make

Enter the command sudo apt-get install make

6. Write makefile with vim

Enter the command vim makefile to write the following code

prog: main1.o sub1.o
        cc main1.o sub1.o -o prog
main1.o: main1.c
            cc -c main1.c
sub1.o: sub1.c
            cc -c sub1.c
clean:
        rm prog *.o

wq! After saving and exiting, enter make first on the command line  , and then enter ./prog

Guess you like

Origin blog.csdn.net/qq_55894922/article/details/126722203