0

My file stacking is as follows

dir1/
    mylib.a
    myheader.h
    file.c
    executable
dir2/
    dependentfile.c // depends on functions in myheader.h implemented in mylib.a

I would like to link my static library file in my Makefile WITHOUT using its name, but just indicating its path. What I have is as follows:

Makefile for dir2/

CC = gcc
CFLAGS   = -g -Wall $(INCLUDES)
INCLUDES = -I../dir1
LDFLAGS = -g -L../dir1

exec: dependentfile.o

dependentfile.o: dependentfile.c

Running 'make' just gives me a bunch of implicit declaration errors, and undefined references because it's not looking in the paths I have specified with -L and -I. My dependentfile.c also has the line

#include "myheader.h"

which it can't find.

How should I modify the makefile in order to make this work? I have tried multiple things, even specifying the lib file with -l and writing the complete path to no avail.

Any help is appreciated!

#

EDIT: Figured it out!

Turns out I was forgetting LDLIBS. So for everyone else, the makefile ended up looking like:

CC = gcc
CFLAGS   = -g -Wall $(INCLUDES)
INCLUDES = -I../dir1
LDFLAGS = -g -L../dir1
LDLIBS = -lmylib (my actual file was named libmylib.a, forgo the "lib") 

exec: dependentfile.o

dependentfile.o: dependentfile.c
user2799712
  • 25
  • 1
  • 5
  • is this even possible? – Claudiordgz Mar 01 '14 at 16:58
  • You want to link in a static library, but really don't want to tell make what it is? How do you expect that to work? Make does what you tell it to do. It isn't going to go and find your dependencies for you - you have to tell it what they are. While you updated your question to say that you'd figured it out, you don't mention that you realised your dependence on make's default rulebase. Understanding that fact would go a long way to you understanding what is going on and how to write Makefiles in the future. – James McPherson Mar 04 '14 at 06:04

1 Answers1

0

I think you should use

#include "myheader.h"

Your header file name should be quoted.

Kang Li
  • 606
  • 7
  • 10