CentOS 7 HelloWorld with C

Source: Internet
Author: User
Tags gpg

In fact, I do not know for what, at this age also want to learn Linux under the C language programming. Because I have always been silly to think that the opportunity is to be prepared for the people, but also stay in the thought of living to learn old. Now Android so fire, a variety of terminal springing up, and these terminals if not installed Windows, will be bound to use open source Linux, and Linux running on the program, C or occupy a large market, once the time is ripe, you can change the foyer immediately, do not produce career delays. This is the purpose of my stupid. In Shenzhen, especially now this time, people around the thought is how quickly into the stock market, to kill into the housing market, while the bull's tail to earn a pot full, buy a car, marry mating, realize the great ideal of life. My idea seems naïve, but right now, when I publish this essay, that's exactly what I really think.

Back to today's theme, I would like to start with a simple Hello,world program, and then talk about module, makefile and so on some very basic C programming, if you are familiar with Linux and C, then you can choose to bypass this article, Or you can post your suggestions at the back, welcome criticism, but refuse to spray the green son, we are all over that age.

First look at the GCC version I'm using

cc -V

Or

GCC -V

As to 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 compiler under UNIX, and now on most Linux, the CC is connected to GCC. This means that CC is a soft connection to GCC.

1, start our first program code to write, here I use the editor is vim

Vim hello.c

and enter it in the hello.c.

#include <stdio.h>int main (intChar* argv[]) {    PRINFTF ("  hello,world! " );         return 0 ;   }

: wq! After you come out, continue typing

gcc hello.c-o hello.out &&./hello.out

Just so simple, we can get our great Hello, world program.

I'm just going to add that here. If you do not use the-O hello.out to specify the filename of the output, then GCC will generate a file called a.out by default. Then the connection in the back should be changed to a.out, that is,

gcc hello.c &&./a.out

Of course, if you think this && abrupt out of the moment, do not know what to do, then you can also be divided into two batches to execute, first compile, and then execute. In fact, the function of && is to submit two commands together, if the previous command if there is no return error, then continue to execute the next command. The above statement is compiled hello.c without errors, followed by the implementation of the newly compiled program. For me, I like to use && to connect, I downloaded the source program on the Internet also often like make && make install to do one step. Because I think./configure is not wrong, there is basically no big problem, of course, this is not absolute.

2, the great Hello,world is done. The reason is great, because it is extremely simple, the martial arts fiction often also said that simple is practical, such as Yang practiced alone nine swords, the use of the sword is more than the ordinary man's sword to be more heavy, that said, not Feng. But has been relying on the simple way to kill the enemy words, it is estimated that also can not achieve the gods carved heroes. So there are solitary nine, and sorrowful palm and so on, so next, we also come to change hello.c a little bit, note is a little bit, is still very simple, that is, we let hello.c in the main method to call another method.

#include <stdio.h>intMainintargcChar*argv[]) {    intA= -; intb= A; intmaxno=Max (A, b); printf ("The max number is %d", maxno); return 0; }intMaxintAintb) {    if(A&GT;B)returnA; returnb;}

Well, the above piece of code is final output:

 is .

It is a good start to get the results as we wish. But let's just think, if this Max function was developed by someone else, wouldn't it be possible for two people to modify the hello.c file? If that's the case, it's a mess, and the merge will make people crazy. For the sake of not being mad, in order to be less mad, we save Max this function in a max.c file, or by another colleague.

Here, let me tell you something about my operation.

Vim hello.c

Open the edit, and then move the cursor to the line int max (int a,intb), press 5DD to get 5 lines to the Clipboard, then: SP MAX.C, then ctrol+w+ the cursor key, and finally press p to paste the copied 5 lines into max.c, finally: wqa!

After we have separated two files, our compilation is done like this:

gcc max.c hello.c-o hello.out

Final execution

./hello.out

You can also get the right result you want.

.

3, well, very well, completed the separation, and the results are as expected. Perfect solution. But soon after, someone realized a min.c module, and informed the HELLO.C to call and show the smallest data out. So the compilation of the corresponding changes to the

gcc max.c min.c hello.c-o hello.out

Yes, it is easy to enumerate the relevant. c files, but if the project is complicated,. c files have thousands of, wouldn't it be exhausting the programmer, exhausted that released colleague? Of course not, we do not abandon to everyone is not to give up. So we use Makefile to save them.

# This was for hello.  out Hello.  out : max.c min.c hello.c        -O hello.  out

