0

I have a question about makefiles used in linux to run c files.

i wrote the following makefile, the purpose is to build the tests regarding the adt's of the program (space,set,arrangement,and plane) and well as to check another program called mtm_space (the first bloc after defining strings)

CC=gcc
OBJS=plane.o set.o space.o main. decoding.o arrangement.o
DEBUG=# insert -g
CFLAGS=-std=c99 -Wall -Werror $(DEBUG) -pedantic-errors/
-DNDEBUG -L. lmtm2

space : $(OBJS) 
    $(CC) $(CFLAGS) -o $@ $^

arrangement.o: arrangement.c arrangement.h set.h plane.h
decoding.o: decoding.c space.h list.h arrangement.h set.h plane.h \
mtm_ex2.h
plane.o: plane.c plane.h
main.o: main.c space.h list.h arrangement.h set.h plane.h mtm_ex2.h \
decoding.h
set.o: set.c set.h
space.o: space.c space.h list.h arrangement.h set.h plane.h
mtm_space.o: mtmspace.c arrangement.h plane.h list.h set.h space.h mtm_ex2.h/
decoding.h

OBJS1=$(OBJS) space_test.o 

space_test : $(OBJS1)
    $(CC)  $(CFLAGS) -o $@ $^

arrangement.o: arrangement.c arrangement.h set.h plane.h
main.o: main.c space.h list.h arrangement.h set.h plane.h mtm_ex2.h \
 decoding.h
plane.o: plane.c plane.h
set.o: set.c set.h
space.o: space.c space.h list.h arrangement.h set.h plane.h
space_test.o: ./tests/space_test.c space.h list.h arrangement.h set.h plane.h

OBJS2= plane.o set.o

plane_test :$(OBJS2)
    $(CC)  $(CFLAGS) -o $@ $^

plane.o: plane.c plane.h
set.o: set.c set.h
plane_test.o: ./tests/plane_test.c set.h plane.h

OBJS3= set.o

set_test :$(OBJS3)
    $(CC)  $(CFLAGS) -o $@ $^

set.o:set.c set.h
set_test.o: ./tests/set_test.c set.h

OBJS4= plane.o set.o arrangement.o

arrangement_test :$(OBJS4)
    $(CC)  $(CFLAGS) -o $@ $^

arrangement.o: arrangement.c arrangement.h set.h plane.h
plane.o: plane.c plane.h
set.o: set.c set.h
arrangement_test.o: ./tests/arrangement_test.c arrangement.h set.h plane.h

but when for instance i try to use

 >make space_test

it returns many errors that all look like this

space_test.c:(.text+0x5fc): undefined reference to `spaceCreate'

for all of the different functions i've used.

Can anyone help me figure out why this is happening? the programs all run ok on eclipse Thanks a lot

Vicky
  • 12,934
  • 4
  • 46
  • 54
  • 2
    Related: http://stackoverflow.com/questions/45135/linker-order-gcc – anatolyg Aug 27 '13 at 20:08
  • 2
    First: Is this your real makefile? You have several syntax errors in it, for example a / instead of a \ in the dependencies for mtm_space.o and the CFLAGS line, a missing `o` on the end of `main.` in your first OBJS definition, etc. Second: for the specific error you mention, you are not including a .o file or other library containing the definition of spaceCreate and whatever other functions you use. If you have source code for the definitions of these functions, you will need to build it and link against the results; otherwise you need to link against a library that provides these. – Vicky Aug 27 '13 at 20:12
  • Why not always use `-g`? It has no downside (except diskspace). I don't think you want that `/` after `-pedantic-errors` and you're missing `-` before `lmtm2`. Why are you defining the prerequisites for `plane.o` and `set.o` multiple times? – Jonathan Wakely Aug 27 '13 at 20:13
  • Thanks a lot everyone! this is my first makefile and i really appreciate your help. @Vicky about the syntax errors i didn't know that they even existed- but that's what you get when there is no compiler to tell you. if I am in the wrong place (like this is an advanced programmers forum) then let me know – Noam Fisher Aug 28 '13 at 06:51

0 Answers0