05. How to write your own h and C files, and manage multiple source files by project-make

Source: Internet
Author: User

We are writingProgramIn this case, some methods will be encapsulated for reuse, which involves compilation of multiple files, how to compile these different files, link them to the final executable program, and how to manage these files, one compilation?

Compile the math. h header file

/* -- ===-------------------------------------------- = ---
Filename: Math. h
Simple File Operations
-- ===-------------------------------------------- = --- */
Extern   Int Add ( Int I, Int J );

 
Compile the math. c file

/* -- ===-------------------------------------------- = ---
Filename: Math. c
Implement the methods defined in the math. h file
Compilation Method: gcc-C math. C-o math. o
-- ===-------------------------------------------- = --- */
Int Add ( Int I, Int J)
{
Return I + J;
}

Compile main. C program

/* -- ===-------------------------------------------- = ---
Filename: Main. c
Main Program, test the math. h and math. c files.
Compilation Method: gcc-C main. C-o main. o
-- ===-------------------------------------------- = --- */
# Include " Math. h "
# Include < Stdio. h >
Int Main ( Int Argc, Int Argv)
{
Printf ( " The sum 3 + 5 = % d. \ n " , Add ( 3 , 5 ));
Return   0 ;
}

FinallyGCC main. O math. O-o mainGenerate the main program.

Xumh @ Ubuntu :~ /CPP/make $ cat makefile
Main: Main. O math. o
GCC main. O math. O-o main

Main. O: Main. C math. h
Gcc-C main. C-o main. o

Math. O: Math. C math. h
Gcc-C math. C-o math. o

Clean:
Rm-f *. o
Xumh @ Ubuntu :~ /CPP/make $

 

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.