4

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.

3
  • Are you somehow #incude'ing that C file in other files ? What's the command lines you use to compile/link your program ? Commented May 2, 2010 at 19:11
  • Yes, in one function's .cpp file I have #include "stem.c". Compiling with g++ Whatever.cpp Anotherthing.cpp stem.c -o myprogram. Commented May 2, 2010 at 19:13
  • 2
    Well there's your problem! By the way, this has nothing whatsoever to do with mixing C and C++. The same thing would happen if you had #include ed one .cpp file inside another. Commented May 2, 2010 at 19:19

3 Answers 3

14

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".

Sign up to request clarification or add additional context in comments.

1 Comment

Of course! I was getting so distracted by the unfamiliar aspects of the C code that I didn't see this. Thank you. I'm getting a new error doing it this way in Xcode, but it works in Unix which is the important thing.
0

The fact that Whatever.cpp has #include "stem.c" provides the first definition, and specifying stem.c on the compiler command line provides the second definition.

You should break up stem.c into a header file (With just function prototypes) and a .c file which contains the implemention. Include only the header file in Whatever.cpp

Comments

0

You need to add "C". You need to extern "C" { ... } and only actually define the function once. But you can declare it (the prototype) as often as you like.

1 Comment

The first part of this really isn't true for the case of this question. You only need extern "C" if you need a C++ function to be able to be called from C code. In this case, it's the other way around and a C function is being called from C++ code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.