error LNK2005:_**** already defined in main.obj

This is a discussion on error LNK2005:_**** already defined in main.obj within the C Programming forums, part of the General Programming Boards category; So I have this sample main.c file: Code: #include <stdio.h> #include <stdlib.h> #include <time.h> void shuffle (int wDeck[][13]); void deal ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    14

    error LNK2005:_**** already defined in main.obj

    So I have this sample main.c file:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle (int wDeck[][13]);
    void deal (const int wDeck[][13], const char *wFace[], const char *wSuit[]);
    
    int main (void)
    {
    	/* initialize suit array */
    	const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    
    	/* initialize face array */
    	const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Size", "Seven", "Eight",
    		"Nine", "Ten", "Jack", "Queen", "King"};
    
    	/* initalize deck array */
    	int deck[4][13] = {0};
    
    	srand ((unsigned) time (NULL)); /* see random-number generator */
    
    	shuffle (deck);
    	deal (deck, face, suit);
    
    	return 0;
    }
    
    /* shuffle cards in deck */
    
    void shuffle (int wDeck[][13])
    {
    	int row = 0;    /* row number */
    	int column = 0; /*column number */
    	int card = 0;   /* card counter */
    
    	/* for each of the 52 cards, choose slot of deck randomly */
    	for (card = 1; card <= 52; card++)
    	{
    		/* choose new random location until unoccupied slot found */
    		do
    		{
    			row = rand () % 4;
    			column = rand () % 13;
    		} while (wDeck[row][column] != 0);
    
    		/* place card number in chosen slot of deck */
    		wDeck[row][column] = card;
    	}
    }
    
    /* deal cards in deck */
    void deal (const int wDeck[][13], const char *wFace[], const char *wSuit[])
    {
    	int row = 0;    /* row number */
    	int column = 0; /*column number */
    	int card = 0;   /* card counter */
     
    	/* deal each of the 52 cards */
    	for (card = 1; card <= 52; card++)
    	{
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 12; column++)
    			{
    				/* if slot contains current card, display card */
    				if (wDeck[row][column] == card)
    				{
    					printf ("%5s of %-8s%c", wFace[column], wSuit[row], card % 2 == 0 ? '\n' : '\t');
    				}
    			}
    		}
    	}
    }
    I'm trying to clean it up by splitting it into a header file and two source files, but when I do, I get three errors stating
    "error LNK2005:_deal already defined in main.obj"
    "error LNK2005:_shuffle already defined in main.obj"
    "fatal error LNK1169: one or more multiply defined symbols found"

    I can't figure out where the mistake is. The modified files look like this:

    poker.h
    Code:
    #ifndef POKER_H
    #define POKER_H
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle (int wDeck[][13]);
    void deal (const int wDeck[][13], const char *wFace[], const char *wSuit[]);
    
    #endif
    poker.c
    Code:
    #include "poker.h"
    
    /* shuffle cards in deck */
    
    void shuffle (int wDeck[][13])
    {
    	int row = 0;    /* row number */
    	int column = 0; /*column number */
    	int card = 0;   /* card counter */
    
    	/* for each of the 52 cards, choose slot of deck randomly */
    	for (card = 1; card <= 52; card++)
    	{
    		/* choose new random location until unoccupied slot found */
    		do
    		{
    			row = rand () % 4;
    			column = rand () % 13;
    		} while (wDeck[row][column] != 0);
    
    		/* place card number in chosen slot of deck */
    		wDeck[row][column] = card;
    	}
    }
    
    /* deal cards in deck */
    void deal (const int wDeck[][13], const char *wFace[], const char *wSuit[])
    {
    	int row = 0;    /* row number */
    	int column = 0; /*column number */
    	int card = 0;   /* card counter */
     
    	/* deal each of the 52 cards */
    	for (card = 1; card <= 52; card++)
    	{
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 12; column++)
    			{
    				/* if slot contains current card, display card */
    				if (wDeck[row][column] == card)
    				{
    					printf ("%5s of %-8s%c", wFace[column], wSuit[row], card % 2 == 0 ? '\n' : '\t');
    				}
    			}
    		}
    	}
    }
    main.c:
    Code:
    #include "poker.h"
    
    
    int main (void)
    {
    	/* initialize suit array */
    	const char suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    
    	/* initialize face array */
    	const char face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Size", "Seven", "Eight",
    		"Nine", "Ten", "Jack", "Queen", "King"};
    
    	/* initalize deck array */
    	int deck[4][13] = {0};
    
    	srand ((unsigned) time (NULL)); /* see random-number generator */
    
    	shuffle (deck);
    	deal (deck, face, suit);
    
    	return 0;
    }
    Can anybody show me where my mistake is?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You may want to do a build-clean, since those symbols used to be defined in main.obj (although they shouldn't be any more).

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    175
    You might want to try and clean up the "sample main.c" file. When I compiled it on a gcc compiler, I got the following errors:

    Line 23: warning, passing argument 1 of 'deal' from incompatible pointer type
    Line 6: note, expected 'const int (*)[13]' but argument is of type 'int (*)[13]'

    Also when I ran the program, I got some of the output to be "Size of Spades" and "Size of Hearts"? Not sure this is correct.
    Last edited by csharp100; 12-13-2010 at 08:24 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Whoa. The clean-build gave me a whole different set of errors. For lines 7 and 10 in main, that is:
    Code:
    	const char suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    
    	/* initialize face array */
    	const char face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Size", "Seven", "Eight",
    		"Nine", "Ten", "Jack", "Queen", "King"};
    Both lines give the error:
    "error C2078: too many initializers"

    Edit:
    I should comment that I'm using Visual Studio '08.
    Last edited by DenJansen; 12-13-2010 at 08:38 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Those should both be
    Code:
    const char * suit[4]
    const char * face[13]

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Quote Originally Posted by csharp100 View Post
    You might want to try and clean up the "sample main.c" file. When I compiled it on a gcc compiler, I got the following errors:

    Line 23: warning, passing argument 1 of 'deal' from incompatible pointer type
    Line 6: note, expected 'const int (*)[13]' but argument is of type 'int (*)[13]'

    Also when I ran the program, I got some of the output to be "Size of Spades" and "Size of Hearts"? Not sure this is correct.
    I ran the sample main.c in Visual Studio and it ran error free, so I think the original is coded right; it's just whatever I'm trying to do to it that's failing.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Quote Originally Posted by tabstop View Post
    Those should both be
    Code:
    const char * suit[4]
    const char * face[13]
    Bingo, that was it. Somehow I ended up changing those between the original and the new files. I don't even remember doing that.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 01:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 06:16 AM
  5. the effects of textures on my frame rate
    By DavidP in forum Game Programming
    Replies: 37
    Last Post: 10-03-2003, 11:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21