1

My project architecture :

project_folder/
 |--bin/
 |--src/
 |    |-----module1/
 |    |      |----> module1.c
 |    |      |----> module1.h
 |    |      |----> module1.o
 |    |      |----> makefile
 |    |-----module2/
 |    |      |----> module2.c
 |    |      |----> module2.h
 |    |      |----> module2.o
 |    |      |----> makefile
 |-- makefile
 |-- main.c

When I make my project It correctly builds the modules of my project and then when I want to compile the whole project I have undefined reference to modules' functions.

I think the error comes from my CC command in my head makefile so here is my makefile :

export CC = gcc

PACKAGES = module1 module2

MAIN = main.c

EXEC = bin

all: $(PACKAGES) $(EXEC)

$(PACKAGES):
    @$(MAKE) $(MAKECMDGOALS) -C $@

$(EXEC): $(MAIN)
    $(CC) $(MAIN) -o $(EXEC)

.PHONY: all clean $(PACKAGES)

clean: $(PACKAGES)

Error is :

/usr/bin/ld: /project_folder/main.c:line undefined reference to `module1_fct1`

Do y have any advice ?

Clément
  • 53
  • 6
  • Please show the actual error message. But in any case, your `$(CC) $(MAIN) -o $(EXEC)` command needs to also be passed all the `.o` files of the modules. You are not doing that. – user17732522 Jun 20 '22 at 10:46
  • Does [this answer](https://stackoverflow.com/a/5559274/1256234) help? In the rule to build `$(EXEC)`, you also need to add the source file where `module1_fct1` is defined. Adding a function declaration in a header file is not enough. – Andy J Jun 20 '22 at 11:00
  • Yes but I want to recursively add the sources – Clément Jun 20 '22 at 11:03
  • 1
    You have to list `module1/module1.o` and `module2/module2.o` as dependencies of `$(EXEC)` and include them in the compiler/linker command arguments. You also need to ensure there are rules to make them. Your modules rules wont help much; the directories exist so they will never need to be executed. – Jonathan Leffler Jun 20 '22 at 21:30

0 Answers0