Writing console programs on c linux. Detailed course program


All computer programs are a set of processor instructions that consist of a specific set of bits. There are several hundred of these commands and with the help of them all actions on your computer are performed. But writing programs directly using these commands is difficult. That's why they were invented various languages programming that is easier for humans to understand.

To prepare the program for execution, special program collects it from source code in a programming language into machine code - processor commands. This process is called compilation. Linux is free software, and therefore the source codes of the programs are available to everyone. If the program is not in the repository or you want to do something non-standard, then you can compile the program.

In this article we will look at how compilation is done Linux programs, how the compilation process occurs, and also consider how flexibly you can configure everything.

We will compile programs written in C or C++, since this is the most used language for programs that require compilation. We have already covered this topic a little in the article installation from tar.gz in Linux, but that article is aimed more at beginners who need to not so much understand it as get a ready-made program.

In the same article, the topic is discussed in more detail. As you can imagine, special software is needed to turn source code into processor commands. We will use the GCC compiler. To install it and everyone necessary tools on Ubuntu do:

sudo apt install build-essential manpages-dev git automake autoconf

Then you can check the correct installation and compiler version:

But before moving on to the program compilation itself, let’s consider the components of this process in more detail.

How is compilation done?

Compiling a Linux program is a fairly complex process. It's even more complicated because the program code is not contained in one file or even in all of its source files. Each program uses many system libraries that contain standard features. In addition, the same code should work in various systems containing different versions libraries.

At the first stage, even before compilation begins, a special tool must check whether your system is compatible with the program, and whether all required libraries. If anything is missing, an error will be thrown and you will have to fix the problem.

Next comes parsing and converting the source code into object code, this step could be dispensed with, but it is necessary so that the compiler can perform various optimizations, make the size of the final program smaller, and the processor instructions more efficient.

Then all object files are collected into one program and linked to system libraries. After completing this stage, all that remains is to install the program into the file system and that’s it. These are the main phases of program compilation, and now let’s move closer to practice.

Compiling Linux Programs

The first thing we need is the source code of the program itself. In this example we will collect the most latest version vim. This is a completely neutral program, quite simple and useful to everyone, so it is perfect for an example.

Getting sources

The first thing we need is the source code of the program, which can be found on GitHub. You can find sources for most Linux programs on GitHub. In addition, there are assembly instructions there:

Let's download the sources of our program using the git utility:

git clone https://github.com/vim/vim

Also, you could download the archive on the site, and then unpack it into the desired folder, but it will be more convenient. The utility will create a folder with the name of the program, we need to make it working:

Setting up configure

Next we need to run a script that will check our program for compatibility with the system and configure the compilation parameters. It is called configure and is supplied by the program developers along with the source code. The entire compilation process is described in the Makefile, which will be created by this utility.

If configure is not in the source folder, you can try to run the following scripts to create it:

./bootstrap
$ ./autogen.sh

You can also use the automake utility to create this script:

aclocal
$autoheader
$ automake --gnu --add-missing --copy --foreign
$ autoconf -f -Wall

The automake utility and others from its set generate necessary files based on Mackefile.am file. This file is required in most projects.

Once you have configured, we can move on to configuration. One of the huge advantages of manually assembling programs is that you can choose which options to build the program with, where it will be located and what options additional features worth turning on. All this is configured using configure. Full set options can be viewed by running:

./configure --help

Let's look at the most commonly used options that are standard for all programs:

  • --prefix=PREFIX- the folder for installing the program, instead of /, for example, it can be /usr/local/, then all files will be distributed other than the main one file system, and in /usr/local;
  • --bindir=DIR- the folder for placing executable files must be in PREFIX;
  • --libdir=DIR- default folder for placing and searching libraries, also in PREFIX;
  • --includedir=DIR- folder for placing man pages;
  • --disable-feature- disable this feature;
  • --enable-feature- enable the feature;
  • --with-library- like enable, activates the specified library or header file;
  • --without-library- this disable disables the use of the library.

You can run configure without options to use the defaults, but you can also manually specify the right paths. In our case, ./configure is there and we can use it:

During setup, the utility will check if all the required libraries are on the system, and if not, you will have to install them or disable this feature if possible. For example, the following error may occur: no terminal library found checking for tgetent()... configure: error: NOT FOUND!

In this case, we need to install the required library. For example, the program offers ncurses, so we put:

sudo apt install libncurces-dev

The lib prefix is ​​always added before libraries, and -dev means that we need a library with all header files. Once all dependencies are satisfied, the setup will be successful.

Building the program

