Author Topic: Error: redefinition of function  (Read 30646 times)

Offline Phrosen

  • Single posting newcomer
  • *
  • Posts: 7
Error: redefinition of function
« on: December 21, 2015, 06:14:55 pm »
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
}

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

Offline headkase

  • Almost regular
  • **
  • Posts: 159
Re: Error: redefinition of function
« Reply #1 on: December 21, 2015, 07:26:33 pm »
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.
« Last Edit: December 21, 2015, 07:28:07 pm by headkase »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Error: redefinition of function
« Reply #2 on: December 21, 2015, 09:28:05 pm »
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.
« Last Edit: December 21, 2015, 09:30:02 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Phrosen

  • Single posting newcomer
  • *
  • Posts: 7
Re: Error: redefinition of function
« Reply #3 on: December 27, 2015, 12:42:29 pm »
I created a few test files to show the error:

Here's main.c:
Code
#include "header.h"

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

    return 0;
}

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

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

#include "test.c"

void test();
void testTwo();

#endif


test.c:
Code
#include "header.h"

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

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

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)) ===|

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?

Offline headkase

  • Almost regular
  • **
  • Posts: 159

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Error: redefinition of function
« Reply #5 on: December 27, 2015, 01:21:31 pm »
There are very rare cases where you need to include *.c or *.cpp files.
Remove this include and it should work, if there are no other beginner errors.

Offline Phrosen

  • Single posting newcomer
  • *
  • Posts: 7
Re: Error: redefinition of function
« Reply #6 on: December 27, 2015, 02:29:51 pm »
So I removed
Code
#include "test.c"
from the header file, and got these errors:

Quote
C:\...\main.o:main.c|| undefined reference to `test'|
C:\...\main.o:main.c|| undefined reference to `testTwo'|
||=== Build failed: 2 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

The program doesn't run now, and it can't seem to find my functions in test.c anymore.

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
Re: Error: redefinition of function
« Reply #7 on: December 27, 2015, 05:37:00 pm »
hi Phrosen,

you completely miss the point.

It is possible to declare a function as often as you like throughout the code, but what you are doing wrong here, is that you define it more than once.

The include directive copies the text literally into the file.

If you examine closely in your code this leads to multiple definition of test and testTwo.

First you include it in main.c via the #include header.h in which there is an #include test.c which holds the definition.

Then in test.c you #include header.c again, which has test.c included with the same definition all over again. Its a bit recursive here, which makes it confusing to understand, but the compiler breaks of after the first recursion, when he finds the first redefinition of test and testTwo.

Solution is simple: don't include test.c in header.h

In order to find the actual implementation of the functions, you must make the implementation known to the compiler on the command line when invoking gcc.

In the case of compiling with c::b give the paths to the folders where these source files are found in the projects options and make sure the file test.c actually and really belongs to the project...
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100

Offline Phrosen

  • Single posting newcomer
  • *
  • Posts: 7
Re: Error: redefinition of function
« Reply #8 on: December 28, 2015, 10:43:25 am »
To frithjofh:
Oh, I see. I have to make it all a part of the same Project first.

I haven't been making projects up 'til now. Just using single files and connecting them via the header-file.
Using a project solved my problem. Thank you. =)

But I have another question: if I want to send these files to a friend. Should I just send my entire folder with all files. Or just the project file. Or the project file + the .c and .h files?
Also: does my project (.cbp) work in other IDEs? (Such as Xcode on MAC)

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Error: redefinition of function
« Reply #9 on: December 28, 2015, 05:24:42 pm »
But I have another question: if I want to send these files to a friend. Should I just send my entire folder with all files. Or just the project file. Or the project file + the .c and .h files?

You can try "Project ->  Create package for distribution", that should create a zip-file with the same basename as trhe project in the projects folder.
You might need a commandline zip.exe in the search path (http://wiki.codeblocks.org/index.php/Installing_Code::Blocks_from_source_on_Windows#ZIP).

Offline Phrosen

  • Single posting newcomer
  • *
  • Posts: 7
Re: Error: redefinition of function
« Reply #10 on: December 29, 2015, 02:24:43 pm »
I can't find "Create package for distribution" under "Project" (or any other menu for that matter.)

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Error: redefinition of function
« Reply #11 on: December 29, 2015, 04:53:36 pm »
I can't find "Create package for distribution" under "Project" (or any other menu for that matter.)
Look into "Settings -> Scripting" and try to enable the "make_dist.script".

Offline Phrosen

  • Single posting newcomer
  • *
  • Posts: 7
Re: Error: redefinition of function
« Reply #12 on: December 29, 2015, 07:58:29 pm »
That worked.

Now I've made a .zip file out of my project. (Including all of my .c files, my .h file and the .cbp file)
If I send this .zip file to a friend and they unpack it. -Will they be able to open it with another IDE?

Offline raynebc

  • Almost regular
  • **
  • Posts: 217
Re: Error: redefinition of function
« Reply #13 on: December 29, 2015, 09:19:49 pm »
Some IDEs support other IDEs' project formats, but this isn't guaranteed.  They may have to create it themselves, using the source and header files as they are.