Use the C function in a C ++ program; multi-valued error

I am trying to use this code for the Porter restriction algorithm in a C ++ program that I already wrote. I followed the instructions at the end of the file to use the code as a separate module. I created a file, stem.c, which 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 - it is strange because usually I have no problem navigating between them. I get an error

ld: fatal: symbol `stem (char *, int, int) 'repeatedly: (file / var / tmp // ccrWWlnb.o type = FUNC; file / var / tmp // cc 6rUXka.o type = FUNC); ld: fatal: File processing errors. No output written to the cluster

I looked online and it seems that there are many things that I could be wrong, but I'm not sure what combination of header file, extern "C" etc. is. will work.

+3
source share
3 answers

This error means that the symbol (base) is defined in several modules.

You can declare a symbol of as many modules as possible. The function declaration is as follows:

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

You do not need the keyword "extern", although this will not hurt anything. For function declarations, this is implied.

A function definition is as follows:

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

" " , . , , , , # , . , #include. .c,.cpp .cc .h, #include.

, stem.h :

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

#include "stem.h".

+12

, Whatever.cpp #include "stem.c", , stem.c .

stem.c ( ) .c, . Whatever.cpp

0

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

0
source

Source: https://habr.com/ru/post/1743733/


All Articles