News:

As usual while waiting for the next release - don't forget to check the nightly builds in the forum.

Main Menu

Error: redefinition of function

Started by Phrosen, December 21, 2015, 06:14:55 PM

Previous topic - Next topic

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:

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?

headkase

#1
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

#2
Post the complete header file; likely you did NOT guard it correctly.

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

Also, Post a full build log in code tags.

Tim S.
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

Phrosen

I created a few test files to show the error:

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

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

    return 0;
}


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

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

#include "test.c"

void test();
void testTwo();

#endif



test.c:
#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:
QuoteC:\...\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?


Jenna

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.

Phrosen

So I removed #include "test.c" from the header file, and got these errors:

QuoteC:\...\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.

frithjofh

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

Phrosen

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)

Jenna

Quote from: Phrosen on December 28, 2015, 10:43:25 AM
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).

Phrosen

I can't find "Create package for distribution" under "Project" (or any other menu for that matter.)

Jenna

Quote from: 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.)
Look into "Settings -> Scripting" and try to enable the "make_dist.script".

Phrosen

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?

raynebc

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.