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

    • The problem isn't with Epic, it's with the platform holders like Steam and Nintendo, they should be a lot more strict in their review process.
    • Hello, Installed here without issue. Regards, Aryeh Goretsky
    • Microsoft updates Visual Studio Code with easier language model discovery and in-app search by Paul Hill Microsoft has released Visual Studio Code 1.125, its latest weekly release. This week, the company has focused on discovering and installing extra language models via the Marketplace; searching the web and securely browsing over remote connections without leaving VS Code; choosing how long VS Code waits before installing extension updates; and delivering managed Copilot settings through existing device management tooling. In older versions of VS Code, extensions could contribute their own model providers, but to find these extensions, you needed the right tags to search for in the Extension view. Now, the Language Models editor gives you an Install Model Providers button that opens the Extensions view, which is filtered to extensions that contribute model providers, making it easier to find and install them. Once you install a provider, its model will appear in the model picker. If you use the integrated browser much, you can now look up information without leaving VS Code by typing a query into the integrated browser’s address bar. It will use your configured search engine, the same way a standalone browser does. You can use workbench.browser.searchEngine to pick a search engine. When the browser is opened in a remote workspace, it's now possible to proxy HTTP(S) traffic via the remote connection. This allows you to connect to any ports or services that can only be accessed from the remote machine. If you read our coverage from two weeks ago about VS Code 1.123, you might have seen that extension updates have a two-hour delay as a safety measure. In this update, Microsoft is giving you the ability to configure the time of the delay. You can find it under extensions.autoUpdateDelay. Finally, with this update, admins can deliver managed GitHub Copilot settings through native device management (MDM) channels on Windows and macOS, in addition to account-based enterprise settings files. Settings delivered via MDM appear as policy-enforced in VS Code and can’t be overridden locally. Future updates will extend the supported policy keys across Copilot surfaces. You can download the update from the Visual Studio Code website now.
    • "it opens up new doors for people who prefer using Edge, but cannot be bothered to configure a Microsoft account" You already have a Microsoft account if you are using Windows 11, because you can't set it up without one.
  • Recent Achievements

    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
    • One Month Later
      eurospharma62 earned a badge
      One Month Later
    • Week One Done
      With What earned a badge
      Week One Done
    • Week One Done
      Harris Gilbert earned a badge
      Week One Done
    • One Month Later
      Vincian earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      543
    2. 2
      +Edouard
      171
    3. 3
      PsYcHoKiLLa
      84
    4. 4
      ATLien_0
      64
    5. 5
      neufuse
      64
  • Tell a friend

    Love Neowin? Tell a friend!