Introduction to Linux programming-GCC, make, and GDB __arduino
Source: Internet
Author: User
Welcome to the Linux programming world.
If you're already a good programmer under Windows, then here, you will find a new world, although in Linux programming than in Windows programming there are all kinds of inconvenience, but her many charming will certainly let you linger-----as long as you are a true "love" Programming programmers; If you have rarely programmed before, but have just entered this gate, then you are in the right place, where you will get more guidance and help than MSDN, and you will quickly grow into a real programmer----if you want to be. Of course, the most appealing thing here is the spirit of free software. Perhaps, in the future, you will take this place as your own home.
To be a good programmer in Linux, the first thing you have to touch is GCC, make, and GDB. If you know something about Microsoft's visual-Series development tools, the following equations will give you an idea of them, though not as appropriate:
GCC + make + gdb = Visual C + +
GCC (GNU cc) is a compiler suite that not only compiles C, objective C and C + + programs, but also compiles programs written in languages such as Fortran and Pascal. But as far as the compiler is concerned, it is the fastest and most efficient compiler at the moment, and, of course, this is an unofficial statement.
Make is a project management tool. If you've been used to knocking F9, Ctrl+f9, or clicking a button to compile a running program, this tool will make you understand how this all happens.
GDB (GNU DeBugger) is a debugger that is essential to a programmer. GDB is very powerful, but if you use a friendly graphical interface as one of the major metrics, then there is really a lack of good debuggers in Linux at this stage. Xxgdb and KDE's debugger are working on this.
As with most textbooks and articles, we are from "Hello world!" Begin.
We first use VI or some other graphical editor to generate a hello.c file:
#include
void Main (void)
{
printf ("Hello world!");
}
We compile using gcc:
GCC Hello.c-o Hello
Generate executable file in current directory Hello, run this file, you will naturally see the words Hello world!. Here hello.c is the source file, the-O is the compilation option, and Hello is the file name you specified. Note that you do not use the "common" names of test and help to name your executable files, and they are often already caught by the system.
If you refer to other header files in your program, you need to tell the compiler where to find the header files using the-I pathname option. For example, there are two lines in the hello.c file:
#include
#include
Where Myheadfile1.h is in the directory/usr/local/include directory, myheadfile2.h in the previous level directory, the compilation commands are as follows:
Gcc-i/usr/local/include-i.. Hello.c-o Hello
If you use a call that is not in the default library in your program, for example, to call the Pthread_create () function in hello.c to create a multithreaded, enter the following code:
......
#include
......
Pthread_create (t,attr,0,0);
......
Compiling with the command above will make an error: undefined reference to ' pthread_create '. This is because the compiler was not told where to find the call containing the name Pthread_create. Pthread_create are in the/usr/lib directory in the library file libpthread.a (static library) and libpthread.so (Dynamic Library). Specifies the location of the library with the-l path name, using-lfile to specify a static or dynamic library named Libfile. The compilation commands for the hello.c containing the pthread_create are:
Gcc-l/usr/lib-lpthread Hello.c-o Hello
When a program is a project and contains many files, you can build the target file with the-C option and then connect the target file with the-O option to generate the executable file. For example, the project's source files are server.c and client.c, and you want to generate an executable named MyApp, the compilation command is:
Gcc-c server.c
Gcc-c client.c
GCC server.o client.o-o MyApp
The above compilation command is equivalent to the following compilation command:
GCC client.c server.c-o MyApp
When a project contains more than one file, each typing tedious compile command may scare away all the developers who are attracted to Linux. It's time to make a point.
Make looks for a file named Makefile or makefile in the current directory, and then executes the file sequentially. Take the previous MyApp as an example:
MYAPP:SERVER.O CLIENT.O
GCC server.o client.o-o MyApp
Server.o:server.c
Gcc-c server.c
Client.o:client.c
Gcc-c client.c
Where, for the Keyboard tab, you can't replace them with a few spaces, even if they look the same. Lines 1th, 3, and 5 define dependencies, and lines 2nd, 4, and 6 define the corresponding compilation commands. Make scans 1, 3, 5 rows, the target does not exist, call the corresponding command line compilation, if already exists, compare the file date, if the file after the colon is new to the colon, the same recompile. Thus, if the user modifies only one file client.c, the SERVER.O is not recompiled when you recompile with make, which saves time.
The journey of the journey begins with the other. Although this is just a simple beginning, but the door to a mysterious palace has been opened, how to become a qualified programmer, all to rely on efforts to develop a look at documents, see How-to, see the good habits of master procedures, and try to practice themselves, everything will be better
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service