4


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?

1 Answer 1

8

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"); }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That is the correct answer and a real urgent help. Thank you.
Why exactly does this work and where's the problem coming from?

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.