Then enter make directly in bash. Of course, as a first-time use, I still haven't forgotten to use MAKE-V to see what version number I'm using:

The result of the final execution is also the desired result.

4, it seems perfect, but we go to see the content of makefile, there is always a strange feeling. Of course, if not found also very normal, after all, we have only three files, and now the machine is also very fast, do not make any difference. In fact, if we go to see the online download makefile, found a lot of files that do not change, there will be. O in the form of. In addition, the source code may also be filled with many. h files. So what are these files for? Why does this kind of file exist? Before answering such questions, let me first affirm that we do not have to name them by. O,. h. O is the module file, that is, the infrequently-changed. c files can be compiled into. o as a module. h files are described to the caller. He typically only contains the signature of the method of the. c file. A bit closer to the high-level language is the equivalent of interface in C # (Java).

Next, I will turn the max.c into MAX.O, and turn min.c into MIN.O to save. and generate two copies of the. h file, and finally provide a closer to the production of the makefile, note that the M is to be capitalized, this is the agreement, and this agreement is immutable.

GCC-c MIN.C

The contents of the Max.h and Min.h files are then entered as:

int max (intint b);

And

int min (int A,int b);

The last Makefile

# This was for hello.  out Hello.  out : MAX.O min.o hello.c        -O hello.  out max.o:max.c       -C max.cmin.o:min.c      to Min.c

5, finally, I put all my code on it.

int max (intint  b) {    ifreturn  A;     return b;}
max.c
int max (intint  b) {    ifreturn  A;     return b;}
min.c
#include <stdio.h>#include"max.h"#include"Min.h"intMainintargcChar*argv[]) {    intA= -; intb= A; intmaxno=Max (A, b); intminno=min (A, b); printf ("The max number is %d"\nthe min number is%d\n", Maxno,minno);     return 0; }
hello.c

6, about my first Linux C program is like this, Happy ending, right?

7, no, such a result is not perfect. As a programmer, I did not feel very happy, because the above does not talk about how to debug. is a simple one-off program, it is really great to be able to write all of the program logic (including complex) at once. Static compilation errors Fortunately, the make stage can be found, but some logic is not so easy to find. The programmer's ultimate weapon Debug will be sacrificed. The GDB tool is commonly used in Linux. Follow the convention to see my version first:

In order to debug the small program above me, I will remove all *.O files and hello.out first.

RM -RF *. orm -RF hello.out

Then modify the makefile as follows:

# This was for hello.  out Hello.  out : MAX.O min.o hello.c        -G max.o min.o hello.c-o hello.  out max.o:max.c       -G-c      max.cmin.o:min.c-g-c min.c

After you execute make. Continue Typing

GdB./hello.  out

This loads our hello.out into the memory, and we can debug it. As to what is stack memory, heap memory, data memory, code memory These, I will not say, you can go back to the University tutorial or the network to search for yourself, paste out some of my simple debugging operations:

8, finally solve a small problem, that is, in the above diagram, we can see that there has been a mistake said missing separate Debuginfos, Use:debuginfo-install glibc-2.17-78.el7.x86_64

In fact, to solve this problem is also very simple, directly under bash input

debuginfo-Install glibc

The premise is that I already 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 P  Ick mirrors that is updated to and# geographically close to the client. You should use this forCentOS updates# Unless you is manually picking other mirrors.## all debug packages from all the various CentOS-7releases# is merged into a single repo,Splitby basearch## Note:packagesinchThe debuginfo repo is currently not signed#[base-Debuginfo]name=centos-7-Debuginfobaseurl=http://debuginfo.centos.org/7/$basearch/gpgcheck=1Gpgkey=file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-debug-7Enabled=0#
Centos-debug.repo

If not, you need to go to http://debuginfo.centos.org/7/x86_64/to find the exact same debuginfo as the kernel. For example, the kernel of the machine is the CentOS Linux 7 (CORE) Kenerl 3.10.0-229.el7.x86_64 on a x86_64, which is the information that can be seen when entering the login for the user name. Then you should

wget http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-3.10.0-229.el7.x86_64.rpm wget http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-common-x86_64-3.10.0-229. el7.x86_64.rpm

Then rpm installs the RPM package to download, but I think it might be possible not to download these two. Finally, it is modeled after configuring the Yum source and executing

debuginfo-Install glibc

can solve this problem.

9, as to how to use GDB, find two articles online

Http://www.cnblogs.com/hankers/archive/2012/12/07/2806836.html

http://blog.csdn.net/feixiaoxing/article/details/7199643

CentOS 7 HelloWorld with C

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.