• 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 Vibe Coding Playbook: Building Your Tech Business with AI —was $35, now FREE by Steven Parker Claim your complimentary copy (worth $35) of "The Vibe Coding Playbook: Building Your Tech Business with AI" for free, before the offer ends on June 23. Description A detailed and up-to-date walkthrough for entrepreneurs with limited (or non-existent) coding skills who want to build profitable software companies using new gen-AI tools. In The Vibe Coding Playbook: Building Your Tech Business With AI, renowned AI and data science educator Siraj Raval walks you through exactly what you need to do to build a technology business with generative AI-powered code assistants. Raval offers step-by-step guidance for non-technical professionals and entrepreneurs interested in creating scalable, profitable enterprises without spending years learning how to code. This book conceives of new artificial intelligence tools, like Cursor, as “co-founders,” lighting your way to constructing valuable software products and services. You’ll learn to build minimally viable products (MVPs), iterate on your software products as you develop and after launch, and grow your company while maintaining a lean, efficient, solopreneur-focused structure. Inside the book: Detailed guidance for entrepreneurs interested in creating powerful tech solutions for niche problems and markets without hiring expensive software developers Strategies for using generative AI tools to substitute for traditional technical co-founders Illustrative case studies from real-world founders who built successful technology businesses without learning to code Useful tools for non-technical entrepreneurs, including prompt libraries, decision trees, QR codes linking to video tutorials demonstrating key techniques, and access to an exclusive online community of like-minded founders Perfect for ambitious professionals and entrepreneurs who want to build a successful technology company now – using commercially available AI tools – The Vibe Coding Playbook is your personal roadmap to creating useful and profitable software for customers without learning how to code. How to download for free Please ensure you read the terms and conditions to claim this offer. Complete and verifiable information is required in order to receive this free offer. If you have previously made use of these offers, you will not need to re-register. Was $35, but is now FREE | Below free offer link expires on June 23. The Vibe Coding Playbook: Building Your Tech Business with AI The below offers are also available for free in exchange for your (work) email: The Vibe Coding Playbook: Building Your Tech Business with AI ($35 Value) FREE - Expires 6/23 The Persuasion Engine: How Any Business Can Use AI-Powered Neuromarketing to Understand and Win Customers ($28 Value) FREE - Expires 6/24 How to Do More with Less: Future-Proofing Yourself in an AI-driven Economy ($28 Value) FREE - Expires 6/30 Cloud Security Fundamentals: Building the Foundations for Secure Cloud Platforms ($131.95 Value) FREE - Expires 7/1 The Complete Free AI Learning: Master ChatGPT, Claude, Gemini & More ($21 Value) FREE How to Build an AI Design Workflow with Gamma ($21 Value) FREE The Ultimate Linux Newbie Guide – Featured Free content Python Notes for Professionals – Featured Free content Learn Linux in 5 Days – Featured Free content Quick Reference Guide for Cybersecurity – Featured Free content We post these because we earn commission on each lead so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin The above deal not doing it for you, but still want to help? Check out the links below. Check out our partner software in the Neowin Store Buy a T-shirt at Neowin's Threadsquad Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: An account at Neowin Deals is required to participate in any deals powered by our affiliate, StackCommerce. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through the branded deals site.
    • Rockstar confirms Grand Theft Auto VI pre-orders begin next week, unveils cover art by Pulasthi Ariyasinghe The release date of Grand Theft Auto VI has moved quite a lot since its original announcement in 2023, but it finally looks like the game has found its final launch slot. Rockstar today had a new video upload on its YouTube channel, and while it wasn't a new trailer for the game, the company revealed two things. This was the pre-order kickoff date for Grand Theft Auto VI as well as the game's official cover art. The company revealed that June 25 is when fans of the series will be able to pre-order their copy of Grand Theft Auto VI. Pre-orders will be available both digitally and in retail stores. The newly unveiled cover art shows off the two new protagonists, as well as a few more characters that are probably vital to the campaign storyline. Shots of vehicles players can use like a light helicopter, motorcycle, sports car, and speed boat are also seen here, alongside a shot of a crocodile. "Jason and Lucia have always known the deck is stacked against them," says Rockstar describing the campaign's protagonist duo. "But when an easy score goes wrong, they find themselves on the darkest side of the sunniest place in America, in the middle of a conspiracy stretching across the state of Leonida — forced to rely on each other more than ever if they want to make it out alive." Grand Theft Auto VI is coming to Xbox Series X|S and PlayStation 5 on November 19, 2026. A PC version has not been confirmed yet, though it's expected by many to land after the console release. When asked about this, the Take-Two CEO says it considers the core audience for the Grand Theft Auto franchise to be on consoles.
  • Recent Achievements

    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • 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
  • Popular Contributors

    1. 1
      +primortal
      552
    2. 2
      +Edouard
      169
    3. 3
      PsYcHoKiLLa
      72
    4. 4
      Michael Scrip
      64
    5. 5
      ATLien_0
      64
  • Tell a friend

    Love Neowin? Tell a friend!