How to use LDFLAGS in makefile

前端 未结 3 1430
广开言路
广开言路 2020-11-28 04:26

I am new to Linux OS. I am trying to compile a .c file using a makefile. The math library has to be linked. My makefile looks like this:

CC=gcc
         


        
相关标签:
3条回答
  • 2020-11-28 04:48

    Seems like the order of the linking flags was not an issue in older versions of gcc. Eg gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16) comes with Centos-6.7 happy with linker option before inputfile; but gcc with ubuntu 16.04 gcc (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413 does not allow.

    Its not the gcc version alone, I has got something to with the distros

    0 讨论(0)
  • 2020-11-28 05:04

    In more complicated build scenarios, it is common to break compilation into stages, with compilation and assembly happening first (output to object files), and linking object files into a final executable or library afterward--this prevents having to recompile all object files when their source files haven't changed. That's why including the linking flag -lm isn't working when you put it in CFLAGS (CFLAGS is used in the compilation stage).

    The convention for libraries to be linked is to place them in either LOADLIBES or LDLIBS (GNU make includes both, but your mileage may vary):

    LDLIBS=-lm
    

    This should allow you to continue using the built-in rules rather than having to write your own linking rule. For other makes, there should be a flag to output built-in rules (for GNU make, this is -p). If your version of make does not have a built-in rule for linking (or if it does not have a placeholder for -l directives), you'll need to write your own:

    client.o: client.c
        $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<
    
    client: client.o
        $(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@
    
    0 讨论(0)
  • 2020-11-28 05:08

    Your linker (ld) obviously doesn't like the order in which make arranges the GCC arguments so you'll have to change your Makefile a bit:

    CC=gcc
    CFLAGS=-Wall
    LDFLAGS=-lm
    
    .PHONY: all
    all: client
    
    .PHONY: clean
    clean:
        $(RM) *~ *.o client
    
    OBJECTS=client.o
    client: $(OBJECTS)
        $(CC) $(CFLAGS) $(OBJECTS) -o client $(LDFLAGS)
    

    In the line defining the client target change the order of $(LDFLAGS) as needed.

    0 讨论(0)
提交回复
热议问题