我在Eclipse IDE上使用C语言尝试使用LIBSVM包。
在README文件中提到,“您需要在您的C/C++源文件中包含’svm.h’,并将您的程序与’svm.cpp’链接。”
我已经在所有源文件中包含了“svm.h”,并且我在源代码文件夹下添加了’svm.cpp’文件。然而,我在’svm.cpp’文件中得到了“带有C链接的模板”的错误。
这个错误与我在程序中添加’svm.cpp’文件的方式有关吗?我真的不明白,我到处搜索过了。
svm.cpp:
#include <math.h>#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include <float.h>#include <string.h>#include <stdarg.h>#include <limits.h>#include <locale.h>#include "libsvm.h"#include "main.h"#ifdef __cplusplusextern "C" {#endifint libsvm_version = LIBSVM_VERSION;typedef float Qfloat;typedef signed char schar;#ifndef min template <class T> static inline T min(T x,T y) { return (x<y)?x:y; }#endif#ifndef max template <class T> static inline T max(T x,T y) { return (x>y)?x:y; }#endiftemplate <class T> static inline void swap(T& x, T& y) { T t=x; x=y; y=t; }template <class S, class T> static inline void clone(T*& dst, S* src, int n)........#ifdef __cplusplus}#endif
回答:
您使用的是libsvm的哪个版本?是从哪里下载的?
这个版本的svm.cpp
文件周围没有extern "C"
包围模板。
如果您自己添加了这些,那么您就只能怪自己了。
如果您的版本自带extern "C"
,那么这个版本是有问题的:模板确实不能有extern "C"
链接。
更新:
我自己添加了extern “C”,因为我认为这是必要的。这样做错了吗?
是的,这样做是错的。
当我不添加extern “C”时,会有更多的错误
您应该接受这个问题的答案,删除您的修改,并提出一个新的问题,详细说明其他错误。