编译时对“dgesv_”的未定义引用

时间:2013-01-19 22:44:07

标签: c makefile

我有以下makefile:

CC = gcc

ROOTPATH = /home/swagatam
LIB_PATH = $(ROOTPATH)/mylib/libf2c.a \
            $(ROOTPATH)/mylib/blas_LINUX.a \
        $(ROOTPATH)/mylib/lapack_LINUX.a -lm


INC_PATH = -I$(ROOTPATH)/myinclude

swadgesv : swadgesv.o
    $(CC) $(LIB_PATH) swadgesv.o -o swadgesv

swadgesv.o : swadgesv.c
    $(CC) $(INC_PATH) -c swadgesv.c

clean:
    rm -f *.o

swadgesv.c是程序求解方程形式A.X = B.我需要3个头文件和3个库来链接解决概率。但是当我在终端中运行Makefile时,它会显示:

gcc -I/home/swagatam/myinclude -c swadgesv.c
gcc /home/swagatam/mylib/libf2c.a /home/swagatam/mylib/blas_LINUX.a         /home/swagatam/mylib/lapack_LINUX.a -lm swadgesv.o -o swadgesv
swadgesv.o: In function `main':
swadgesv.c:(.text+0x0): multiple definition of `main'
/home/swagatam/mylib/libf2c.a(main.o):(.text.startup+0x0): first defined  here
/home/swagatam/mylib/libf2c.a(main.o): In function `main':
(.text.startup+0xad): undefined reference to `MAIN__'
swadgesv.o: In function `main':
swadgesv.c:(.text+0xc1): undefined reference to `dgesv_'
collect2: ld returned 1 exit status
make: *** [swadgesv] Error 1

即使我在LIB_PATH中更改库的顺序,它也会显示不同的错误。我从我安装的clapack库中调用函数dgsev。

1 个答案:

答案 0 :(得分:6)

将您的库放在目标文件之后:

swadgesv: swadgesv.o
    $(CC) swadgesv.o -o swadgesv $(LIB_PATH)

如果首先列出库,则加载器正在寻找符号main(),它通常不会在库中找到它,因此它没有理由链接库中的任何符号并实际忽略他们。当它最终遇到您的目标文件时,它会找到main(),但在目标文件之后的(不存在的)参数中找不到main()引用的符号。因此符号保持不确定。链接行上的参数顺序很重要。