Difference between revisions of "Make/CVS example 1"

From Wiki**3

(Implementation)
(Step 2: Building the Makefile)
Line 84: Line 84:
  
 
<tt>.PHONY</tt> indicates targets that do not correspond to actual files and attempts should not be made to build them (e.g. from implicit rules). <tt>all</tt> depends on <tt>depend</tt> to force the Makefile to keep its dependencies updated without explicitly calling "make depend".
 
<tt>.PHONY</tt> indicates targets that do not correspond to actual files and attempts should not be made to build them (e.g. from implicit rules). <tt>all</tt> depends on <tt>depend</tt> to force the Makefile to keep its dependencies updated without explicitly calling "make depend".
 +
 +
Finally, to put the Makefile under CVS control, the following two commands would be needed:
 +
 +
  prompt% cvs add Makefile
 +
  cvs add: use `cvs commit' to add this file permanently
 +
 +
  prompt% cvs commit
 +
  /cvs/factorial/Makefile,v  <--  Makefile
 +
  initial revision: 1.1

Revision as of 17:35, 9 March 2008

The Problem (in Portuguese)

Considere quatro ficheiros recurs.cpp (contendo a função factrecurs), recurs.h (contendo a declaração da função factrecurs), iter.cpp (contendo a função factiter ), iter.h (contendo a declaração da função factiter) e main.cpp (contendo a função main que invoca sequencialmente ambas as funções anteriores e que inclui recurs.h e iter.h).

  1. Construa, com estes cinco ficheiros, um projecto designado factorial, indicando os comandos para o colocar sob o controlo de versões do CVS. Admita que o repositório já existe em /cvs.
  2. Construa uma Makefile para gerar o executável fact, a partir dos ficheiros anteriores. Indique os comandos necessários para colocar a Makefile no repositório CVS.
  3. Considere agora que vai ter dois executáveis, cada um invocando a sua função. Indique as alterações à Makefile para que passe a gerar os executáveis recurs e iter, utilizando ambos o mesmo ficheiro main.cpp (mas diferentes implementações do factorial). Indique as operações do CVS para criar a nova versão do projecto, assumindo que o projecto está checked-out no directório corrente.
  4. Considere uma nova versão do projecto: pretende-se construir um único executável fact, mas a escolha da implementação do factorial é efectuada em tempo de execução. Considere que os ficheiros recurs.cpp e recurs.h existem num subdirectório recurs e, analogamente, existe um directório iter para a outra versão. Cada um dos conjuntos apresenta agora a mesma interface para a função que calcula o factorial: fact. Construa duas bibliotecas dinâmicas, ambas designadas por fact.so, cada uma em seu directório (recurs/fact.so e iter/fact.so). A selecção da variante a utilizar faz-se definindo a variável de ambiente LD_LIBRARY_PATH com o directório apropriado. Indique quais os ficheiros iniciais modificados, face à alínea anterior, a nova Makefile e os comandos CVS necessários para que o repositório passe a conter a nova versão.
  5. Modifique a alínea anterior para que o utilizador possa escolher a variante a utilizar através de um argumento na linha do comando fact, respectivamente recurs ou iter. Indique quais os ficheiros iniciais modificados, a nova Makefile e os comandos CVS necessários para registar a nova versão.

Implementation

The following sections present possible implementations.

All shell commands are prefixed with prompt% (their output is presented for explanation purposes only and is not required to answer the problem).

It is assumed that the CVSROOT variable has been defined: <bash>

 export CVSROOT=/cvs

</bash>

Step 1: Building the Project

Assuming that all files are in directory /some/temporary/directory, the following commands may be used to create the factorial project ().

 prompt% cd /some/temporary/directory
 prompt% cvs import factorial david initial
 N factorial/recurs.cpp
 N factorial/recurs.h
 N factorial/iter.cpp
 N factorial/iter.h
 N factorial/main.cpp

Then, after import is complete, we can check-out a fresh copy of the project and change to that directory:

 prompt% cvs co factorial
 U factorial/iter.cpp
 U factorial/iter.h
 U factorial/main.cpp
 U factorial/recurs.cpp
 U factorial/recurs.h
 prompt% cd factorial

Step 2: Building the Makefile

The following is a simple Makefile for building the executable. <text> fact: main.o iter.o recurs.o

recurs.o: recurs.cpp

       g++ -c recurs.cpp

iter.o: iter.cpp

       g++ -c iter.cpp

main.o: main.cpp iter.h recurs.h

       g++ -c main.cpp

</text>

The following is a slightly more complex Makefile for doing exactly the same thing. The possible advanytage of using this version is that it automates a few error-prone tasks (such as keeping dependencies up to date). <text> .PHONY: all depend

CXXFILES = $(wildcard *.cpp) CXXHEADS = $(wildcard *.h) OFILES = $(CXXFILES:%.cpp=%.o) PROG = fact

%.o: %.cpp

       $(CXX) $(CXXFLAGS) -c $< -o $@

all: depend $(PROG)

$(PROG): $(OFILES)

       $(CXX) -o $@ $^

depend: .makedeps

.makedeps: $(CXXFILES) $(CXXHEADS)

       $(CXX) $(CXXFLAGS) -MM $(CXXFILES) > .makedeps

-include .makedeps </text>

.PHONY indicates targets that do not correspond to actual files and attempts should not be made to build them (e.g. from implicit rules). all depends on depend to force the Makefile to keep its dependencies updated without explicitly calling "make depend".

Finally, to put the Makefile under CVS control, the following two commands would be needed:

 prompt% cvs add Makefile
 cvs add: use `cvs commit' to add this file permanently
 prompt% cvs commit
 /cvs/factorial/Makefile,v  <--  Makefile
 initial revision: 1.1