Once the setup is complete and the Makefile is ready, you can proceed directly to building the program. At this stage, the source code is directly converted into machine code. The Makefile-based make utility will do everything necessary actions:

After this, the program will be installed in the folder you specified and you can use it. But the more correct way is to create a package to install the program, this is done using the checkinstall utility, it allows you to create both deb and rpm packages, so it can be used not only in Ubuntu. Instead of make install, run:

Then simply install the resulting package using dpkg:

sudo dpkg install vim.deb

After this, the program assembly is completely completed and installed, so you can proceed to full use.

If you installed the program using make install, you can remove it by running the reverse command in the same folder:

sudo make uninstall

The command will delete all files that have been copied to the file system.

conclusions

In this article, we looked at how a Linux program is compiled. This process can be difficult for beginners, but in general, everything is possible if you spend a few hours solving the problem. If you have any questions, ask in the comments!

To conclude the video about what a compiler and interpreter are:

It became clear that the topic was very relevant. Some errors and questions were taken into account, and as a result, a decision was made to hold a second master class. Added and corrected!

Master class on C programming under Linux. We study the main API.

This master class is intended for people who want to learn the *nix API of similar operating systems, in particular for Linux. Here we will look at the features of development for Linux OS, which include:


  • Familiarization with the process of building software and the specifics of the C compiler from GCC

  • Development and use of shared libraries

  • Debugging programs

  • Exploring low-level file I/O mechanisms

  • Studying mechanisms for multitasking and interprocess communication

  • Using file and network sockets

  • Study and Application of the Signal Mechanism

  • Studying processes, threads, their differences, using multithreading, studying thread synchronization mechanisms and their problems

  • Creating Daemons, Learning the Differences Between Daemons and Application Software

  • Exploring the features of console I/O

  • Applying and using memory-mapped files

This master class is intended primarily for software developers, engineers, programmers and simply amateurs who are interested in the device and specifics of the Linux OS.

Requirements for master class participants: Knowledge of the C language, at book level B.V. Kernighan, D.M. Richie "LANGUAGE C".

The cost of this master class will be 6,000 rubles.

The venue is Moscow, in the premises of the Neuron Haxspace.
Dates: Approximately July 4 (Monday), to July 7 (Thursday) from 10 to 17 with a lunch break and tea/coffee breaks.

There are no plans for online broadcasts.
Number of people in the group: 8-10.

Registration is carried out by e-mail [email protected] or in the comments to this post. Your full name is required to register. (in full) and contact information (phone number and email). It is advisable to describe the purpose of attending this master class, level of training and occupation.

Detailed course program:

Module 1. Introduction


  • Familiarization with the specifics of software assembly in GNU/Linux

  • Introduction to console text editors (vi, nano, mcedit)

  • Working with the gdb debugger

  • Manual and automatic software assembly (Makefile)

  • Client-Interface-Server (CIS) model

  • Static build of libraries

  • Shared Libraries

  • Working with Environment Variables

Module 2: Low-Level I/O and File Operations

  • Overview of I/O mechanisms in Linux (Ubuntu)

  • File descriptors

  • System calls: open, close, write, read and lseek

  • File types

  • Inodes and Hard Links

  • File permissions

  • proc file system

  • Two ways to read the contents of a directory

  • Sparse files and the specifics of their use

  • Locking areas of a file

Module 3: Interprocess Communication

  • Linux Interprocess Communication Mechanisms (Ubuntu)

  • Unnamed pipes

  • Named pipes

  • Messages queue

  • Shared memory

  • Semaphores

Module 4: Sockets

  • Sockets in a file namespace (UNIX sockets)

  • Pair sockets

  • Network sockets

Module 5. Signals

  • Introduction to Signals

  • How signals differ from other interprocess communication mechanisms

  • Specifics of signal handling

  • Module 6: Processes

  • Cloning processes - fork()

  • Replacing the executing process - exec()

  • Zombies - causes and ways to eliminate them

Module 7: Streams

  • Threads and processes

  • Specifics of building multithreaded applications (multithreading)

  • Terminating a thread early

Module 8. Streams (continued)

  • Creating a Thread Termination Handler

  • Synchronize primitives

  • Stream Attributes

Module 9. Daemons (services)

  • The difference between a daemon and a console utility

  • Specifics of daemons development

  • Creating a daemon that uses network sockets

Module 10: Console I/O

  • Specifics of console application development

  • Preventing Output Redirection

  • Terminal management

  • Hiding a user's password during authentication

  • Controlling the terminal using ESC sequences

Module 11: Mapped Memory

  • Displaying a regular file

  • Share a file

  • Private mappings

  • Other uses of mmap

