萬能的Makefile檔案

  據作者說是萬能的Makefile檔案,好像作者是這麼說的:「我還沒有發現當給出一個源碼檔案的目錄,這個 makefile 會失敗的情況,除非依存檔案被弄亂。如果這種弄亂的情況發生了,只要輸入 `make clean' ,所有的目標檔案和依存檔案會被刪除,問題就應該被解決了。當然,最好不要把它們弄亂。如果你發現在某種情況下這 個 makefile 檔案不能完成它的工作,請告訴我,我會把它整好的。」

  我還沒驗證個這個makefile到底是否如作者說的那樣,要是真是的話到省了不少麻煩.改天有空一定試試.



────────────────────────────────
# $Id: Makefile,v 2.0 2008/10/08 18:55:40 Update $


prog_name = MyProgam

######################################
#
# Generic makefile
#
# by Jackie Xie
# email: jackie.CPlusPlus@gmail.com
#
# Copyright (c) 2008 Jackie Xie
# All rights reserved.
#
# No warranty, no liability;
# you use this at your own risk.
#
# You are free to modify and
# distribute this without giving
# credit to the original author.
#
######################################
### Customising
#
# Adjust the following if necessary; EXECUTABLE is the target
# executable's filename, and LIBS is a list of libraries to link in
# (e.g. alleg, stdcx, iostr, etc). You can override these on make's
# command line of course, if you prefer to do it that way.
#

EXECUTABLE := $(prog_name)
LIBS :=

# Now alter any implicit rules' variables if you like, e.g.:
#

CFLAGS := -g -Wall -O3
CXXFLAGS := $(CFLAGS)
CC := g++

# The next bit checks to see whether rm is in your djgpp bin
# directory; if not it uses del instead, but this can cause (harmless)
# `File not found' error messages. If you are not using DOS at all,
# set the variable to something which will unquestioningly remove
# files.
#

ifneq ($(wildcard $(DJDIR)/bin/rm),)
RM-F := rm -f
else
RM-F := del
endif

# You shouldn't need to change anything below this point.
#
# 從這裡開始,你應該不需要改動任何東西。

SOURCE := $(wildcard *.c) $(wildcard *.cc)
OBJS := $(patsubst %.c,%.o,$(patsubst %.cc,%.o,$(SOURCE)))
DEPS := $(patsubst %.o,%.d,$(OBJS))
MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS))
MISSING_DEPS_SOURCES := $(wildcard $(patsubst %.d,%.c,$(MISSING_DEPS)) $(patsubst %.d,%.cc,$(MISSING_DEPS)))
CPPFLAGS += -MD

.PHONY : everything deps objs clean veryclean rebuild

everything : $(EXECUTABLE)

deps : $(DEPS)

objs : $(OBJS)

clean :
    @$(RM-F) *.o
    @$(RM-F) *.d

veryclean: clean
    @$(RM-F) $(EXECUTABLE) *.*~ *~
    
@$(RM-F) $$(file `find .` | grep "bit executable" | awk '{print $$1}' | sed 's/\.*\://')

distclean: veryclean

rebuild: veryclean everything
ifneq ($(MISSING_DEPS),)
$(MISSING_DEPS) :
    @$(RM-F) $(patsubst %.d,%.o,$@)
endif
-include $(DEPS)

$(EXECUTABLE) : $(OBJS)
    $(CC) -o $(EXECUTABLE) $(OBJS) $(addprefix -l,$(LIBS))

 

下面是一個C程式引用C++程式的例子:

/* header_c.h */

// $Id: header_c.h,v 1.2 2001/03/05 10:20:02 fusz Exp $

#ifdef __cplusplus
extern "C" {
#endif

void cpp_function( void );
   
#ifdef __cplusplus
}
#endif

 

/* header_cc.h */

#include <iostream>
#include "header_c.h"

using namespace std;

#ifdef __cplusplus
extern "C" {
#endif

class foo
{

 public:
    int i;

    foo() {i=0; }
    ~foo() {}

    void see()
        {
            cout << "---------I am inside class ------------" << endl;
        }
};

#ifdef __cplusplus
}
#endif


