链接库时的“undefined reference to”

    通常的“undefined reference to”都是找不到头文件和库文件,一般需要指定-I${header_path},-L${lib_path} -l${lib_name} ,都能解决问题,不过最近遇到一个特别奇怪的问题,加上以上的信息仍然报此错误,我的Makefile如下:

CROSS = mipsel-linux-
CFLAGS += -I./include
LDLIBS += -L./lib -lmtd


all: hello.c
	$(CROSS)gcc ${CFLAGS} ${LDLIBS} -o hello hello.c

出现以下的错误:

hello.c:(.text+0xa74): undefined reference to `libmtd_open'
hello.c:(.text+0xae0): undefined reference to `mtd_get_dev_info'
hello.c:(.text+0x13f0): undefined reference to `mtd_is_bad'
hello.c:(.text+0x1cc0): undefined reference to `mtd_write_oob'
hello.c:(.text+0x1df8): undefined reference to `mtd_write'
hello.c:(.text+0x1f68): undefined reference to `mtd_erase'
hello.c:(.text+0x2158): undefined reference to `mtd_mark_bad'
hello.c:(.text+0x2328): undefined reference to `libmtd_close'
collect2: ld returned 1 exit status
make: *** [all] Error 1


后来发现调换下-L的路径就能编译通过了,修改Makefile如下:

CROSS = mipsel-linux-
CFLAGS += -I./include
LDLIBS += -L./lib -lmtd


all: hello.c
	$(CROSS)gcc -o hello hello.c ${CFLAGS} ${LDLIBS}


真是编程世界无奇不有,究竟是什么原因呢?

你可能感兴趣的:(嵌入式开发)