Module 12. Homework

  • Specifics of developing 64-bit applications

  • Using the ncurses library

Course leader: Sergey Dolin. Electronics engineer, Linux software developer (application software, test software for hardware, drivers). Software developer for embedded systems. Linux programmer since 2011. Worked at NICEVT OJSC, Concern Systemprom JSC, PROSOFT LLC (in the Dolamant subsidiary).

C is a fairly “ancient” programming language; it was formed in the early 70s. Despite this, C is a living language in the sense that it is actively used today. It was invented, used and is used to write significant parts of the program code of Unix-like operating systems. It is also used for writing utilities, compilers, and less commonly application programs. Therefore C is called system language programming.

Its survivability can be explained by the fact that the operating principles of operating systems are relatively universal; they are not subject to the progress and diversity that can be observed in the environment of desktop and mobile software, and Web applications. C is not a language enough high level, it is closer to computer architecture. As a result, C programs are compact and fast.

C does not support object-oriented programming. OOP support is implemented in C++. Although the latter arose from the C language, it is not a “continuation” of it, but is a separate language that can be learned without knowing C. However, learning C is useful before getting to know its “advanced little brother”, because The syntax of the languages ​​is similar, C does not overload the brain of a novice programmer with superpowers and teaches him to understand the essence of what is happening.

Is C good for getting into programming for the first time? If you are not studying at a university in a specialty related to IT, then no. C assumes an understanding of organization and operating principles hardware, in particular – memory. A lot of this is done with pointers, they play a key role; this topic is quite difficult to understand and is not usually studied in school.

Of course, you can learn the basics of programming with C without having to learn pointers. However, a person will think he knows C without knowing much about it in substance. The C language was created by hardware-savvy professional programmers to write the UNIX operating system. It was not intended as a language for teaching beginners.

Environments and compilers for C programming

If you use one of the GNU/Linux distributions, any text editor with syntax highlighting, you will also need GCC and a terminal.

There are editors for programmers that include various add-ons, including a terminal, directory browsing, etc. For example, Geany or Atom.

C is a compiled programming language. In GNU/Linux, to obtain executable files, GCC is used - a set of compilers, including a compiler for C. To obtain an executable file from a source file (usually such files have the *.c extension), you need to run a command in the terminal that looks something like this:

gcc -o hello hello.c

Where gcc is a command that launches a program that performs compilation and other actions; -o – a switch indicating that we manually specify the name of the executable file; hello – name of the resulting executable file; hello.c – file name with source code. The name of the executable file can be omitted:

gcc hello.c

In this case executable file will have a default name of a.out.

Windows has its own set of compilers - MinGW. It can be used on its own, but it is included in the simple Dev-C++ development environment, which may be good choice for learning programming in C and C++ languages.

When saving, select the file type "C source files (*.c)". The program is compiled and launched by pressing the F9 key. After execution, the program closes immediately and the result cannot be seen. To prevent this from happening, write two additional lines: #include and getch(). (This may not be relevant for more new version Dev-C++.)

"Hello World" in GNU/Linux:

#include \n") ; }

"Hello World" on Windows:

#include #include int main() (printf("Hello World \n") ; getch(); )

On the other hand, there is a large number of cross-platform development environments. For example, Eclipse + CDT module, KDevelop, CLion. The latter is paid, produced by JetBrains - a leader in IDE development, however, it has a 30-day trial period, which may be enough for training. CLion is more convenient than other IDEs.

"Hello World" in C

Using the example of a simple program, we will immediately note some features of the C programming language.

In the C language, the main() function takes on the role of the main branch of the program. This function must always be present in a complete C program, and program execution begins with it. However, the variables declared inside it are not global; their scope extends only to main(). However, in the C programming language almost all program code is in a function, and the main() function is the main and required one.

By default, the main() function returns type data int, so you don't have to specify the return data type. However, the compiler issues a warning in this case.

The printf() function is designed to print data. Its purpose is similar to the write() procedure Pascal language and print() functions in Python. The printf() function does not jump to after output. new line. Therefore, for the transition we use special character, which is denoted by the combination \n. Complete C expressions are separated by semicolons.

In C, I/O functions are not part of the language. For example, in Python we do not need to import any module to use print functions() and input(). In C, we cannot simply call the printf() function, because C itself simply doesn't have it. This function, as well as a number of others, can be enabled using the stdio.h header file. This is precisely why the line is written at the beginning of the program #include . Include is translated from English as “turn on”, and stdio is an abbreviation for “standard input-output”.

