2010-09-19 85 views
1

我almst總項目包括:foo.h中的gcc的簡易的makefile/G ++

  1. 對和Foo.cpp中

  2. 一些額外的頭util.h等

寫一個makefile的最簡單方法是什麼

  • 奔跑

    $CC -c foo.cpp 
    

每個.cpp文件,保持依賴於它的coresponding .h文件中

  • 提供某種方式,我可以手動添加額外的依賴
  • 包括一個鏈接步驟我的manuall設置$ LIBS變量。

我使用Linux(Ubuntu的)和gcc /克++工作。

回答

1

如何:

%.o: %.cpp %.h 
    $(CC) -c $< -o [email protected] 

# Some things have extra dependencies. (Headers like util.h are unlikely 
# to change, but you can handle them this way if you really want to.) 
# 
# foo.o and bar.o both depend on baz.h 
foo.o bar.o: baz.h 

# foo.o also depends on gab.h and jig.h 
foo.o: gab.h jig.h 

# You will need a list of object files. You can build it by hand: 
OBJ_FILES = foo.o bar.o snaz.o # and so on 

# ...or just grab all the files in the source directory: 
SOURCE_FILES = $(wildcard *.cpp) 
OBJ_FILES = $(SOURCE_FILES:.cpp=.o) 

# It is possible to get this from the environment, but not advisable. 
LIBS = -lred -lblue 

final-thing: $(OBJ_FILES) 
    $(CC) $(LIBS) $^ -o [email protected] 
+0

玩得開心手動維護這些依賴關係。 – 2010-09-20 12:23:49

+1

@Jack Kelly:是的,我熟悉像mad-scientist.net/make/autodep.html這樣的更高級的方法,但請看這個問題。 – Beta 2010-09-20 16:01:57

+0

啊,是的,它確實要求手動管理代價。我的錯。 – 2010-09-20 20:58:56

0

這裏是我的一個項目的例子 - 你可以簡單地在那裏丟棄新對foo1.ccfoo1.h,他們會自動地爲您構建:

# determine all sources and from that all targets 
sources :=    $(wildcard *.cpp) 
programs :=    $(sources:.cpp=) 

## compiler etc settings used in default make rules 
CXX :=     g++ 
CPPFLAGS :=    -Wall 
CXXFLAGS :=    -O3 -pipe 
LDLIBS := 

# build all and strip programs afterwards 
all:     $(programs) 
         @test -x /usr/bin/strip && strip $^ 
+2

在不依賴頭文件的情況下,有些情況下在更改後不會重建。通常的'.depend'配方會修復它。多個SO問題的地址,包括http://stackoverflow.com/questions/297514/how-can-i-have-a-makefile-automatically-rebuild-source-files-that-include-a-modif。 – dmckee 2010-09-19 20:14:28

+0

在編譯步驟中建立'strip'似乎是一個非常糟糕的主意,特別是對於開發。 GNU makefile約定會將其剝離到自己的目標'install-strip'中:http://www.gnu.org/prep/standards/standards.html#Makefile-Conventions – 2010-09-20 03:39:24

+0

正如我所說,它來自本地項目 - - 我認爲這是合適的。 – 2010-09-20 11:40:12

2

也許你可以看看CMake

如果你不熟悉CMake,它基本上是一個Makefile生成器(或XCode或Visual Studio Projects等,取決於平臺),所以它可以讓你指定你需要的變量,並照顧到頭依賴你的問題,生成文件生成等

3

請,只要使用automake。你會得到適當的依賴追蹤,隨GNU Makefile Standards(例如,make install做正確的事,尊重DESTDIRprefix),根據需要檢查系統怪癖的能力,並建立適當的分發壓縮包支持符合makefile文件。

這是一個最小的configure.ac

           -*- Autoconf -*- 
# Process this file with autoconf to produce a configure script. 

AC_PREREQ([2.61]) 
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS]) 
AM_INIT_AUTOMAKE([foreign]) 

# Checks for programs. 
AC_PROG_CXX 

# Checks for libraries. 

# Checks for header files. 

# Checks for typedefs, structures, and compiler characteristics. 

# Checks for library functions. 

AC_CONFIG_FILES([Makefile]) 
AC_OUTPUT 

和最小Makefile.am

## Process this file with automake to generate Makefile.in 
bin_PROGRAMS = foo 
foo_SOURCES = foo.cpp bar.h baz.h quux.cpp 

運行autoreconf -i生成配置腳本,然後./configuremake

這裏是一個excellent autotools tutorial

1

下面是一個簡單的shell腳本,構造一個makefile文件所有cpp文件在指定目錄:

# !sh  
if [ $# = 0 ] 
then 
echo -e "please give executable name" 
exit 1 
fi 

echo -e -n "CC=g++\nOPTIMS=\nLIBS= " > makefile 

echo >> makefile 
echo -n "$1: " >> makefile 
for fic in *.cpp 
do 
echo -n "${fic%\.cpp}.o " >> makefile 
done 

echo >> makefile 
echo -n -e "\t\$(CC) " >> makefile 
for fic in *.cpp 
do 
echo -n "${fic%\.cpp}.o " >> makefile 
done 
echo -n -e "-o $1 \$(OPTIMS) \$(LIBS)\n" >> makefile 

echo >> makefile 
for fic in *.cpp 
do 
g++ -MM $fic >> makefile 
echo -e "\t\$(CC) -c $fic \$(OPTIMS)\n" >> makefile 
done 

exit 0 

它使用創建的makefile依賴行的-MM選項的gcc。剛創建的源文件夾下的,(讓我們稱之爲micmake),使其可執行(chmod +x micmake),然後鍵入

./micmake go 

這將創建一個makefile和make命令編譯您的項目。該可執行文件名爲go。如果需要特殊的編譯選項或庫,可以編輯makefile。對於更復雜的項目和依賴項,您應該使用automake,cmake或scons。