1

For simple program eg, proc.c which has only one source file, it's convenient to compile and link using just make proc, which by default search for proc.c and create proc as output file. There is no need to create a Makefile for that.

If proc.c requires to link with some library such as the math library (defined by <math.h>). Using gcc directly, we can use gcc -c proc.c -o proc -lm. Is there an equivalent make command line option to specify -lm so we can use make command directly without writing a Makefile?


Similar task suggested by Basile Starynkevitch:

Makefile with Multiple Executables

Community
  • 1
  • 1
artm
  • 17,291
  • 6
  • 38
  • 54

1 Answers1

1

Link library with make without makefile

If your source file is prova.c then you can do the following:

$> export LDFLAGS=-lm; make prova

or:

$> export LDLIBS=-lm; make prova    # LDLIBS works on Linux (but LDFLAGS doesn't)

or:

$ make prova LDLIBS=-lm

as Make uses this implicit rule:

%: %.o    # Link object file
    $(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)

Make has several implicit rules, you can see all of them with make -p. For further information see this post.

Community
  • 1
  • 1
terence hill
  • 3,354
  • 18
  • 31