Header files (they end in *.h) usually contain declarations of certain functions. A declaration is simply a description of a function: what parameters it takes and what it returns. The function code itself (definition) is not located in the header file, but in libraries (other files), which may already be compiled and located in system directories. Before compiling a program, the C language preprocessor is launched. Among other things, it includes at the beginning of the program file the contents of the header files specified in it.

Practical work

Comment out the first line of HelloWorld code. Try compiling the program. Were you able to obtain the executable file? What warning did the compiler give?

1 // - one-line comment in C language; /* … */ - multiline comment in C language.

All actions in operating system are performed using programs, so many beginners are interested not only in using other people’s programs, but in writing their own. Many people want to contribute to the OpenSource codebase.

This is a review article about programming for Linux. We will look at which languages ​​are used most often, look at the basic concepts, as well as capabilities, and figure out how to write the simplest program in one of the most popular programming languages, how to manually build and run it.

Historically, the Unix kernel was written in C. Moreover, this language was created for writing the Unix kernel. Because the Linux kernel was based on the Minix kernel (a version of Unix), it was also written in C. Therefore, we can say that the main programming language for Linux is C and C++. This trend continued for a long time.

In general, you can write programs for Linux in almost any language, from Java and Python to C# and even Pascal. All languages ​​have compilers and interpreters. Writing programs in C++ is difficult, and C is considered obsolete by many, so many programmers use other languages ​​to write programs. For example, many system tools written in Python or Perl. Most programs from the team Linux Mint, the Ubuntu installer and some apt scripts are written in Python. Many scripts, including simple scripts optimizations are written in Perl. Ruby is sometimes used for scripting. These are OpenShift scripts or, for example, the Metasploit framework. Some cross-platform program developers use Java. But the main components of the system are still written in C.

We won't cover the basics of C in this article. C is a difficult language and you will need to read at least one book and practice a lot to master it. We will look at how to write C programs on Linux, how to compile and run them.

Why learn C:

2. Libraries

Naturally, if you need to display a string or an image on the screen, you will not directly access the video card. You simply call a few functions that are already implemented in the system and pass them the data you want to display. Such functions are placed in libraries. In fact, libraries are collections of functions that are used by other programs. They contain the same code as in other programs, the only difference is that there is not necessarily the presence of an initialization function.

Libraries are divided into two types:

  • Static- they are associated with the program at the compilation stage, they are associated and after that all library functions are available in the program as native ones. Such libraries have the extension .a;
  • Dynamic- such libraries are much more common; they are loaded into RAM and linked to the software dynamically. When a program needs a library, it simply calls it at a known address in random access memory. This saves memory. The extension of these libraries is .so, which comes from Shared Object.

Thus, for any C program you need to include libraries, and all programs use some kind of library. It is also important to note that no matter what language you decide to write in, in the end everything will be reduced to the C system libraries. For example, you are writing a program in Python, using standard features of this language, and the interpreter itself is already a C/C++ program that uses system libraries to access the main capabilities. Therefore, it is important to understand how C programs work. Of course, there are languages, like Go, that are immediately translated into assembly, but the same principles are used there as here. In addition, systemic linux programming, basically, this is C or C++.

3. Program build process

Before we move on to practice and create our first program, we need to understand how the assembly process occurs and what stages it consists of.

Every serious program consists of many files, these are source files with the extension .c and header files with the extension .h. Such header files contain functions that are imported into the program from libraries or other files.c. Before. How can the compiler build a program and prepare it for work, it needs to check whether all functions are actually implemented, whether all static libraries are available and assemble it into one file. Therefore, the first step is to run a preprocessor that assembles the source file, executing statements such as include to include the code of the header files.

At the next stage, the compiler starts working, it performs all the necessary actions on the code, parses the syntactic structures of the language, variables and converts all this into intermediate code, and then into the code of machine instructions, which we can then look at in assembly language. The program at this stage is called an object module and is not yet ready for execution.

Next, the linker gets to work. Its task is to connect the object module with static libraries and other object modules. A separate object module is created for each source file. Only now the program can be launched.

Now, let's look at this whole process in practice using the GCC compiler.

4. How to build the program

There are two types of compilers used to compile programs in Linux: . So far, GCC is more common, so we will consider it. Usually, the program is already installed on your system, but if not, you can install it on Ubuntu:

sudo apt install gcc

Before we move on to writing and assembling the program, let's look at the syntax and compiler options:

$gcc options source_file_1.c -o finished_file

