请教:这个makefile的写法

LeonTown 2010-08-13 10:02:36
在工程目录(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文件。

或者,还有什么更好的方法实现上述的需求?

谢谢!!
...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
LeonTown 2010-08-13
  • 打赏
  • 举报
回复
4楼给的makefile看不懂啊。。。

之前用的VS,可视化的设置比较方便。

简单看4楼的代码,似乎也有VS下设置查找路径的风格。。。
newman0708 2010-08-13
  • 打赏
  • 举报
回复
你再改改, 感觉这个不错.


#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) $<
%.o:%.cc
$(CXX) -c $(ALL_CFLAGS) $<
%.o:%.cpp
$(CXX) -c $(ALL_CFLAGS) $<
%.o:%.C
$(CXX) -c $(ALL_CFLAGS) $<

# ------------ make the prerequisites ------------------------------------------------
#
.%.d:%.c
@$(make-prerequisite-c)
.%.d:%.cc
@$(make-prerequisite-cplusplus)
.%.d:%.cpp
@$(make-prerequisite-cplusplus)
.%.d:%.C
@$(make-prerequisite-cplusplus)

# canned command sequences
# echoing of the sed command is suppressed by the leading @

define make-prerequisite-c
@$(CC) -MM $(ALL_CFLAGS) $< > $@.$$$$; \
sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' < $@.$$$$ > $@; \
rm -f $@.$$$$;
endef

define make-prerequisite-cplusplus
@$(CXX) -MM $(ALL_CFLAGS) $< > $@.$$$$; \
sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' < $@.$$$$ > $@; \
rm -f $@.$$$$;
endef

# ------------ remove generated files ------------------------------------------------
# ------------ remove hidden backup files --------------------------------------------
clean:
rm --force $(EXECUTABLE) $(OBJECTS) $(PREREQUISITES) *~

# ------------ tarball generation ------------------------------------------------------
tarball:
@lokaldir=`pwd`; lokaldir=$${lokaldir##*/}; \
rm --force $$lokaldir.tar.gz; \
tar --exclude=$(TARBALL_EXCLUDE) \
--create \
--gzip \
--verbose \
--file $$lokaldir.tar.gz *

# ------------ zip ---------------------------------------------------------------------
zip:
@lokaldir=`pwd`; lokaldir=$${lokaldir##*/}; \
zip -r $$lokaldir.zip * -x $(ZIP_EXCLUDE)

# ======================================================================================
# vim: set tabstop=2: set shiftwidth=2:
LeonTown 2010-08-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ecsape 的回复:]
可以在c文件里#include "../headers/file.h"
[/Quote]

也不想这样这样。

就是想统一设置几个查找路径,
让程序到这些路径下去查找文件。。
ecsape 2010-08-13
  • 打赏
  • 举报
回复
可以在c文件里#include "../headers/file.h"

23,124

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