Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get started with linux Program Development

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how to get started with linux program development, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

There are always people who say that application development on Linux can only be done by experts, and this "superstition" seems to be very common at present. However, this is not the case. In terms of library support, the Linux platform provides many powerful and rich libraries for the development of user-level applications, and most of them are cross-platform (Boost, OpenGL, STL, Qt, Java, etc.) and based on POSIX standards (glibc, etc.). At the same time, the Linux kernel also provides a fully functional kernel interface for driver development. Linux provides powerful compiler GCC and debugger GDB. With their help, we can easily develop portable applications on Linux. Such being the case, where does "superstition" come from? I think, on the one hand, because there are few books introducing all kinds of Linux development in detail, all kinds of Linux applications are still not popular in China, on the other hand, it is because many people can not find a handy IDE environment after installing an IDE, so they feel at a loss. After all, many of us are used to writing programs, press F5, and let IDE act as the sole agent for the rest of the tasks. In fact, it is no problem to want to be like this under Linux. Now that we are talking about IDE, let's start with it. I believe that choosing a good IDE environment is a good start for your whole learning process.

If you want to do good work, you must first sharpen its tools-- IDE

In fact, there are many powerful IDE environments under Linux, because in a sense, Linux is an operating system for developers. Of course, this thing is indispensable. Here we introduce some of the more commonly used IDE.

KDevelop

This is an IDE developed with Qt, and its main supported language is C / C++.

Eclipse

In recent years, eclipse has developed rapidly. It is not only a development platform based on java, but also its powerful plug-in architecture makes it can be used as a variety of applications. As the carrier of various plug-ins, eclipse provides a complete GUI interface, and users can only care about what they want to do with the help of eclipse.

Emacs

VIM

The story of IDE backstage is GCC.

