Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use C function in C++ program; "multiply-defined" error

I am trying to use this code for the Porter stemming algorithm in a C++ program I've already written. I followed the instructions near the end of the file for using the code as a separate module. I created a file, stem.c, that ends after the definition and has

extern int stem(char * p, int i, int j) ...

It worked fine in Xcode but it does not work for me on Unix with gcc 4.1.1--strange because usually I have no problem moving between the two. I get the error

ld: fatal: symbol `stem(char*, int, int)' is multiply-defined: (file /var/tmp//ccrWWlnb.o type=FUNC; file /var/tmp//cc6rUXka.o type=FUNC); ld: fatal: File processing errors. No output written to cluster

I've looked online and it seems like there are many things I could have wrong, but I'm not sure what combination of a header file, extern "C", etc. would work.

like image 427
eom Avatar asked Dec 12 '22 22:12

eom


1 Answers

That error means that the symbol (stem) is defined in more than one module.

You can declare the symbol in as many modules as you want. A declaration of a function looks like this:

int stem(char * p, int i, int j);

You don't need the "extern" keyword, although it doesn't hurt anything. For functions declarations, it's implied.

A definition of a function looks like this:

int stem(char * p, int i, int j) 
{
    /* body of your function */
}

The "multiply-defined" error indicates that you have two modules with a definition for the same function. That usually means that you have two files that define the function, or two files that #include a file that defines the function. Normally, you should not put function definitions in files that you #include. Put the definition in a .c, .cpp, or .cc file and just put a declaration in a .h file that you #include.

For example, you could create a stem.h file with this in it:

int stem(char * p, int i, int j);

Then, #include "stem.h".

like image 51
Daniel Stutzbach Avatar answered Dec 26 '22 20:12

Daniel Stutzbach