Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple definition of linking error in C

Tags:

c

linker


I am developing an application in C. I want to use a local function with the same name in more than one source files. Let me simplify the issue:

In hell.c

void myLocalFunc(){ printf("Hello hell\r\n"); }

In world.c

void myLocalFunc(){ printf("Hello world\r\n"); }

Because they are local functions only, i dont declare them in header files. But when i compile the project, it gives me "Multiple definition of 'myLocalFunc'" error message and also this one: "Multiple definition of 'myLocalFunc' (first defined here)".

What is my mistake here?

like image 308
Fer Avatar asked Feb 23 '23 07:02

Fer


1 Answers

Replace it with

static void myLocalFunc(){ printf("Hello world\r\n"); }

Or, if you're using C++, you can also use an anonymous namespace like this:

namespace {
void myLocalFunc(){ printf("Hello world\r\n"); }
}
like image 55
Andrea Bergia Avatar answered Mar 04 '23 00:03

Andrea Bergia