当前位置: 技术问答>linux和unix
请教:这个makefile的写法
来源: 互联网 发布时间:2016-09-19
本文导语: 在工程目录(Proj)下有如下几个子目录: headers(用于统一存放.a静态库的头文件,以便于其它程序调用.a文件时,统一到该目录下寻找相应的头文件); lib(用于统一存放.a静态库文件,其它程序到该目录下寻找库...
在工程目录(Proj)下有如下几个子目录:
headers(用于统一存放.a静态库的头文件,以便于其它程序调用.a文件时,统一到该目录下寻找相应的头文件);
lib(用于统一存放.a静态库文件,其它程序到该目录下寻找库文件,调用相应的方法);
myMaths(数学功能的.c文件,编译后生成.a静态库(置于上面lib目录下));
operations(应用程序目录,调用上述的数学功能库)。
-----------------------
在headers目录下,有myMaths.h头文件。
其内容为:
int myIntAdd(int a, int b);
int myIntMinus(int a, int b);
在myMaths目录下,有myMaths.c实现文件。
其内容为:
#include "myMaths.h" //希望引用上述../headers/目录下的myMaths.h头文件
int myIntAdd(int a, int b)
{
return a+b;
}
int myIntMinus(int a, int b)
{
return a-b;
}
--------------------------------------------
现在想在myMaths目录下,写一个makefile文件,
将myMahts.c中的两个功能函数编译成.a静态库文件,并置于../lib/目录下。
我目前写的makefile为:
myMaths.a : myMaths.o
ar rc myMaths.a myMaths.o
rm myMaths.o
myMaths.o : myMaths.c
g++ -o myMaths.o -c myMaths.c -I ../headers
clean:
rm *.o
上述makefiel可以运行,并生成.a文件,
但我不希望写上述的“-I ../headers”,
据说通过VPATH可以设置查找路径,
但由于不会用,发现不起作用(不写-I,就找不到头文件)。
所以,请教各位,
该如何写在myMahts目录下的makefiel文件,
使其能够自动查找../headers目录下的头文件,并生成相应.a文件。
或者,还有什么更好的方法实现上述的需求?
谢谢!!
headers(用于统一存放.a静态库的头文件,以便于其它程序调用.a文件时,统一到该目录下寻找相应的头文件);
lib(用于统一存放.a静态库文件,其它程序到该目录下寻找库文件,调用相应的方法);
myMaths(数学功能的.c文件,编译后生成.a静态库(置于上面lib目录下));
operations(应用程序目录,调用上述的数学功能库)。
-----------------------
在headers目录下,有myMaths.h头文件。
其内容为:
int myIntAdd(int a, int b);
int myIntMinus(int a, int b);
在myMaths目录下,有myMaths.c实现文件。
其内容为:
#include "myMaths.h" //希望引用上述../headers/目录下的myMaths.h头文件
int myIntAdd(int a, int b)
{
return a+b;
}
int myIntMinus(int a, int b)
{
return a-b;
}
--------------------------------------------
现在想在myMaths目录下,写一个makefile文件,
将myMahts.c中的两个功能函数编译成.a静态库文件,并置于../lib/目录下。
我目前写的makefile为:
myMaths.a : myMaths.o
ar rc myMaths.a myMaths.o
rm myMaths.o
myMaths.o : myMaths.c
g++ -o myMaths.o -c myMaths.c -I ../headers
clean:
rm *.o
上述makefiel可以运行,并生成.a文件,
但我不希望写上述的“-I ../headers”,
据说通过VPATH可以设置查找路径,
但由于不会用,发现不起作用(不写-I,就找不到头文件)。
所以,请教各位,
该如何写在myMahts目录下的makefiel文件,
使其能够自动查找../headers目录下的头文件,并生成相应.a文件。
或者,还有什么更好的方法实现上述的需求?
谢谢!!
|
你再改改, 感觉这个不错.
#http://hi.baidu.com/loganx/blog/item/402f1a37e0c5f7380a55a995.html
#=======================================================================================
#
# Filename: Makefile
# Description:
#
# Usage: make (generate executable )
# make clean (remove objects, executable, prerequisits )
# make tarball (generate compressed archive )
# make zip (generate compressed archive )
#
# Version: 1.0
# Created:
# Revision: ---
#
# Author:
# Company:
# Email:
#
# Notes: C extension : c
# C++ extensions : cc cpp C
# C and C++ sources can be mixed.
# Prerequisites are generated automatically; makedepend is not
# needed (see documentation for GNU make Version 3.80, July 2002,
# section 4.13). The utility sed is used.
#
#============================================== makefile template version 1.6 ==========
# ------------ name of the executable ------------------------------------------------
EXECUTABLE = test
# ------------ list of all source files ----------------------------------------------
SOURCES = test.cpp
# ------------ compiler --------------------------------------------------------------
CC = gcc
CXX = g++
# ------------ compiler flags --------------------------------------------------------
CFLAGS = -Wall -O0 -g # Do not optimize. Produce debugging information.
# ------------ linker-Flags ----------------------------------------------------------
LFLAGS = -g
# ------------ additional system include directories ---------------------------------
GLOBAL_INC_DIR =
# ------------ private include directories -------------------------------------------
#LOCAL_INC_DIR = $(HOME)/include
LOCAL_INC_DIR = $(HOME)/doc/C.Language/newmanlib
# ------------ system libraries (e.g. -lm ) -----------------------------------------
SYS_LIBS = -lm
# ------------ additional system library directories ---------------------------------
GLOBAL_LIB_DIR =
# ------------ additional system libraries -------------------------------------------
GLOBAL_LIBS =
# ------------ private library directories -------------------------------------------
LOCAL_LIB_DIR = $(HOME)/lib
# ------------ private libraries (e.g. libxyz.a ) -----------------------------------
LOCAL_LIBS =
# ------------ archive generation -----------------------------------------------------
TARBALL_EXCLUDE = *.{o,gz,zip}
ZIP_EXCLUDE = *.{o,gz,zip}
# ------------ run executable out of this Makefile (yes/no) -------------------------
# ------------ cmd line parameters for this executable -------------------------------
EXE_START = no
EXE_CMDLINE =
#=======================================================================================
# The following statements usually need not to be changed
#=======================================================================================
C_SOURCES = $(filter %.c, $(SOURCES))
CPP_SOURCES = $(filter-out %.c, $(SOURCES))
ALL_INC_DIR = $(addprefix -I, $(LOCAL_INC_DIR) $(GLOBAL_INC_DIR))
ALL_LIB_DIR = $(addprefix -L, $(LOCAL_LIB_DIR) $(GLOBAL_LIB_DIR))
GLOBAL_LIBSS = $(addprefix $(GLOBAL_LIB_DIR)/, $(GLOBAL_LIBS))
LOCAL_LIBSS = $(addprefix $(LOCAL_LIB_DIR)/, $(LOCAL_LIBS))
ALL_CFLAGS = $(CFLAGS) $(ALL_INC_DIR)
ALL_LFLAGS = $(LFLAGS) $(ALL_LIB_DIR)
BASENAMES = $(basename $(SOURCES))
# ------------ generate the names of the object files --------------------------------
OBJECTS = $(addsuffix .o,$(BASENAMES))
# ------------ generate the names of the hidden prerequisite files -------------------
PREREQUISITES = $(addprefix .,$(addsuffix .d,$(BASENAMES)))
# ------------ make the executable ---------------------------------------------------
$(EXECUTABLE): $(OBJECTS)
ifeq ($(strip $(CPP_SOURCES)),)
$(CC) $(ALL_LFLAGS) -o $(EXECUTABLE) $(OBJECTS) $(LOCAL_LIBSS) $(GLOBAL_LIBSS) $(SYS_LIBS)
else
$(CXX) $(ALL_LFLAGS) -o $(EXECUTABLE) $(OBJECTS) $(LOCAL_LIBSS) $(GLOBAL_LIBSS) $(SYS_LIBS)
endif
ifeq ($(EXE_START),yes)
./$(EXECUTABLE) $(EXE_CMDLINE)
endif
# ------------ include the automatically generated prerequisites ---------------------
# ------------ if target is not clean, tarball or zip ---------------------
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),tarball)
ifneq ($(MAKECMDGOALS),zip)
include $(PREREQUISITES)
endif
endif
endif
# ------------ make the objects ------------------------------------------------------
%.o:%.c
$(CC) -c $(ALL_CFLAGS) $
#http://hi.baidu.com/loganx/blog/item/402f1a37e0c5f7380a55a995.html
#=======================================================================================
#
# Filename: Makefile
# Description:
#
# Usage: make (generate executable )
# make clean (remove objects, executable, prerequisits )
# make tarball (generate compressed archive )
# make zip (generate compressed archive )
#
# Version: 1.0
# Created:
# Revision: ---
#
# Author:
# Company:
# Email:
#
# Notes: C extension : c
# C++ extensions : cc cpp C
# C and C++ sources can be mixed.
# Prerequisites are generated automatically; makedepend is not
# needed (see documentation for GNU make Version 3.80, July 2002,
# section 4.13). The utility sed is used.
#
#============================================== makefile template version 1.6 ==========
# ------------ name of the executable ------------------------------------------------
EXECUTABLE = test
# ------------ list of all source files ----------------------------------------------
SOURCES = test.cpp
# ------------ compiler --------------------------------------------------------------
CC = gcc
CXX = g++
# ------------ compiler flags --------------------------------------------------------
CFLAGS = -Wall -O0 -g # Do not optimize. Produce debugging information.
# ------------ linker-Flags ----------------------------------------------------------
LFLAGS = -g
# ------------ additional system include directories ---------------------------------
GLOBAL_INC_DIR =
# ------------ private include directories -------------------------------------------
#LOCAL_INC_DIR = $(HOME)/include
LOCAL_INC_DIR = $(HOME)/doc/C.Language/newmanlib
# ------------ system libraries (e.g. -lm ) -----------------------------------------
SYS_LIBS = -lm
# ------------ additional system library directories ---------------------------------
GLOBAL_LIB_DIR =
# ------------ additional system libraries -------------------------------------------
GLOBAL_LIBS =
# ------------ private library directories -------------------------------------------
LOCAL_LIB_DIR = $(HOME)/lib
# ------------ private libraries (e.g. libxyz.a ) -----------------------------------
LOCAL_LIBS =
# ------------ archive generation -----------------------------------------------------
TARBALL_EXCLUDE = *.{o,gz,zip}
ZIP_EXCLUDE = *.{o,gz,zip}
# ------------ run executable out of this Makefile (yes/no) -------------------------
# ------------ cmd line parameters for this executable -------------------------------
EXE_START = no
EXE_CMDLINE =
#=======================================================================================
# The following statements usually need not to be changed
#=======================================================================================
C_SOURCES = $(filter %.c, $(SOURCES))
CPP_SOURCES = $(filter-out %.c, $(SOURCES))
ALL_INC_DIR = $(addprefix -I, $(LOCAL_INC_DIR) $(GLOBAL_INC_DIR))
ALL_LIB_DIR = $(addprefix -L, $(LOCAL_LIB_DIR) $(GLOBAL_LIB_DIR))
GLOBAL_LIBSS = $(addprefix $(GLOBAL_LIB_DIR)/, $(GLOBAL_LIBS))
LOCAL_LIBSS = $(addprefix $(LOCAL_LIB_DIR)/, $(LOCAL_LIBS))
ALL_CFLAGS = $(CFLAGS) $(ALL_INC_DIR)
ALL_LFLAGS = $(LFLAGS) $(ALL_LIB_DIR)
BASENAMES = $(basename $(SOURCES))
# ------------ generate the names of the object files --------------------------------
OBJECTS = $(addsuffix .o,$(BASENAMES))
# ------------ generate the names of the hidden prerequisite files -------------------
PREREQUISITES = $(addprefix .,$(addsuffix .d,$(BASENAMES)))
# ------------ make the executable ---------------------------------------------------
$(EXECUTABLE): $(OBJECTS)
ifeq ($(strip $(CPP_SOURCES)),)
$(CC) $(ALL_LFLAGS) -o $(EXECUTABLE) $(OBJECTS) $(LOCAL_LIBSS) $(GLOBAL_LIBSS) $(SYS_LIBS)
else
$(CXX) $(ALL_LFLAGS) -o $(EXECUTABLE) $(OBJECTS) $(LOCAL_LIBSS) $(GLOBAL_LIBSS) $(SYS_LIBS)
endif
ifeq ($(EXE_START),yes)
./$(EXECUTABLE) $(EXE_CMDLINE)
endif
# ------------ include the automatically generated prerequisites ---------------------
# ------------ if target is not clean, tarball or zip ---------------------
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),tarball)
ifneq ($(MAKECMDGOALS),zip)
include $(PREREQUISITES)
endif
endif
endif
# ------------ make the objects ------------------------------------------------------
%.o:%.c
$(CC) -c $(ALL_CFLAGS) $