Write makefile-for C and Java

Source: Internet
Author: User

To organize an old JAVA project, the original development environment is no longer available. Find the source code and re-build the compilation environment. Decided to use Makefile as part of the source code. Make is generally used in gnu c. We can think of it as a shell that can complete the compilation work we expect, whether it is C or java. Makefile always needs to be written for half a day each time, so you don't remember it every time. To sum up, you can easily copy it later.

Simply put the compiled command into it. For example, in clutter learning, the most primitive method is as follows:

Stage:

Gcc-Wall-g stage. c-o stage 'pkg-config clutter-1.0 -- cflags -- libs'

This is also the only way I can remember. To help myself understand, if it wasn't a big project in the past, I would add one project at a time. The original is also the best. If you want to have a template, you can change the parameters to adapt to different situations. You can use the following method:

CFLAGS =-Wall-g 'pkg-config clutter-1.0 clutter-imcontext-0.1 -- cflags'

LIBS =-Wall-g 'pkg-config clutter-1.0 clutter-imcontext-0.1 -- libs'

TARGET1 = mytarget

OBJ1 = file1.o file2.o file3.o ....

% O: % c

$ (CC) $ (CFLAGS)-c $ <-o $ @

$ (TARGET1): $ (OBJ1)

$ (CC) $ (LIBS)-o $ (TARGET1) $ ^

All: $ (TARGET1)

Clean:

Rm $ (TARGET1) *. o

The key here is % o: % c. After the colon, it indicates the dependent file. For example, test: main. o test1.o test2.o. The following three files are used to generate a list of files on which the target file test depends. % O here refers to the goal ending with o, that is, the filen. o defined in OBJ1, and the corresponding % c, that is, the corresponding filen. c. $ @ Indicates the list of target files under the current rule. In this example, filen. o is used. $ <Indicates the first file in the dependent file list, namely, filen. c.

$ (TARGET1): $ (OBJ1). The target file is $ (TARGET1), that is, mytarget, and the dependent file is $ (OBJ1), that is, file1.o file2.o file3.o. To generate the related file1.o file, the matching % o: % c is found. If it is file1.o, It is file1.o: file1.c. Get file2.o, file3.o, and generate the final execution file, $ (CC) $ (LIBS)-o $ (TARGET1) $ ^, $ ^ indicates all dependencies in the dependency file list, where all filen. o.

For a project, we usually have multiple subdirectories, each of which completes specific content. Different subdirectories may be maintained by Different developers. Each subdirectory has its own Makefile. The project only needs to traverse each subdirectory and execute its make command. The simplest way is to enter the subdirectory util and run make. You can simply write it as follows:

Wei:

(Cd util & make)

The following is a relatively formal example. In fact, we can use the simple method above, and I still like it easily:

SUBDIRS = util sip ......

Define make_subdir

@ For subdir in $ (SUBDIRS); do/

(Cd $ subdir & make $1 )/

Done;

Endef

All:

$ (Call make_subdir, all)

Rm:

$ (Call make_subdir, rm)

Install:

$ (Call make_subdir, install)

The key here is to define the processing of make_subdir. For every $ (SUBDIRS), such as util and sip, it is assigned to subdir and executed to enter the subdirectory. Running make and make carries parameters, it carries the first parameter ($1) of the call ). The method for calling make_subdir is $ (call make_subdir, param1, param2 ....).

For java compilation, write the Makefile below the subdirectory as follows:

CLASS_PATH = ../classes

SRC_FILE = Debug WeiUtil name1 name2 name3...

Define compile

@ For file in $ (SRC_FILE); do/

Echo javac-d $ (CLASS_PATH) $ file. java ;/

(Javac-d $ (CLASS_PATH) $ file. java) | exit ;/

Done;

Endef

All: rm

$ (Call compile)

Rm:

Rm-rf $ (CLASS_PATH)/wei/util

The Traversal method is also used here. It should be noted that when a compilation error occurs, we want the compilation to stop, so we make a judgment. If the compilation fails, exit. In this case, we wrote a java Makefile. Build your own compiling environment. You can also organize large projects without using Eclipse or other tools. As long as there is code, the development environment is there.

Related Links: My JAVA articles

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.