Ryan Jaya Ryan Jaya - 6 months ago 41
C Question

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

I am trying to use the LIBSVM package on Eclipse IDE using C.

In the README file, it states that "You need to #include "svm.h" in your C/C++ source files and
link your program with `svm.cpp'."

I already included "svm.h" in all my source files and I added my 'svm.cpp' file under the source code folder. However, I got an error "template with C linkage" in the 'svm.cpp' file.

Does the error have anything to do with the way I added the 'svm.cpp' file in my program? I really do not understand and I tried searching everywhere.


#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" {

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; }

#ifndef max
template <class T> static inline T max(T x,T y) { return (x>y)?x:y; }

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

Answer Source

What version of libsvm you are using, and where did you download it from?

This version of svm.cpp does not have extern "C" around templates.

If you added them yourself, you have no-one else to blame.

If your version came with extern "C", that version is broken: templates really can't have extern "C" linkage.


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

Yes, it is.

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

You should accept this answer to your question, remove your modifications, and ask a new question detailing the other errors.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download