CentOS 7 Helloworld with c, centoshelloworld
In fact, I don't know why. At this age, I still want to learn C Programming in Linux. Because I have always been dumb and think that the opportunity is a preparation person, and I have always thought that I will never learn again. Now, when Android is so popular, various terminals spring up. If these terminals do not install Windows, they will inevitably use open-source Linux, and the programs running on Linux will, C still occupies a very large market. Once the time is ripe, you can change the car to the court immediately to avoid career delays. This is my initial intention. In Shenzhen, especially during this time period, people around me think about how to quickly integrate into the stock market, go into the housing finance market, and take advantage of the bull market's tail to buy a house, buy a car, and marry Bai fumei, realize the great ideal of life. I seem very naive, but at this moment, when I publish this essay, I do think so.
Back to today's topic, I want to use a simple Hello, World Program, Module, Makefile, and other very basic c programming, if you are familiar with Linux and C, you can choose to bypass this article, or you can post your suggestions in the future. Thank you for your criticism and correction, however, we refused to drop our qingzi, who had passed that age.
First, let's take a look at the gcc version I use.
cc -v
Or
gcc -v
As for why the two commands get the same information gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC? This is because cc is a unix compiler, And now most linux systems connect cc to gcc. That is to say, cc is a soft connection of gcc.
1. Let's start writing the first program code. The editor I use here is vim.
vim hello.c
Enter
#include <stdio.h>int main(int argc, char* argv[]){ prinftf("Hello,World!"); return 0; }
: Wq! Enter
gcc hello.c -o hello.out && ./hello.out
In this simple way, we can get our great Hello, World program.
Here I just want to add a description. If-o hello. out is not used to specify the output file name, gcc will generate a file named a. out by default. Then the subsequent connection should be changed to a. out, that is
gcc hello.c && ./a.out
Of course, if you don't know what it is for the moment, you can divide it into two batches for execution, compile it first, and then execute it again. In fact, the role of & is to combine the two commands for submission. If the previous command does not return an error, continue to execute the next command. The above statement is to compile hello. c without errors, and then execute the program just compiled. For me, I like to use & to connect, and the source programs I download online often like make & make install for one-step execution. I think there is nothing wrong with./configure. there is basically no big problem in the future. Of course this is not absolute.
2. The great Hello World is complete. The reason for greatness is that it is extremely simple. In martial arts novels, it is often said that simplicity is practical. For example, Yang Xiaolian's solitary sword, the sword used is much heavier than the ordinary sword, that is, the heavy sword is not a front. However, if we continue to confront the enemy with simple tactics, it is estimated that we will not be able to achieve the hero. So there are also nine standalone, there is a sudden loss of soul, and so on, so next, we will also put hello. c is a little changed. Note that it is a little bit, and it is still very simple, that is, let's make hello. the main method in c calls another method.
#include <stdio.h>int main(int argc, char* argv[]){ int a=33; int b=22; int maxNo=max(a,b); printf("the max number is %d",maxNo); return 0; }int max(int a, int b){ if(a>b) return a; return b;}
Well, the above Code finally outputs:
the max number is 33.
It is a good start to get results as we wish. But let's think about it. If this max function is jointly developed by someone else, it is impossible for two people to modify the hello. c file? If that is the case, it will be messy, and merge will drive people crazy. In order not to be crazy, we can save the max function into a max. c file, or implement it by another colleague.
Here I will talk about my operations like this.
Vim hello. c
Open edit, move the cursor to the line of int max (int a, intb), press 5dd to get 5 rows to the clipboard, and then: sp max. c. Then, ctrol + w + the cursor key, and press p to paste the copied five lines into max. c, and finally: wqa!
After the two files are separated, we need to compile them like this:
gcc max.c hello.c -o hello.out
Last executed
./Hello. out
You can also get the expected results.
the max number is 33.
3. Well, the separation is completed, and the result is as expected. Perfect solution. Soon after, someone implemented a min. c Module and notified hello. c to call it and showed the smallest data. So the corresponding compilation is changed
gcc max.c min.c hello.c -o hello.out
That's right. the c file can be listed, which is easy to use. However, if the project is complex ,. there are thousands of c files. Isn't it necessary to get tired of programmers and the colleagues who have released the files? Of course not. We don't give up on everyone. Therefore, we use Makefile to save them.
#this is a make file for hello.outhello.out:max.c min.c hello.c gcc max.c min.c hello.c -o hello.out
Then input make in bash. Of course, for the first time I used it, I still did not forget to use make-v to check the version number I used:
The final execution result is also the expected result.
4. It seems perfect, but there is always a strange feeling when we look at the Makefile content. Of course, it would be normal if we didn't find it. After all, we only have three files, and the current machine is also very fast, so there will be no difference when executing make. In fact, if we look at the Makefile downloaded from the internet and find many files that do not change much, there will be a. o format. In addition, the source code may be filled with many. h files. What are these files. Why does this type of file exist? Before answering these questions, I want to declare that it is not necessary to begin. o ,. h ,. o is the module file, which is not often changed. c files can be compiled. o is saved as a module. The. h file is explained to the caller. Generally, it only contains the signature of the method in the. c file. It is closer to the advanced language, that is, the interface in c # (java.
Next, I will convert "max. c" to "max. o" and "min. c" to "min. o. Generate two. H files, and finally provide a Makefile that is closer to production. Note that M must be capitalized, which is also an agreement and cannot be changed.
gcc -c max.c gcc -c min.c
Then the input content of the max. h and min. h files is:
int max(int a, int b);
And
int min(int a ,int b);
The final Makefile
#this is a make file for hello.outhello.out:max.o min.o hello.c gcc max.o min.o hello.c -o hello.outmax.o:max.c gcc -c max.cmin.o:min.c gcc -c min.c
5. Finally, I Will paste all my code.
Int max (int a, int B) {if (a> B) return a; return B ;}Max. cint max (int a, int B) {if (a <B) return a; return B ;}Min. c # include <stdio. h> # include "max. h "# include" min. h "int main (int argc, char * argv []) {int a = 33; int B = 22; int maxNo = max (a, B ); int minNo = min (a, B); printf ("the max number is % d" \ nthe min number is % d \ n ", maxNo, minNo); return 0 ;}Hello. c
6. My first linux C program is like this, Happy ending, right?
7. No. Such an ending is not perfect. As a programmer, I didn't feel very happy because I didn't talk about how to debug it. It is a simple program once, and it is really awesome to write all the program logic (including complicated ones) at once. Static compilation errors can be found in the make stage, but some logics are not so easy to find. The programmer's debugging weapon is about to be sacrificed. In linux, the gdb tool is generally used. By convention, let's take a look at my version first:
To debug the small program above, I will delete all the *. o files and hello. out files first.
rm -rf *.orm -rf hello.out
Then modify the Makefile as follows:
#this is a make file for hello.outhello.out:max.o min.o hello.c gcc -g max.o min.o hello.c -o hello.outmax.o:max.c gcc -g -c max.cmin.o:min.c gcc -g -c min.c
Run make. Continue Input
gdb ./hello.out
In this way, hello. out is loaded into the memory and can be debugged. As for what stack memory, heap memory, data memory, and CODE memory are, I will not talk about them anymore. You can go back to college tutorials or search for them on the Internet, paste some of my simple debugging operations:
8, finally solve a small problem, is in the figure above, we can see that one of the errors said Missing separate debuginfos, use: debuginfo-install glibc-2.17-78.el7.x86_64
In fact, to solve this problem, it is also very simple. Enter
debuginfo-install glibc
The premise is that I have/etc/yum. repos. d/CentOS-Debug.repo as follows:
# CentOS-Debug.repo # The mirror system uses the connecting IP address of the client and the # update status of each mirror to pick mirrors that are updated to and # geographically close to the client. you shoshould use this for CentOS updates # unless you are manually picking other mirrors. # All debug packages from all the various CentOS-7 releases # are merged into a single repo, split by BaseArch # Note: packages in the debuginfo repo are currently not signed # [base-debuginfo] name = CentOS-7-Debuginfobaseurl = Signature #CentOS-Debug.repo
If not, you need to first go to the http://debuginfo.centos.org/7/x86_64/ to find the debuginfo that is exactly the same as the kernel. for example, the machine kernel is CentOS Linux 7 (Core) Kenerl 3.10.0-229. el7.x86 _ 64 on an x86_64, which is displayed when you enter the user name for logon. Then we should
wget http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-3.10.0-229.el7.x86_64.rpmwget http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-common-x86_64-3.10.0-229.el7.x86_64.rpm
Then, install and download the rpm package. However, I think it is feasible that you do not need to download these two packages. Finally, configure the yum source and execute
debuginfo-install glibc
You can solve this problem.
9. For how to use gdb, find two articles on the Internet.
Http://www.cnblogs.com/hankers/archive/2012/12/07/2806836.html
Http://blog.csdn.net/feixiaoxing/article/details/7199643