VPATH not seems to be working

  • Thread starter Thread starter olaoni
  • Start date Start date
O

olaoni

To Whom It May Concern,

I am trying to learn the usage of VPATH in a makefile, but does not seems to be working for me. I am sure I am doing something wrong and hoping that someone would point this out to me

I have the following makefile named Makefile

################MAKEFILE##################
#INCLUDE = /path/to/the/general.h
INCLUDE = ./inc

VPATH = ./src:./inc

obj=$(patsubst %.c,%.o,$(wildcard *.c))

main: $(obj) general.h
gcc -g -o $@ $^ -I$(INCLUDE)

%.o: %.c
gcc -g -c -o $@ $< -I$(INCLUDE)

clean:
rm -r *.o main

######### END OF MAKEFILE #####################

############ main.c #######################

#include <stdio.h>
#include <general.h>

int main(int argc, char** argv)
{
print_foo();
print_bar();

return 0;
}

############ end main.c #######################.

########### bar.c ######################

#include <general.h>

void print_bar(void)
{
printf("Hello bar.\n");
}

######### end of bar.c ###############

############# foo.c ###############
#include <general.h>

void print_foo(void)
{
printf("Hello foo.\n");
}

############ end of foo.c ############

########### general.h ####################
#ifndef GENERAL_H
#define GENERAL_H

#include <stdio.h>

void print_foo(void);
void print_bar(void);

#endif

########### end of general.h ##############

Below is the tree directory structure in the file system

Code:
.
├── inc
│ └── general.h
├── main.c
├── main.o
├── Makefile
└── src
├── bar.c
└── foo.c
My understanding that make will search the path assigned to the VPATH variables hence "src" and "inc" in this case but when I build it get the following linker error.

############# OUTPUT ################
gcc -g -o main main.o -I./inc
main.o: In function `main':
/home/olaoni/devl/make-tutor/main.c:5: undefined reference to `print_foo'
/home/olaoni/devl/make-tutor/main.c:6: undefined reference to `print_bar'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
#####################################

Many thanks in advance.

Regards

onio

Continue reading...
 
Back
Top