C- linkage program

C

crea

I have a FANN library (ANN: http://leenissen.dk/fann/wp/ ) which I try to
compile. Its basically C-library but has C++ wrapper as well. So most files
are *.c.

C-compiling works ok, but when I compile C++ version then I get error:
"C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\math.h(514) : error
C2894: templates cannot be declared to have 'C' linkage"

So math.h function seems to give troubles. I can see it has to do with
mixing C and C++ things together...

Any advice? Somebody said that try to use C version of math.h library. How
to do that? (now its #include <math.h>). Or do I need to change all
c-files to cpp? Or other way to change everything to c++ style?

There is this kind of section in one file, which seems to be linked to this
problem. This is the only place where #ifdef __cplusplus and extern "C" is
used:
....
#ifdef __cplusplus
extern "C"
{

#ifndef __cplusplus
} /* to fool automatic indention engines */
#endif
#endif /* __cplusplus */

#ifndef NULL
#define NULL 0
#endif /* NULL */
....
 
J

jacob navia

Le 06/03/11 18:08, crea a écrit :
I have a FANN library (ANN: http://leenissen.dk/fann/wp/ ) which I try to
compile. Its basically C-library but has C++ wrapper as well. So most files
are *.c.

C-compiling works ok, but when I compile C++ version then I get error:
"C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\math.h(514) : error
C2894: templates cannot be declared to have 'C' linkage"

The macro "max" is giving problems. The C++ people decided to make it a
template, a very good idea. Two years ago I earned quite a good
consulting fee changing all those max(a,b) into MaX(a,b). The problem
(then) was that "max(2.3,6) " would not work because of two different
types into the template max.

Now, you have included some C++ files and somewhere they defined the
max template. Then you include math.h, and then everything breaks.

Just a wild gues of course. You did not tell anyone what isat line
514 of math.h. Is it a secret?
 
C

crea

Thanks. Lets solve this!! I will get very happy if it happens :). Really
want to use this library...its awesome.

jacob said:
Le 06/03/11 18:08, crea a écrit :

The macro "max" is giving problems. The C++ people decided to make it

So certain functions give problems, i guess. This seems to be Power (see
below)
a template, a very good idea. Two years ago I earned quite a good
consulting fee changing all those max(a,b) into MaX(a,b). The problem
(then) was that "max(2.3,6) " would not work because of two different
types into the template max.

ok, so might be only a template problem then. Good thought.
Now, you have included some C++ files and somewhere they defined the
max template. Then you include math.h, and then everything breaks.

Just a wild gues of course. You did not tell anyone what isat line
514 of math.h. Is it a secret?

ok, here it is. Can you see anything? thanks very much:

#ifdef __cplusplus
}

#if !defined(_M_M68K)


line 514: template<class _Ty> inline
_Ty _Pow_int(_Ty _X, int _Y)
{unsigned int _N;
if (_Y >= 0)
_N = _Y;
else
_N = -_Y;
for (_Ty _Z = _Ty(1); ; _X *= _X)
{if ((_N & 1) != 0)
_Z *= _X;
if ((_N >>= 1) == 0)
return (_Y < 0 ? _Ty(1) / _Z : _Z); }}
 
C

crea

jacob said:
Le 06/03/11 18:08, crea a écrit :

The macro "max" is giving problems. The C++ people decided to make it

btw, function max() is not redefined in this project. They have created
their own version actually: fann_max, and they use it. So it seems to me
that Power is the problem
 
J

jacob navia

Le 06/03/11 22:42, crea a écrit :
btw, function max() is not redefined in this project. They have created
their own version actually: fann_max, and they use it. So it seems to me
that Power is the problem
Well, the problem is that there is an "extern "c" " too much

If you read the error message it says:


This means that maybe somebody did:

extern "C" {
#include <math.h>
}

Now, math.h apparnetly has been modified to be a C++ header since
it defines templates.
 
C

crea

jacob said:
Le 06/03/11 22:42, crea a écrit :

This means that maybe somebody did:

extern "C" {
#include <math.h>
}

Now, math.h apparnetly has been modified to be a C++ header since
it defines templates.

Yes, there is this code:

#ifdef __cplusplus
extern "C"
{

#ifndef __cplusplus
} /* to fool automatic indention engines */
#endif
#endif /* __cplusplus */

#ifndef NULL
#define NULL 0
#endif /* NULL */

But I dont know how to change that.
 
C

crea

jacob said:
This means that maybe somebody did:

extern "C" {
#include <math.h>
}

Now, math.h apparnetly has been modified to be a C++ header since
it defines templates.

hmm, I got a wild idea: I could make a copy of that C++ math.h file, and
change that one template function to be non-template! What do you think?
Then use this modified mathMod.h instead...
 
C

crea

crea said:
hmm, I got a wild idea: I could make a copy of that C++ math.h file,
and change that one template function to be non-template! What do you
think? Then use this modified mathMod.h instead...

Yes! I did that (I changed template Pow to be 4 different Pow s). I took the
only template off and changed to C-funtions. Now seems to work! nice. Thanks
for giving me direction, your advices helped to find this. I created
FannMath.h file and use it instead of math.h.
 
A

Alf P. Steinbach /Usenet

* crea, on 06.03.2011 23:44:
Yes! I did that (I changed template Pow to be 4 different Pow s). I took the
only template off and changed to C-funtions. Now seems to work! nice. Thanks
for giving me direction, your advices helped to find this. I created
FannMath.h file and use it instead of math.h.

That may or rather will land you in trouble.

Since you were able to change the include directive, then fix the include, not
the header file.

That is, place the include outside 'extern "C"', and use [math.h].


Cheers & hth.,

- Alf
 
C

crea

Alf said:
That may or rather will land you in trouble.

Why would it because my FannMath.h is exatcly the same as math.h in machine
code. The code created is exatcly the same as math.h... i just replaced
template with 4 versions (math.h uses only 4 different versions of it inside
the file). I think you know what I mean...
Since you were able to change the include directive, then fix the
include, not the header file.

That is, place the include outside 'extern "C"', and use [math.h].

yes, I would do this if I knew how. The problem is that math.h is NOT even
in the same file where this 'extern "C"' is. So I dont really know how it
goes...

'extern "C"' is in h-file and [math.h]. is in c-file and in another h-file.
 
C

crea

Alf said:
That may or rather will land you in trouble.

Actually not really. I just have to remember what I did (put a comment). The
code will work 100% the same as if I used math.h.

Of course if I use other platforms /compilers I have to do something else
maybe.

But true that your suggestion is better.. if I just knew how to do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top