LinuxC programming (3)-a simple example

Source: Internet
Author: User
LinuxC programming (3)-a simple example-general Linux technology-Linux programming and kernel information. The following is a detailed description. /* Main. c/

# Include "print. h"

Int main (void)

{

Myprint ("hello world! ");

}

Code Description:

Main. c calls a myprint function, which is defined in print. h.

The purpose of calling the myprint function is to print a "hello world !"

Define the header file in print. h.







/* Print. h */

# Ifndef _ MYPRINT_H

# Define _ MYPRINT_H



Void myprint (char * print_str );



# Endif

Code Description:

# Ifndef _ MYPRINT_H: The naming rule for declarative recognition is generally that the header file name is in uppercase, followed by an underscore, and "." In the file name is also underlined.

Implement a simple print and display function.







/* Print. c */

# Include



Void myprint (char * print_str)

{

Printf ("This is my first linux's program: % s \ n", print_str );

}

Code Description:

The myprint function uses the standard output function printf, so the header file includes stdio. h.

(4) use makefile for project management. The contents of makefile are as follows.









Main: main. o myprint. o

Gcc-o main. o myprint. o

Main. o: main. c

Gcc-c main. c

Myprint. o: myprint. c print. h

Gcc-c myprint. c

Code Description:

The base principle of make is "dependency". The executable file main depends on main. o, myprint. o, similarly, main. o depends on main. c, and myprint. o depends on myprint. c and print. h.

Gcc is used to program source files into target files, such as gcc in code? C main. c is to compile main. c into main. o,

Save the source and makefile files in the same folder in Linux, and then run the make compilation link program as follows:

[David @ XUEER ex0101] $ make

Gcc-c main. c

Gcc-o main. o myprint. o

[David @ XUEER ex0101] $./main

This is myfirst linux's progruam: hello world!

So far, this small program is complete.

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.