一、问题描述
下载了nmealib库文件,默认工程进行编译,报错,提示如下:
gcc -I include -c src/generate.c -o build/nmea_gcc/generate.o
gcc -I include -c src/generator.c -o build/nmea_gcc/generator.o
ar rsc lib/libnmea.a build/nmea_gcc/generate.o build/nmea_gcc/generator.o build/nmea_gcc/parse.o build/nmea_gcc/parser.o build/nmea_gcc/tok.o build/nmea_gcc/context.o build/nmea_gcc/time.o build/nmea_gcc/info.o build/nmea_gcc/gmath.o build/nmea_gcc/sentence.o
ranlib lib/libnmea.a
gcc -I include -c samples/generate/main.c -o samples/generate/main.o
gcc samples/generate/main.o -Llib -lnmea -o build/samples_generate
/usr/bin/ld: lib/libnmea.a(generate.o): in function `nmea_gsv_npack':
generate.c:(.text+0x729): undefined reference to `ceil'
collect2: error: ld returned 1 exit status
make: *** [Makefile:43: samples_generate] Error 1
rm samples/generate/main.o
网上搜索,因为使用了 ceil数学相关的库,说是链接需要添加 -lm 的参数。
二、解决办法
官方的 Makefile 文件
CC = gcc
BIN = lib/libnmea.a
MODULES = generate generator parse parser tok context time info gmath sentence
SAMPLES = generate generator parse parse_file math
OBJ = $(MODULES:%=build/nmea_gcc/%.o)
LINKOBJ = $(OBJ) $(RES)
SMPLS = $(SAMPLES:%=samples_%)
SMPLOBJ = $(SAMPLES:%=samples/%/main.o)
INCS = -I include
LIBS = -lm -Llib -lnmea
.PHONY: all all-before all-after clean clean-custom doc
all: all-before $(BIN) samples all-after
all-before:
mkdir -p build/nmea_gcc
clean: clean-custom
rm -f $(LINKOBJ) $(BIN) $(SMPLOBJ) $(SMPLS)
doc:
$(MAKE) -C doc
remake: clean all
$(BIN): $(LINKOBJ)
ar rsc $@ $^
ranlib $@
build/nmea_gcc/%.o: src/%.c
$(CC) $(INCS) -c $< -o $@
samples: $(SMPLS)
samples_%: samples/%/main.o
$(CC) $< $(LIBS) -o build/$@
samples/%/main.o: samples/%/main.c
$(CC) $(INCS) -c $< -o $@
可以看到 LIBS 已经包含了 lm 。目前的报错该怎么解决呢?解决办法:修改 LIBS 中 lm 的位置,放在后面。 如下:
LIBS = -Llib -lnmea -lm
再次编译,官方工程编译成功。