使用 LIBSVM 将我的 svm.cpp 文件与我的 C 程序链接时出错

Error linking my svm.cpp file using LIBSVM with my C program

我正在尝试使用 C 在 Eclipse IDE 上使用 LIBSVM 包。

在 README 文件中,它指出 "You need to #include "svm.h" 在您的 C/C++ 源文件中,并且 link 您的程序带有 `svm.cpp'。"

我已经在我的所有源文件中包含 "svm.h",并且我在源代码文件夹下添加了我的 'svm.cpp' 文件。但是,我在 'svm.cpp' 文件中收到错误 "template with C linkage"。

错误是否与我在程序中添加 '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 __cplusplus
extern "C" {
#endif

int 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; }
#endif


template <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

This version 没有 extern "C" 模板。

如果您自己添加了它们,就没有其他人可以责怪。

如果您的版本带有 extern "C",则该版本已损坏:模板确实不能具有 extern "C" 链接。

更新:

I added the extern "C" by myself because I thought it was necessary. Is it wrong?

是的,是的。

when I did not add extern "C", there were more errors

您应该接受这个问题的答案,删除您的修改,并提出一个新问题详细说明其他错误。