Code::Blocks Forums

User forums => General (but related to Code::Blocks) => Topic started by: Phrosen on December 21, 2015, 06:14:55 pm

Title: Error: redefinition of function
Post by: Phrosen 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?
Title: Re: Error: redefinition of function
Post by: headkase 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 (https://www.thenewboston.com/videos.php?cat=16&video=17491).
Title: Re: Error: redefinition of function
Post by: stahta01 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 (http://forums.codeblocks.org/index.php/topic,9996.0.html)

Also, Post a full build log in code tags.

Tim S.
Title: Re: Error: redefinition of function
Post by: Phrosen 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?
Title: Re: Error: redefinition of function
Post by: headkase on December 27, 2015, 01:20:47 pm
See: https://en.wikipedia.org/wiki/Include_guard
And: https://en.wikipedia.org/wiki/Pragma_once
Title: Re: Error: redefinition of function
Post by: Jenna 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.
Title: Re: Error: redefinition of function
Post by: Phrosen 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.
Title: Re: Error: redefinition of function
Post by: frithjofh 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...
Title: Re: Error: redefinition of function
Post by: Phrosen 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)
Title: Re: Error: redefinition of function
Post by: Jenna 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).
Title: Re: Error: redefinition of function
Post by: Phrosen 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.)
Title: Re: Error: redefinition of function
Post by: Jenna 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".
Title: Re: Error: redefinition of function
Post by: Phrosen 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?
Title: Re: Error: redefinition of function
Post by: raynebc 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.