在 SVM-Struct 中未定义对 sqrt 的引用(已使用 -lm)

undefined reference to sqrt in SVM-Struct (have used -lm)

本文关键字:-lm 引用 SVM-Struct 未定义 sqrt      更新时间:2023-10-16

实际上,我正在使用Ubuntu 16.04上著名的SVM结构项目(http://www.cs.cornell.edu/people/tj/svm_light/svm_struct.html(。我按照 http://www.cs.cornell.edu/people/tj/svm_light/svm_multiclass.html 中的说明使用 SVM 多类,下载了源代码并make.但是我在构建项目时遇到了一些错误:

$ make
cd svm_light; make svm_learn_hideo_noexe
make[1]: Entering directory '/home/parallels/CLionProjects/svm_multiclass/svm_light'
make[1]: Nothing to be done for 'svm_learn_hideo_noexe'.
make[1]: Leaving directory '/home/parallels/CLionProjects/svm_multiclass/svm_light'
cd svm_struct; make svm_struct_noexe
make[1]: Entering directory '/home/parallels/CLionProjects/svm_multiclass/svm_struct'
make[1]: Nothing to be done for 'svm_struct_noexe'.
make[1]: Leaving directory '/home/parallels/CLionProjects/svm_multiclass/svm_struct'
gcc  -O3 -lm -Wall svm_struct/svm_struct_learn.o svm_struct_learn_custom.o svm_struct_api.o svm_light/svm_hideo.o svm_light/svm_learn.o svm_light/svm_common.o svm_struct/svm_struct_common.o svm_struct/svm_struct_main.o -o svm_multiclass_learn 
svm_light/svm_learn.o: In function `estimate_sphere':
svm_learn.c:(.text+0x6e88): undefined reference to `sqrt'
svm_light/svm_learn.o: In function `estimate_r_delta':
svm_learn.c:(.text+0x7053): undefined reference to `sqrt'
svm_light/svm_learn.o: In function `estimate_r_delta_average':
svm_learn.c:(.text+0x7b04): undefined reference to `sqrt'
svm_light/svm_learn.o: In function `length_of_longest_document_vector':
svm_learn.c:(.text+0x7b86): undefined reference to `sqrt'
svm_light/svm_learn.o: In function `incorporate_unlabeled_examples':
svm_learn.c:(.text+0x89eb): undefined reference to `sqrt'
svm_light/svm_learn.o:svm_learn.c:(.text+0xc952): more undefined references to `sqrt' follow
svm_light/svm_common.o: In function `classify_example':
svm_common.c:(.text+0x38f): undefined reference to `tanh'
svm_common.c:(.text+0x42d): undefined reference to `exp'
svm_common.c:(.text+0x4a7): undefined reference to `pow'
svm_light/svm_common.o: In function `kernel':
svm_common.c:(.text+0x870): undefined reference to `tanh'
svm_common.c:(.text+0x90d): undefined reference to `exp'
svm_common.c:(.text+0x988): undefined reference to `pow'
svm_light/svm_common.o: In function `model_length_s':
svm_common.c:(.text+0x2c8d): undefined reference to `sqrt'
svm_light/svm_common.o: In function `model_length_n':
svm_common.c:(.text+0x2f5c): undefined reference to `sqrt'
svm_light/svm_common.o: In function `cholesky_matrix':
svm_common.c:(.text+0x3c6f): undefined reference to `sqrt'
svm_light/svm_common.o: In function `find_indep_subset_of_matrix':
svm_common.c:(.text+0x3f41): undefined reference to `sqrt'
svm_light/svm_common.o: In function `single_kernel':
svm_common.c:(.text+0xc0c): undefined reference to `tanh'
svm_common.c:(.text+0xc84): undefined reference to `pow'
svm_common.c:(.text+0xcfb): undefined reference to `exp'
collect2: error: ld returned 1 exit status
Makefile:48: recipe for target 'svm_multiclass_learn' failed
make: *** [svm_multiclass_learn] Error 1

我认为制作文件已经包含了-lm.

# Makefile for SVM-multiclass, 03.07.04
#Use the following to compile under unix or cygwin
CC = gcc
LD = gcc
#Call 'make' using the following line to make CYGWIN produce stand-alone Windows executables
#       make 'SFLAGS=-mno-cygwin'
CFLAGS =   $(SFLAGS) -O3 -fomit-frame-pointer -ffast-math -Wall
LDFLAGS =  $(SFLAGS) -O3 -lm -Wall
#CFLAGS =  $(SFLAGS) -pg -Wall
#LDFLAGS = $(SFLAGS) -pg -lm -Wall 
all: svm_multiclass_learn svm_multiclass_classify
.PHONY: clean
clean: svm_light_clean svm_struct_clean
rm -f *.o *.tcov *.d core gmon.out *.stackdump 
#-----------------------#
#----   SVM-light   ----#
#-----------------------#
svm_light_hideo_noexe: 
cd svm_light; make svm_learn_hideo_noexe
svm_light_clean: 
cd svm_light; make clean
#----------------------#
#----  STRUCT SVM  ----#
#----------------------#
svm_struct_noexe: 
cd svm_struct; make svm_struct_noexe
svm_struct_clean: 
cd svm_struct; make clean
#-------------------------#
#----  SVM MULTICLASS ----#
#-------------------------#
svm_multiclass_classify: svm_light_hideo_noexe svm_struct_noexe svm_struct_api.o svm_struct/svm_struct_classify.o svm_struct/svm_struct_common.o svm_struct/svm_struct_main.o 
$(LD) $(LDFLAGS) svm_struct_api.o svm_struct/svm_struct_classify.o svm_light/svm_common.o svm_struct/svm_struct_common.o -o svm_multiclass_classify $(LIBS)
svm_multiclass_learn: svm_light_hideo_noexe svm_struct_noexe svm_struct_api.o svm_struct_learn_custom.o svm_struct/svm_struct_learn.o svm_struct/svm_struct_common.o svm_struct/svm_struct_main.o
$(LD) $(LDFLAGS) svm_struct/svm_struct_learn.o svm_struct_learn_custom.o svm_struct_api.o svm_light/svm_hideo.o svm_light/svm_learn.o svm_light/svm_common.o svm_struct/svm_struct_common.o svm_struct/svm_struct_main.o -o svm_multiclass_learn $(LIBS)

svm_struct_api.o: svm_struct_api.c svm_struct_api.h svm_struct_api_types.h svm_struct/svm_struct_common.h
$(CC) -c $(CFLAGS) svm_struct_api.c -o svm_struct_api.o
svm_struct_learn_custom.o: svm_struct_learn_custom.c svm_struct_api.h svm_light/svm_common.h svm_struct_api_types.h svm_struct/svm_struct_common.h
$(CC) -c $(CFLAGS) svm_struct_learn_custom.c -o svm_struct_learn_custom.o

链接器首先收集依赖项(svm_light/svm_learn.o(,并在看到定义(-lm(时填充它们。

您似乎在ld调用结束时已经有了$(LIBS),所以只是:

LIBS += -lm

应该修复它。

其他提示:

  • 您可以将模式cd X; make TARGET替换为make -C X TARGET
  • 您可以将目标称为$@,将依赖项列表中的第一个先决条件称为$<,因此您的最后一个规则可以编写为:
svm_struct_learn_custom.o: svm_struct_learn_custom.c svm_struct_api.h svm_light/svm_common.h svm_struct_api_types.h svm_struct/svm_struct_common.h
$(CC) -c $(CFLAGS) $< -o $@

其中配方重新定义了用于编译 .c 文件的内置配方,因此只需将标头列为(额外(先决条件就足够了:

svm_struct_learn_custom.o: svm_struct_api.h svm_light/svm_common.h svm_struct_api_types.h svm_struct/svm_struct_common.h