GNU Compiler Collection, first steps

This article is intended to introduce a beginner nix developer to GNU tools with simple examples, in particular the GCC compiler.


With it, we will create the simplest program. By and large, everything is as usual. We create a special folder in which the project will be placed.
We create a file in it with the name: hello.c
Open the file in any text editor and write the simplest code: Save the file and execute the command: A new file appeared in the folder we created - a.out, this name is assigned by default, unless otherwise specified . This is the executable file. Let's try to run it, for this we type in the console: And rejoice in connection with the first written program in Linux!

#include
int main(void)
{
printf("Hello world!");
return(0);
}


gcc hello.c



./a.out



We go further. When you run the executable file, if we specify only its name, the system will look for it in the directories / usr / bin and / usr / local / bin, and, of course, will not find it. The first of them is designed to host stable versions of programs, usually included in the Linux distribution. The second is for programs installed by the user (for whose stability no one can vouch). By default, when building the program, they are installed in the / usr / local / bin directory.

The flags used during compilation

The -o flag is used to specify a specific name for the executable file to be received: -Egcc hello.c -o say_hello

flaguse to see what happens after the preprocessor. This flag stops the execution of the program, just at the stage of processing by the preprocessor. The result is a source code file with the contents of the header files included in it.
We lift / look: We use the -cgcc -E hello.c -o hello.cpp

flag to create object files (analogous to * .obj): The name of the resulting file is the same, but the compiler changes the .c extension to .o (but you can also specify it manually). -X flaggcc -c kalkul.c



we use it if an object file is created from the source already processed by the preprocessor (for example, the one we got above), we must definitely indicate that the compiled file is a source code file processed by the preprocessor and having preprocessor tags. Otherwise, it will be processed like a regular C ++ file, without taking into account preprocessor tags, which means that communication with the declared functions will not be established.

The C ++ file processed by the preprocessor is indicated by cpp-output: The
gcc -x cpp-output -c hello.cpp

project is assembled as follows: gcc hello.o -o say_hello
Run: ./say_hello

Why is all this fuss with intermediate steps necessary?
Programs rarely consist of a single file. As a rule, there are several source files, and they are combined into a project. And in some exceptional cases, the program has to be composed of several parts, written possibly in different languages. In this case, you have to run compilers of different languages, so that everyone gets the object file from their source, and then compose these received object files into an executable program.

Also popular now: