WriteLinuxThe following tools are required for applications:
(1) compiler: gcc
GCC isLinuxThe most important development tool in the platform. It is the gnu c and C ++ compilers, and its basicUsageGCC [Options] [filenames].
We should use Linux-GCC.
(2) Debugger: GDB
GDB is a powerful debugger used to debug C and C ++ programs. We can use it to perform a series of debugging tasks, including setting breakpoints, viewing variables, and performing a single step.
We should use Linux-GDB.
(3) make
The main task of GNU make is to read a text file called makefile. This file records which files are generated and what commands are used to generate them. Make checks files on the disk based on the information in this makefile. If the creation or modification time of the target file is earlier than that of one of its dependent files, make executes the corresponding command, to update the target file.
The compiling rules in makefile must use the Linux-version accordingly.
(4) code editing
You can use the traditional VI editor, but it is best to use Emacs, which has syntax highlighting, version control, and other ancillary functions.
After developing an application using the above tools on the host machine, you can download the program to the target board to run it through the following channels:
(1) download the program to the file system of the Target Board through the serial communication protocol RZ (thanksLinuxProvides a command like RZ );
(2) download the program from the FTP directory on the host through the FTP communication protocol to the file system on the target board;
(3) swap the program into the USB flash drive, mount the USB flash drive on the target machine, and run the program in the USB flash drive;
(4) If the target machineLinuxUsing the NFS file system, you can directly hack the program into the corresponding directory of the host machine, in the target machineLinux.
1. File Programming
LinuxThe file operation API involves creating, opening, reading, writing, and closing files.
Create
IntCreat(Const char * filename, mode_t mode );
The mode parameter specifies the access permission for the new file. It determines the final permission of the file (Mode & umask) together with umask. umask indicates some access permissions that need to be removed during file creation. Umask can be changed by calling umask:
Int umask (INT newmask );
This call sets umask as newmask and returns the old umask, which only affects read, write, and execution permissions.
Open
Int open (const char * pathname, int flags );
Int open (const char * pathname, int flags, mode_t mode );
Function Description:
The pathname parameter points to the file path string to be opened. The following are flags that can be used by the flags parameter:
O_rdonly open a file in read-only mode
O_wronly open the file in write-only mode
O_rdwr can open files in read/write mode.
The above three flags are mutually exclusive, that is, they cannot be used at the same time, but can be combined with the following flags using the OR (|) operator.
O_creat: if the file to be opened does not exist, the file is automatically created.
O_excl if o_creat is also set, this command checks whether the file exists. If the file does not exist, the file is created,
Otherwise, the file will be opened incorrectly. In addition, if both o_creat and o_excl are set and the file to be opened is a symbolic connection, opening the file will fail.
If the file to be opened is a terminal device, o_noctty does not regard the terminal as a process control terminal.
If the o_trunc file exists and is opened in writable mode, this flag will clear the file length to 0, and the information originally stored in the file will also disappear.
O_append when reading and writing a file, it will start to move from the end of the file, that is, the written data will be appended to the end of the file.
O_nonblock open the file in an unblocking way, that is, whether there is data read or waiting, it will immediately return to the process.
O_ndelay is the same as o_nonblock.
O_sync opens the file in synchronous mode.
O_nofollow if the file indicated by pathname is a symbolic connection, opening the file will fail.
O_directory if the file indicated by the pathname parameter is not a directory
The file may fail to be opened. This isLinuxThe flag unique after 2.2 to avoid some series
Security issues. The mode parameter has the following combinations:
In addition, the permissions during file creation are affected by the umask value.
The file permission should be (Mode-umaks ).
S_irwxu00700 permission indicates that the object owner has the readable, writable, and executable permissions.
S_irusr, s_iread, and 00400 permissions indicate that the file owner has the readable permission.
S_iwusr or s_iwrite, 00200 permission indicates that the file owner has the write permission.
S_ixusr or s_iexec, 00100 permission indicates that the file owner has executable permission.
S_irwxg 00070 permission indicates that the file user group has the readable, writable, and executable permissions.
S_irgrp 00040 permission indicates that the file user group has the readable permission.
S_iwgrp 00020 permission indicates that the file user group has the write permission.
S_ixgrp 00010 permission indicates that the file user group has executable permissions.
S_irwxo 00007 indicates that other users have the readable, writable, and executable permissions.
S_iroth 00004 permission, which indicates that other users have the readable permission
S_iwoth 00002 permission indicates that other users have the write permission.
S_ixoth 00001 permission indicates that other users have executable permissions.
Return Value:
If all the permissions to be verified have passed the check, 0 is returned, indicating that the operation is successful. If one permission is disabled,-1 is returned.
Error code:
The file specified by the eexist parameter pathname already exists, but the o_creat and o_excl flag are used
The file indicated by the eaccess parameter pathname does not meet the required permissions.
The file to be tested by erofs is stored in the read-only file system.
The pathname pointer of the efault parameter exceeds the accessible memory space.
The Mode Val parameter mode is incorrect.
The pathname parameter of enametoolong is too long.
The pathname parameter of enotdir is not a directory.
The enomem core memory is insufficient.
The pathname parameter of eloop has too many symbolic connections.
Eio I/O access error.
Read/write
After the file is opened, we can read and write the file,LinuxThe read and write functions are called by the system:
Int read (int fd, const void * Buf, size_t length );
Int write (int fd, const void * Buf, size_t length );
The parameter Buf is the pointer to the buffer, and the length is the buffer size (in bytes ). The read () function reads length bytes from the file specified by the file descriptor FD to the buffer zone pointed to by the BUF. The returned value is the actual number of bytes read. Function write writes the Length byte from the buffer to which the Buf points to the file pointed by the file descriptor FD. the return value is the actual number of bytes written.
Open marked with o_creat actually implements the file creation function. Therefore, the following function is equivalentCreat() Function:
Int open (pathname, o_creat | o_wronly | o_trunc, mode );
Positioning
For random files, we can read and write at random locations and use the following functions to locate them:
Int lseek (int fd, offset_t offset, int whence );
Lseek () moves the file read/write pointer to the offset byte relative to whence. When the operation is successful, the position of the returned file pointer relative to the file header. The whence parameter can use the following values:
Seek_set: Start of relative file
Seek_cur: Current Position of the relative file read/write pointer
Seek_end: relative to the end of the file
The offset value can be a negative value. For example, the following call can move the file pointer 5 bytes forward from the current position:
Lseek (FD,-5, seek_cur );
Because the return value of the lseek function is the position of the file pointer relative to the file header, the following returned values are the file length:
Lseek (FD, 0, seek_end );
Close
You only need to call close. FD is the file descriptor to be closed:
Int close (int fd );
Next we will compile an application, create a user-readable and writable file "example.txt" in the current directory, write "Hello World" in it, close the file, and open it again, read the content and output it on the screen:
# Include
# Include
# Include
# Include
# Define length 100
Main ()
{
Int FD, Len;
Char STR [length];
FD = open ("hello.txt", o_creat | o_rdwr, s_irusr | s_iwusr );
If (FD)
{
Write (FD, "Hello, software Weekly", strlen ("Hello, software Weekly "));
Close (FD );
}
FD = open ("hello.txt", o_rdwr );
Len = read (FD, STR, length );
STR [Len] = '\ 0 ';
Printf ("% s \ n", STR );
Close (FD );
}