• 0

[C] Nasty Error: Duplicate Symbol


Question

I encountered this error a couple of days ago:

nastyError.png

I have not been able to fix it. I've tried the suggestions on this page at stack overflow, but to no avail. There are no duplicate files in my project, and there are no duplicate function definitions in my program. Does anyone have any ideas where the source of the error may be, or could be?

The developer tool returning the error is Xcode. Thanks in advance :)

Link to comment
https://www.neowin.net/forum/topic/1078449-c-nasty-error-duplicate-symbol/
Share on other sites

12 answers to this question

Recommended Posts

  • 0

This is difficult to solve without seeing the entire sourcecode, all I can suggest is that you check all the import/includes. Also, Xcode can sometimes forget to recompile some objects, try doing Product -> Clean and rebuild

Also, what is updateStudents? I assume it's a function

  • 0

Yes, updateStudents is a function. I tried the product clean, and it still fails.

Update students is only mentioned twice in my program. Once as the declaration, secondly as the definition, so there is definitely no repeat that i can find:


void updateStudents(Game g, int diceRolled);
void updateStudents(Game g, int diceRolled){
int i = 0;
int j = 0;
int numConverted = 0;
if (diceRolled != 7){
while (i < NUM_REGIONS){
if (g->regions[i].diceValue == diceRolled){
for (j = 0;j < g->campusesOwned;j++){
if ( (g->campuses[j].vertexID == regionToVertex(i) ) ||
(g->campuses[j].vertexID == regionToVertex(i) + 1) ||
(g->campuses[j].vertexID == regionToVertex(i) + 6) ||
(g->campuses[j].vertexID == regionToVertex(i) + 7) ||
(g->campuses[j].vertexID == regionToVertex(i) + 12)||
(g->campuses[j].vertexID == regionToVertex(i) + 13) ){ // if a campus lies on either one of the six veritces
if ( (g->campuses[j].campusType > VACANT_VERTEX) && (g->campuses[j].campusType <= CAMPUS_C) ){
// if it is a normal campus
g->players[g->campuses[j].campusType].students[g->regions[i].discipline] ++;
} else if ( (g->campuses[j].campusType > CAMPUS_C) && (g->campuses[j].campusType <= GO8_C) ){
// if it is a group of eight campus
g->players[g->campuses[j].campusType - 3].students[g->regions[i].discipline] += 2;
}
}
}
}
i++;
}
} else{
i = 1;
while (i <= NUM_UNIS){
numConverted = g->players[i].students[STUDENT_MTV] + g->players[i].students[STUDENT_MMONEY];
g->players[i].students[STUDENT_MTV] = 0;
g->players[i].students[STUDENT_MMONEY] = 0;
g->players[i].students[STUDENT_THD] += numConverted;
i++;
}
}
}
[/CODE]

updateStudents is not mentioned or even hinted anywhere else in my program

  • 0

I remove the function declaration then it complains:

'no previos prototype for function 'updateStudents'

AND the error still remains:

nastError2.png

'tis making me tear my hair out!

Can you remove the function declaration and see what happens? It is not invoked anywhere right?

@ surrealvortex: What do you mean by "is it invoked somewhere?"

  • 0

Yeah the no previous prototype thing is OK. Now do the reverse. Let the declaration remain and comment out the definition. What I am trying to find out is if the code surrounding the function is somehow causing a problem. What I meant was, are you calling it anywhere?

  • 0

From the error message the function is defined in both runGame and Game .o files, so is probably also defined in the .c files as well.

Is updateStudents defined in a header file like that then included into both .c files?

If the function definition is in the header it will need an "inline" added to the declaration.

Also do you have include guards in your headers?

Could you give more information on which files the updateStudents prototype and declaration are in.

  • 0
Yeah the no previous prototype thing is OK. Now do the reverse. Let the declaration remain and comment out the definition. What I am trying to find out is if the code surrounding the function is somehow causing a problem. What I meant was, are you calling it anywhere?

At the moment the function is not being called. When I comment out the definition the error disappears. But yeah, then my function won't perform the action that I want it to haha.

You aren't including it twice are you? Also, have you tried GCC? I know it says a linker error, but still, it's worth a shot.

The file which contains the definition and declaration of the file has not been included twice. It comes up with the same error in gcc:


ld: duplicate symbol _updateStudents in /var/folders/wr/4xt1cmpd68x3r6n157qz6rnc0000gn/T//cc7FczcU.o and /var/folders/wr/4xt1cmpd68x3r6n157qz6rnc0000gn/T//ccC6zt9h.o for architecture x86_64
collect2: ld returned 1 exit status
[/CODE]

From the error message the function is defined in both runGame and Game .o files, so is probably also defined in the .c files as well. Is updateStudents defined in a header file like that then included into both .c files? If the function definition is in the header it will need an "inline" added to the declaration. Also do you have include guards in your headers? Could you give more information on which files the updateStudents prototype and declaration are in.

So far my project consists of three files. These are Game.c, Game.h and runGame.c

the runGame.c only includes the Game.c file and contains a main function which returns EXIT_SUCCESS; straight away

the Game.c contains a #include "Game.h", the definitions of the functions in Game.h. The updateStudents function is declared underneath the #include and also defined underneath the declaration. The updateStudents function is not declared in the Game.h file.

Yeah, there is probably duplication in the project hierarchy.

@Sith:What do you mean by duplication in the project hierachy? I have looked at all the files for the project and do not see any duplicate files as show below:

projectFiles.png

  • 0

The .h file should not contain the implementation, and if it does, then it does not need the prototype. If you remove the prototype, then add a semicolon to the end of the implementation.


void method(int param)
{
/* ... */
};
[/CODE]

The more that I think about it, you're almost certainly double-including the code. Put this at the top of all of your *.h files:

[CODE]#pragma once[/CODE]

In older compilers (not your XCode compiler, remotely recent versions of GCC or even Visual Studio), you would use:

[CODE]#ifndef __Game_h__
#define __Game_h__

/* ... all code ... */

#endif[/CODE]

You can use both, but there really is almost no need to use #ifndef for this purpose anymore.

So far my project consists of three files. These are Game.c, Game.h and runGame.c

the runGame.c only includes the Game.c file and contains a main function which returns EXIT_SUCCESS; straight away

the Game.c contains a #include "Game.h", the definitions of the functions in Game.h.

You should never #include a *.c file from another *.c file. Only #include *.h files. The linker will combine the code.

  • 0

You should never #include a *.c file from another *.c file. Only #include *.h files. The linker will combine the code.

THAT has solved the problem!!!!! Thanks so much guys.

Oh and I meant to say that i included the declaration in game.h and the definition in game.c :p Got them mixed up.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.