0

I wish to convert this statement into a makefile:

gcc -Wall -I/usr/local/include -c my_program.c

I wrote this makefile:

CC=gcc
CFLAGS=-c -Wall -I:/usr/local/include
LDFLAGS= -LE:/usr/local/include
LIBS= -lgsl
SOURCES=my_program.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=my_program
all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@

.c.o:
    $(CC) $(CFLAGS) $< -o $@

However, I have an error which specifies undefined references and shows this:

collect2: error: ld returned 1 exit status
make: *** [my_program] Error 1

In this program, I am using GNU GSL library stored at /usr/local/include

I read various tutorials on makefiles and the man make page and I'm still stuck. Please let me know if you have any suggestions to fix this. Thank you!

Anagha
  • 740
  • 1
  • 10
  • 16
  • 2
    `CFLAGS=-c -Wall -I:/usr/local/include` you probably don't want that colon there. Same for `LDFLAGS`. – Kninnug Aug 07 '15 at 19:51
  • Thanks @Kninnug, did that. Now it says - makefile:14: *** missing separator. Stop. – Anagha Aug 07 '15 at 19:57
  • 2
    Is your makefile indented properly with hard tabs? See [here](http://stackoverflow.com/questions/14109724/makefile-missing-separator) for an alternative with semicolons. – Eugene Sh. Aug 07 '15 at 20:06
  • Please update your question if it gets better based on the comments, so that future readers know what to answer... Or even better, post an answer to your own post with the correct version, and what you did to get there! – holroy Aug 07 '15 at 20:20
  • 2
    It is most likely a missing tabulator, make sure the code is correctly formatted – Michał Szydłowski Aug 07 '15 at 20:21
  • For future reference: when showing errors you should also show a few relevant lines _before_ the error for context. Here this is a message saying that the link line is incorrect, and you show the error message but you don't show the link line that make printed! If you'd shown the command line make printed just before the error the question would have been much easier to answer. – MadScientist Aug 07 '15 at 22:06
  • Note that you do not normally include the `-c` option in `CFLAGS` because you also include `CFLAGS` in the line that links the code. All else apart, if you try to build a debuggable program, the object files need to be created with the `-g` option, and the executable needs to be linked with the `-g` option, so you have to repeat yourself to get a debuggable program. – Jonathan Leffler Aug 07 '15 at 23:33
  • Look at what the makefile is trying. Try options -n, -d, or --trace. – Steve Kolokowsky Aug 08 '15 at 00:16
  • "*makefile:14: *** missing separator. Stop.*" so please what is line 14? *sigh* – alk Aug 08 '15 at 10:38
  • What is this `-LE:`? – alk Aug 08 '15 at 10:43

0 Answers0