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

Error: redefinition of function

<< < (2/3) > >>

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
--- Code: ---#include "test.c"
--- End code ---
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)) ===|

--- End quote ---

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...

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?

--- End quote ---

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

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version