User forums > General (but related to Code::Blocks)

Error: redefinition of function

(1/3) > >>

Phrosen:
I have a problem that I think is related to my IDE. I am using Codeblocks (13.12).

Here's my "set up":
main.c (includes 'int main()')
header.h (your typical header, includes prototypes)
test.c (a random file, includes custom-made functions.)

Here's the issue: All the functions works as intended, but when I compile my test.c I get an error (for each function) saying: "error: redefinition of ***"
This issue doesn't affect anything, but it's annoying. I'm wondering if it's possible to get rid of it somehow? Maybe I'm doing something wrong when I'm creating my prototypes?

Here's an example of what my functions and prototypes look like:


--- Code: ---void func_showMenu();  //This is the prototype, in header.h

void func_showMenu(){
  //This is the function, in test.c
}
--- End code ---

Is there some setting in Code::Blocks that can fix this issue?

headkase:
Please post the complete contents of both your header and source files.  If each function pair is the same then one complete pair will suffice.  Make sure to include the boiler-plate code like the: define that is supposed to be in a header.

stahta01:
Post the complete header file; likely you did NOT guard it correctly.

Edit: read this site rules. http://forums.codeblocks.org/index.php/topic,9996.0.html

Also, Post a full build log in code tags.

Tim S.

Phrosen:
I created a few test files to show the error:

Here's main.c:

--- Code: ---#include "header.h"

int main(){
    test();
    testTwo();

    return 0;
}

--- End code ---

header.h:

--- Code: ---#ifndef header.h
#define header.h

#include <stdio.h>
#include <stdlib.h>

#include "test.c"

void test();
void testTwo();

#endif


--- End code ---

test.c:

--- Code: ---#include "header.h"

void test(){
    printf("Test works! \n");
}

void testTwo(){
    printf("TestTwo also works! \n");
}

--- End code ---

This is all there is, nothing more, nothing less.

When I run the program (main.c) it outputs:
"Test works!"
"TestTwo also works!"

So the program runs just fine.
But. Whenever I build (ctrl+F9) test.c I get the following errors:

--- Quote ---C:\...\test.c|3|error: redefinition of 'test'|
C:\...\test.c|3|note: previous definition of 'test' was here|
C:\...\test.c|7|error: redefinition of 'testTwo'|
C:\...\test.c|7|note: previous definition of 'testTwo' was here|
||=== Build failed: 2 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
--- End quote ---

If I add more functions in test.c I will get one error like this for each function added.
Like I said: the program still runs and all, but it is very annoying.
Is there some way to fix this?

headkase:
See: https://en.wikipedia.org/wiki/Include_guard
And: https://en.wikipedia.org/wiki/Pragma_once

Navigation

[0] Message Index

[#] Next page

Go to full version