I have the following problem, assuming my project structure is the following ( /
is the root).
/bin # Directorio vacío, es la salida del makefile.
/include/*.h # assembler.h core.h utils.h, Todos los headers estan aquí.
/src/assembler/*.c # assembler.c tokenizer.c y otras herramientas del ensamblador.
/src/core/*.c # utils.c core.c y todos los ficheros del núcleo compartido.
/src/machine/*.c # bla.c foo.c .... Todos los ficheros de la máquina virtual.
All projects under /src
have the project as a dependency core
, but have access to the directory headers/include
Using GNU make
I need to be able to compile only one of the projects I am working on, without the need to alter other projects, so far I have used the following Makefile
:
# #$@% Makefile
CC=gcc
FLAGS=-Wall -Wextra -Wpedantic -Werror -std=c11 -O3
INCLUDES=-Iinclude
checkbin:
# Ensure bin is there.
@if [ ! -e "bin" ]; then mkdir "bin"; fi
assembler: checkbin # First check for binaries folder.
$(CC) -o bin/assembler $(INCLUDES) $(FLAGS) src/core/*.c src/assembler/*.c
vm: checkbin
$(CC) -o bin/vm $(INCLUDES) $(FLAGS) src/core/*.c src/machine/*.c
clean:
@if [ -e "bin" ]; then \
rm -rf "bin"; \
fi
default: assembler # set assembler to default...
He Makefile
actually does the work, but he does a lot of work; it is recompiling the core
every time i go to compile some other project like machine
or assembler
.
To compile them separately I know I have to use the flag -c
, gcc
but when inputting multiple files with this flag, it doesn't allow me to use the flag -o
to specify the output, for example:
$(CC) -c $(FLAGS) $(INCLUDES) /src/core/*.c
# Si intento poner -o "bin/core" en este caso, me arroja error
How can I move the compiled files of the dependency project , for this case, core
into their respective directory /bin/core
so that it can be used later to compile any other project?
For example:
/bin/core/utils.o
/bin/core/core.o
And binds to the following statement:
# Asumiendo los valores puestos arriba:
assembler: checkbin
@if [ ! -e "bin/assembler" ]; then mkdir "bin/assembler"; fi
# ... <- instrucción que pondrá los .o en el directorio /bin/assembler/*.o
$(CC) -o /bin/assembler/assembler /bin/core/*.o /bin/assembler/*.o
# Sólo necesitamos enlazar los archivos objeto.
The basic solution was as Trauma described in his comment, for this I had to re-structure the project in such a way that it would be as follows:
And
Makefile
it configures it in each project that it needs fromcore
so that it will check if it is compiled and thus copy its object files, otherwise it compiles the project. Staying as follows:When doing , the recipe
make
is executed , which has as a prerequisite a and , in , it is in charge of calling , where it specifies that it is in the directory to its right, with this the dependencies of the project are compiled, that is, .build
dependencies
checkbin
dependencies
make -C ../core
-C
Makefile
core
After that, proceed to copy the object files inside the folder
/core/bin/
to the folder of the project you are working on and link them without problem.