Linux Makefile simple example tutorial

Source: Internet
Author: User
(Test version: RedHat9.0 Simplified Chinese version) first create the following instance program text: /************************** filename: main. c **************************/# include & lt; stdio. h & gt; # include & lt; stdlib. h & gt; # include "hello. h "# include" init. h"

(Test version: RedHat 9.0 Simplified Chinese Version)

First, create the following instance program text:

/************************** Filename: main. c **************************/

# Include

# Include


# Include "hello. h"

# Include "init. h"


Void aftermain (void)

{

Printf ("\ n ");

Printf ("<>>>>>>>>> \ n ");

Printf ("... \ n ");

Return 0;

}


Int main (int argc, char * argv [])

{

Printf ("========= main =========\ n ");

Init (1, 1234 );

Hello (argc, argv );

Atexit (aftermain );

Printf ("... exit main... \ n ");


Return 0;

}

/********************** Filename: init. c ***************************/

# Include

# Include "init. h"


Const char ro_data [1024] = {"This is readonly data "};

Static char rw_data [1024] = {"This is readwrite data "};

Static char bss_data [1024];


Int init (int number)

{

Printf ("input number: % d \ n", number );

Printf ("ro_data: % x, % s \ n", (unsigned int) ro_data, ro_data );

Printf ("rw_data: % x, % s \ n", (unsigned int) rw_data, rw_data );

Printf ("bss_data: % x, % s \ n", (unsigned int) bss_data, bss_data );

Return number;


}

/******************* Filename: hello. c ********************/

# Include

# Include "hello. h"


Int hello (int argc, char * argv [])

{

Int I;

Printf ("Hello world! \ N ");

For (I = 0; I

{

Printf ("argv [% d] = % s \ n", I, argv [I]);

}

Return 0;

}

/**************** Filename: init. h *************************/

# Ifndef _ INIT_H _

# Define _ INIT_H _


Int init (int number );


# Endif

/********************* Filename: hello. c *********************/

# Ifndef _ HELLO_H _

# Define _ HELLO_H _


Int hello (int argv, char * argc []);


# Endif

You can see from the dependency:

All: main. o hello. o init. o

Main. o: main. c hello. h init. h

Hello. o: hello. c hello. h

Init. o init. h init. c


Create a terminal and enter

Vi makefile1

Create a makefile1 text file

All: main. o hello. o init. o

Gcc-o myapp main. o hello. o init. o


Main. o: main. c hello. h init. h

Gcc-c main. c

Hello. o: hello. c hello. h

Gcc-c hello. c

Init. o: init. c init. h

Gcc-c init. c

Note: ":" is followed by a tab rather than a space.

Input make-f makefile on the terminal to complete the compilation.

Note in Make file starts #

Related Article

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.