Earlier we briefly introduced some of the IDE environments, in which all the programs related to GCC are compiled by GCC, while IDE is only used to collect compilation information and generate makefile for our project (which we'll talk about later). Due to the characteristics of Linux development at present, C # is still the mainstream language of system development. Therefore, it is necessary to have a comprehensive understanding of GCC, once IDE can not meet your needs, we should have the ability to create procedures by hand, and for the purpose of learning, we often do not need those complex files generated by IDE, for a Hello world to generate more than 2m files is obviously superfluous.

The full name of GCC is GNU Compiler Collection. From this name, we can easily see that GCC represents a collection of compilers. At present, GCC can support C, Cellular, Objective-C, Objective-C++, Fortran, Java, and Ada and other languages. But for the sake of generality, we will only discuss the part of GCC, which is called "Cmax" +.

At present, the * * release of GCC is 4.0.0, but this version is not recommended because it uses new technologies and new coding standards, and a lot of old code needs to be modified before it can be compiled. The relatively stable new version is currently 3.4.4, you can go to the GNU home page to update and download. So what exactly is the power of GCC and how to use it? Let me show you the powerful features provided by GCC through a few simple but practical examples.

Be familiar with the basic usage of GCC through Helloworld compilation

It seems that providing a Hello World sample program for all new languages has become an unwritten standard through which people understand some of the basic elements of the language. Here, we use a Hello World to see how to generate executables with GCC.

Save the above file as helloworld.c, then open the console and enter the following command gcc helloworld.c-o helloworld. If everything is all right, there should be no output on your console. Check your working directory with ls, and you will find that there is an executable file named helloworld in the directory, and then execute. / hellworld

You will see the output of this program.

It's simple, isn't it? But anyone who has studied computers should know that the compilation process of the program is divided into the processes shown in the following figure, and the power of GCC is that it allows you to stop and look at the intermediate results in any of the processes shown above and control them.

1. Preprocessing begins with the preprocessing process. The-E option of GCC allows GCC to stop compiling after preprocessing and print the preprocessed file to standard output. The following-o is used to specify the file name of the output file.

Gcc-E hellowrold.c-o helloworld.cpp is part of the helloworld.cpp below, and we can see that the file already contains the contents of the stdio.h.

If we want to perform the next step in the compilation process, we can continue to use the-x option of GCC, which displays the suffix name of the specified file (rather than letting the compiler judge from the line based on the suffix). There are several kinds of language type that we commonly use (if you want to get a more complete parameter name, please refer to GCC manual):

L c c-header c-cpp-output

L C++ c++-header c++-cpp-output

L assembler assembler-with-cpp

In addition, the following table lists the commonly used GCC suffix names

Of course, you can also omit the language type, and GCC will judge based on the suffix name of the file, as if you didn't use this option.

Let's continue with our compilation process.

two。 Compile if we want to get the compiled source file, we can use the-S option, which lets gcc only perform compilation (generate assembly file) and not assembly (generate target file). In this case, we can use the-o option to specify the name of the output assembly file.

Gcc-S helloworld.cpp-o hellowrld.S

3. In addition, we can use the-c option of GCC to compile and assemble the source file without linking, where the output file specified by-o is the compiled target file name gcc-x C++-c helloworld.cpp-o helloworld.o

4. Link * *, we can use GCC to link the .o file we just generated into an executable program gcc helloworld.o-o helloworld. This time, we used the-o option to specify the executable file name, that is,-o has different meanings depending on the type of input file.

5. Function library links and include files for any and a program we write, it is unthinkable without the support of library functions, and when the header files and function libraries we want to use are not under the default search path of GCC (such as OpenGL, Qt, KDE, Boost, etc.), we need to tell GCC their location manually.

Let's first take a look at the assignment of the header file path. We can use-I to specify the header file directory we want GCC to search, for example, if we want to use X11, we will use the following option to look at the library function settings: we use the-L and-l command line options to complete the task. The-L is used to tell GCC to look for the library in, and the-l option tells GCC to use the user-specified library. In Linux, the naming of libraries follows the UNIX convention, that is, lib {lib name}, such as libsocket.so, so you can use the-lsocket option when you need to tell GCC to use these libraries. Usually, these two commands are used together, for example, when referencing the X11 library, we can do this:

-L/usr/X11R6/lib-lX11

In addition, GCC uses shared libraries to link programs by default, and when you want to link static libraries, be sure to use the-static option, such as-lncurses-static

In the * * section of this section, we make a brief summary of the common GCC commands used at compile time.

Above, we mentioned the common commands about GCC compilation, and here are some helpful commands that will give you an understanding of the basic configuration and use of GCC.

In this section, let's talk about the setting of link parameters when building software. As we mentioned in part 5 above, the use of function libraries requires a combination of-L and-l, but in fact, a decent program often requires the support of many libraries. For example, if you need to write a GTK program, we need the following link parameters:-L/usr/lib-L/usr/X11R6/lib-lgtk-lgdk-rdynamic-lgmodule-ldl-lXi-lXext-lX11-lm. It looks a little scary, you might ask, how do I know I need this, if I want to write KDE programs, and OpenGL? In fact, the situation is much better than you think. In the / usr/bin directory, there are many scripts called xxx-config, which are used to show users the parameters used when compiling the linked program. These scripts can accept some parameters, such as-libs to list the libraries used to link to specific programs, and-cflags to generate the include directory of the header file, that is, the-I parameter we mentioned above. So, for GTK programs, we can compile them with the following command: gcc gtksource.c `gtk-config-libs-- crous` of course, it's obviously not a good idea to write a config for each program, and currently new development packages use the script pkg-config to generate link parameters. You can use pkg-config- list-all to view all the link parameters supported by pkg-config. When you find out that you want a package in the above list, you can use the following command to compile the program `pkg-config--libs-- cuplos`.

Let GCC help you do a better job

Above we briefly introduced the common command line options of GCC, in fact, the features of GCC are much richer than those mentioned above. GCC provides rich support for code warning, optimization, debugging, and so on. Let's take a look at these functions provided by GCC from some examples.

1. Warn the problem code

GCC provides a complete check function for the program code, due to the characteristics of the Cmax Cure + language, many errors are inadvertently made by programmers, such as the use of undefined variables, the use of = instead of = = in bool expressions, and so on. Using the code review function provided by GCC, we can let the compiler find these problems for us to avoid runtime disaster.

First of all, let's look at a "problem code"

/ * test_warning.c We use this file to check the warning facilities provided by GCC*/ # include#include

Void main () {/ * main should return int*/

Int a, b; long long l = 2.2; / * long long type is GNU extension, not standard ANSI / ISO type*/

Miss_decl (); / * We call an undeclared function*/

If (a = 0) / * May be we want = = here instead of = * /

Printf ("a really equals to 0?\ n"); if (b! = 0) / * We used uninitialized variables*/ / *% d and "We should put b here" don't match*/ printf ("We make a mistake again! b =% d\ n", "We should put b here")

}

Void miss_decl () {

/ * / * This type of annotation is prohibited*/

Printf ("We should put the declaration before it's been used!\ n")

}

The above code deliberately creates a lot of common problems in programming, and then we'll use this code to test the various commonly used warning facilities provided by GCC.

First of all, we do not use any warning facilities to compile the above program gcc test_warning.c-o test_warning. By default, GCC will give output, in which GCC identifies two problems: non-standard main functions (warning) and the use of undeclared functions (error), but the other GCC is not aware of it.

1. Use-pedantic to find codes that do not conform to the ANSI / ISO standard

Execute the following command: gcc-pedantic test_warning.c-o test_warning can see that this time GCC reports the use of long long in the code as a warning, but to be clear, we cannot rely on this option to ensure that our code fully conforms to the ANSI / ISO standard, because this option only reports what ANSI C requires the compiler to check. Alternatively, you can use-pedantic-errors to have GCC turn all warnings into errors.

two。 Use-Wformat to check for parameter mismatches in printf and execute the following command: gcc-Wformat test_warning.c-o test_warning

3. Use-WComment to find errors in the comments and execute the following command: gcc-WComment test_warning.c-o test_warning

4. Use-Wparentheses to find the = error in the bool expression and execute the following command: gcc-Wparentheses test_warning.c-o test_warning

5. Use-Wuninitialized to find the use of uninitialized variables to execute the following command: gcc-O-Wuninitialized test_warning.c-o test_warning it is worth noting that when using this option, be sure to use the-O option (which we'll talk about later)

6. Use-Wimplicit-function-declaration /-Werror-implicit-function-declaration to check the use of undeclared functions to execute the following command: gcc-Wimplicit-function-declaration test_warning.c-o test_warning plus-Werror-implicit-function-declaration and-Wimplicit-function-declaration work similarly, except that if you use an undeclared function, the former will consider it an error.

7. If you just want to conduct a comprehensive review of your code, you don't have to list the above options side by side. GCC provides the-Wall option, which means to list all the warnings in the code and execute the following command: gcc-Wall test_warning.c-o test_warning 8. If you want to go to the other extreme, that is, you don't want gcc to output any warnings, use the-w option, which prohibits all warnings from executing the following command: gcc-w test_warning.c-o test_warnin

For all of the above options, you can use them with the-Werror option, which turns all warnings into errors. In addition, if you just want to check the code and do not compile, you can use the-fsyntax-only option, such as the following command such as gcc-fsyntax-only test_warning.c basically, some of our commonly used warning options are these, and-Wall is our very common function.

two。 The part of optimization options can be divided into two parts, one is to let the compiler analyze the code.

The other part of the code optimization is that we can develop some information about the hardware for the compiler to generate code that is better integrated with the hardware, and the reason why we want to compile the program with source code, in many cases, is for this reason.

First, let's look at code optimization. In terms of overall code optimization, GCC provides the following options

-O-O1

The meaning of these two options is the same. GCC will perform optimizations that reduce code size and execution time, and this level of optimization will not be performed for those optimizations that seriously affect compile time.

-O2

At this level, GCC will provide all supported optimizations, but this does not include space-for-time optimizations, such as compilers that do not use loop expansion and function inlining. Compared to-O, this option further speeds up compilation time and code generation performance.

-O3

In addition to the optimization options provided by-O2, the-finline-functions,-funswitch-loops and-fgcse-afer-reload options are specified for only one purpose, which is to perform code optimization with full effort.

-Os

This option is specifically designed to optimize code size, and-Os turns on all optimization options that do not increase code size significantly at the-O2 level.

-O0

This option means that no optimization is performed

It is important to note that although GCC provides four overall optimization options, 1x3 and s, in terms of the actual optimization effect, the efficiency of the program optimized by O3 is often not * *, but in most cases we use-O2. If you want to get the efficiency benefits of * *, you might as well try all four options. In addition, these options are just a combination of many unilateral optimizations provided by GCC. If you want to know more specific optimizations, you can check out the GCC manual, which will not be discussed in detail here due to space limitations. One thing to remember is that if your program is for high-precision numerical calculations, remember not to use any of the optimization options above.

Let's take a look at hardware-based optimization. Since this part is related to computer hardware, we only use Intel's CPU to explain: for all optimization options provided for Intel and AMD x86-64, they all start with m. Here are some common options:

-march

This option is used to specify the type of CPU, such as i386\ i486\ i586\ pentium-mmx\ i686\ pentium2\ pentium3\ pentium-m\ pentium4\ prescott\ athlon\ athlon-4\ K8 and so on. Readers can specify them according to their own circumstances.

-mfpmath

This option is used to specify the type of floating-point unit. Include

three hundred and eighty seven

Using a standard mathematical coprocessor

Sse

Use the scalar floating-point operations provided by the SSE instruction set. This feature is supported on Pentium3\ Athlon-4 and newer chips. In addition, SSE2 can perform double-precision floating-point calculations on pentium4 and AMD x86-64 processors.

Sse,387 uses a mix of 387 mathematical coprocessors and SSE instructions, which makes full use of CPU's floating-point registers and xmm registers, but this option is still in the experimental stage.

-malign-double

This option causes GCC to align variables of type double\ long double\ long long on 4-byte or 2-byte addresses, which makes code execution faster on Pentium-level CPU, at the expense of more memory to execute the program. -mmmx-msse-msse2-msse3-m3dnow these options are used to start built-in functions that directly use these processor extension instructions. It is very effective to use them when compiling 3D or multimedia programs.

3. Support for debugging when the program goes wrong, we can easily debug in Visual Studio, but in Linux, once Segmentation Fault appears, it seems that we have no better choice except to look at the code with our eyes, in fact, it is not the case, we can use GDB to debug the program by adding some appropriate debugging information to the program with GCC. Here, we introduce the most commonly used-g and-ggdb options.

Let's take a look at-g first. This option makes use of the operating system's "native format (native format)" to generate debugging information. GDB can use this information directly. Although we can use-O and-g together, this is highly deprecated.

If you want to debug your program with GDB, you can use-ggdb to have GCC generate richer debugging information for GDB, but you can't debug with other debuggers at this time.

Finally, both of the above options can accept a level of output debugging information, and the default level is 2. If you specify level 1 (- G1), GCC generates the least debug information, including descriptions of functions and global variables, but does not output information such as local variables and line numbers at this level. The other level is level 3 (- G3), at which GCC generates debugging information for all macro definitions and symbols in the program.

After reading the above, do you have any further understanding of how to get started with linux program development? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report