[Snowball-discuss] call stemmer from C++

Olly Betts olly@survex.com
Tue Mar 25 17:23:01 2003


On Tue, Mar 25, 2003 at 06:04:25PM +0100, Jacob Hallenborg wrote:
> Unfortunately I'm not a so good at this. Should I put the extern stuff 
> in api.h, header.h and stem.h?

The header file generated by the Snowball compiler which you include in
your own code - probably stem.h, but the "--output" option sets the name
so if you've generated it yourself it will be called whatever you asked
for!

Compare your stem.h to the file quoted below - it should match apart
from the "snowball_english_" prefixes and the "#ifdef __cplusplus" bits.

> And should the braces include the whole file (all declarations) or
> just some part?

Wrap the whole header, exactly as I show:

> > #ifdef __cplusplus
> > extern "C" {
> > #endif
> > 
> > /* This file was generated automatically by the Snowball to ANSI C
> >  * compiler */
> > 
> > extern struct SN_env * snowball_english_create_env(void);
> > extern void snowball_english_close_env(struct SN_env * z);
> > 
> > extern int snowball_english_stem(struct SN_env * z);
> > 
> > #ifdef __cplusplus
> > }
> > #endif

C++ function names are "mangled" into C-like function names for object
files.  The mangling includes the classname (if any) and argument types
(to allow overloading).  To link to a function defined in C you need
to tell C++ to use C linkage rules.  This is what extern "C" does.  You
need to apply it to the prototypes for the C functions when used from
C++, but it's fine to wrap the whole header if it doesn't declare or
define any C++ functions.

See any good C++ book for more details - e.g. section 9.2.4 of the 3rd
edition of "The C++ Programming Language" by Bjarne Stroustrup.

Cheers,
    Olly