Undefined reference to 'pow' even though -lm is a compile flag. [C]

前端 未结 2 968
不思量自难忘°
不思量自难忘° 2020-12-10 16:36

Any reason

cc -g -lm -DBLITZ_HOST_IS_LITTLE_ENDIAN

would produce an error with code using math.h? Is it possible there\'s a d

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-10 17:28

    Instead of

    cc -g -lm -DBLITZ_HOST_IS_LITTLE_ENDIAN foo.c
    

    Try:

    cc -g -DBLITZ_HOST_IS_LITTLE_ENDIAN foo.c -lm
    

    When the linker searches a library, it links in modules that contain definitions for previously-undefined symbols.

    If the linker searches -lm before foo.o, then pow() is not yet undefined. Conversely, if foo.o comes first, it undefines pow(), which -lm can then resolve.


    EDIT: To accomplish this advice in your makefile, make these changes:

    CFLAGS=-g -DBLITZ_HOST_IS_LITTLE_ENDIAN
    LDLIBS=-lm
    
    ...
    
    asm: asm.c
            $(CC) $(CFLAGS) asm.c $(LDLIBS) -o asm
    

提交回复
热议问题