/* main.c */

// $Id: main.c,v 1.2 2001/03/05 10:20:17 fusz Exp $
//# g++ -o cinvokecc foo.cc main.c

#include "header_c.h"

int main( void )
{
    cpp_function();
    return 0;
}

 

/* foo.cc */

// $Id: foo.cc,v 1.1 2001/03/05 10:13:15 fusz Exp $

#include "header_cc.h"

#include <iostream>
using namespace std;

#ifdef __cplusplus
extern "C" {
#endif

void cpp_function( void )
{
  foo f;

  //--------------------------------------------------
 
  f.see();
  cout<<"Hi, I am in Cpp file and called by C file\n"<<flush;
}

#ifdef __cplusplus
}
#endif

 

/* Makefile */

# $Id: Makefile,v 2.0 2008/10/08 18:55:40 fusz Exp fusz $

# make
# make $prog_name
# make $EXECUTABLE

prog_name = cinvokecc

######################################
#
# Generic makefile
#
# by Jackie Xie
# email: jackie.CPlusPlus@gmail.com
#
# Copyright (c) 2008 Jackie Xie
# All rights reserved.
#
# No warranty, no liability;
# you use this at your own risk.
#
# You are free to modify and
# distribute this without giving
# credit to the original author.
#
######################################
### Customising
#
# Adjust the following if necessary; EXECUTABLE is the target
# executable's filename, and LIBS is a list of libraries to link in
# (e.g. alleg, stdcx, iostr, etc). You can override these on make's
# command line of course, if you prefer to do it that way.
#
EXECUTABLE := $(prog_name)
LIBS :=


# Now alter any implicit rules' variables if you like, e.g.:
#
CFLAGS := -g -Wall -O3
CXXFLAGS := $(CFLAGS)
CC := g++


# The next bit checks to see whether rm is in your djgpp bin
# directory; if not it uses del instead, but this can cause (harmless)
# `File not found' error messages. If you are not using DOS at all,
# set the variable to something which will unquestioningly remove
# files.
#
ifneq ($(wildcard $(DJDIR)/bin/rm),)
RM-F := rm -f
else
RM-F := del
endif


# You shouldn't need to change anything below this point.
#
# 從這裡開始,你應該不需要改動任何東西
SOURCE := $(wildcard *.c) $(wildcard *.cc) $(wildcard *.cpp) $(wildcard *.cxx)
#OBJS := $(patsubst %.c,%.o,$(patsubst %.cc,%.o,$(SOURCE)))
OBJS := $(patsubst %.c,%.o,$(patsubst %.cc,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))))
DEPS := $(patsubst %.o,%.d,$(OBJS))
MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS))
MISSING_DEPS_SOURCES := $(wildcard $(patsubst %.d,%.c,$(MISSING_DEPS)) $(patsubst %.d,%.cc,$(MISSING_DEPS)))
CPPFLAGS += -MD

.PHONY : all everything deps objs clean veryclean rebuild

all:
    $(CC) -o $(EXECUTABLE) $(SOURCE)

everything : $(EXECUTABLE)

deps : $(DEPS)

objs : $(OBJS)

clean :
    @$(RM-F) *.o
    @$(RM-F) *.d

veryclean: clean
    @$(RM-F) $(EXECUTABLE) *.*~ *~
    @$(RM-F) $$(file `find .` | grep "bit executable" | awk '{print $$1}' | sed 's/\.*\://')

distclean: veryclean

rebuild: veryclean everything
ifneq ($(MISSING_DEPS),)
$(MISSING_DEPS) :
    @$(RM-F) $(patsubst %.d,%.o,$@)
endif
-include $(DEPS)


$(EXECUTABLE) : $(OBJS)
    $(CC) -o $(EXECUTABLE) $(OBJS) $(addprefix -l,$(LIBS))

 

arrow
arrow
    文章標籤
    Makefile
    全站熱搜

    Bluelove1968 發表在 痞客邦 留言(0) 人氣()