Using options, we tell the utility what to do, what libraries to use, then we simply specify the source files of the program. Let's look at the options we'll be using today:

  • -o- write the result to a file for output;
  • -c- create an object file;
  • -x- specify the file type;
  • -l- load a static library.

Actually, this is all the most basic thing we need. Now let's create our first program. It will display a line of text on the screen and, to make it more interesting, calculate the square root of the number 9. Here is the source code:

include
#include

int main())(
printf("website\n");
printf("Root: %f\n", sqrt(9));
return 0;
}

gcc -c program.c -o program.o

This is the compilation stage, if there are no errors in the program, then it will be successful. If source files several, then the same command is executed for each of them. Next we perform the linking:

gcc -lm program.o -o program

Pay attention to the -l option, with it we indicate which libraries need to be included, for example, here we include the library of mathematical functions, otherwise the linker simply will not find where this or that function is. Only after this you can run the program for execution:

Of course, all these actions can be performed using various graphical environments, but by doing everything manually, you can better understand how everything works. Using the ldd command you can see which libraries our program uses:

These are two loader libraries, the standard libc and libm, which we included.

5. Assembly automation

When we consider programming under Linux, it is impossible not to note the system for automating program assembly. The point is that when there are many source files for a program, you will not manually enter commands to compile them. You can record them once and then use them everywhere. There is a make utility and Makefiles for this purpose. This file consists of targets and has the following syntax:

target: dependencies
team

The target dependency can be a file or another target, the main target is all, and the command performs the necessary build actions. For example, for our program the Makefile might look like this:

program: program.o
gcc -lm program.o -o program

program.o: program.c
gcc -c program.c -o program.o

Then you just need to run the make command to start compilation, just remember to delete the previous temporary files and the compiled program:

The program is ready again and you can run it.

conclusions

Creating Linux programs is very interesting and exciting. You will see for yourself when you get a little used to this matter. It's difficult to cover everything in such a short article, but we've covered the very basics and they should give you the basics. In this article we looked at the basics of programming in Linux, if you have any questions, ask in the comments!

C programming course for Linux:

Last update: 08/28/2017

In the previous topic, we looked at creating the first program on Windows. Now let's look at creating the first program on Linux, in particular in the Ubuntu 16.04 environment. On Linux, g++ is also a popular compiler for creating programs. Therefore in in this case We will also use this compiler.

As a rule, many Linux distributions, including Ubuntu, already contain the g++ compiler installed by default, which we can immediately use. But even if suddenly it is not installed, you can install it in the terminal using the command:

Sudo apt-get install g++

Otherwise, if we use the g++ compiler for compilation, everything will be similar to creating a program on Windows.

Let's define a directory in the file system for source files with C++ code and create in it new file hello.c with the following code:

#include // include the header file iostream int main() // define the function main ( // start of the function std::cout<< "Hello World!"; // выводим строку на консоль return 0; // выходим из функции } // конец функции

And this is the same code that was in the case of Windows, because C++ programs are more portable at the source code level.

To output a line to the console, you need to connect the necessary functionality. To do this, at the beginning of the file there is a line

#include

This line represents a preprocessor directive that allows you to include the iostream library. This library is needed to output a line to the console.

The main function consists of four elements:

    Return type. In this case, the type is int . This type specifies that the function should return an integer.

    Function name. In this case, the function is called main.

    List of parameters. After the function name there is a list of parameters in parentheses. But in this case, the brackets are empty, that is, the main function does not take parameters.

    Function body. After the list of parameters in curly braces comes the body of the function. Here the actual actions that the main function performs are defined.

    ( std::cout<< "Hello World!\n"; return 0; }

In the body of the function, a line is output to the console. The standard output stream std::cout is used to access the console. Using an operator<< в этот поток (в данном случае фактически на консоль) передается строка символов, которую надо вывести на консоль, то есть "Hello World!". В конец строки добавлена специальная управляющая последовательность "\n", которая позволит после вывода текста перевести курсор на новую строку.

In a function, we exit the function using the return statement. Since the function must return an integer, the number 0 is indicated after return. Zero is used as an indicator of successful completion of the program.

Every statement in C++ is followed by a semicolon.

Let's go to the terminal and first use the cd command to navigate to the directory where the source code file is located.

g++ hello.cpp -o hello

In addition to the name of the source code file, the compiler is passed the -o hello parameter. It indicates that we want to compile a file named hello. If this parameter is not passed, a file will be created with the default name - a.out.

As a result, after executing the above command, a compiled file will appear in the folder with the hello.cpp file, which will be called hello. And we can run it with the following command:

And the line “Hello World!” will be displayed on the console.







2024 gtavrl.ru.