By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

how to remove "error LNK2005"

P: 52
hello,

my program snippet:

Expand|Select|Wrap|Line Numbers
  1. /*****************************/
  2. /******runThread Function*****/
  3. /*to accept other client connections*/
  4. /*****************************/
  5. DWORD WINAPI runThread(LPVOID param)
  6. {
  7.     SOCKET acceptSocket;    //Socket variable
  8.     HANDLE hThreadC;    //Handle to thread
  9.     DWORD ThreadIdC;    //used to store the thread id
  10.  
  11.     //Parent object (not used anywhere)
  12.     GridServerCThread *ob = (GridServerCThread *)param;
  13.  
  14.     printf( "\n A New Thread for Accepting client connections created.\n");
  15.  
  16.     // Create a socket.
  17.     SOCKET clientSocket;
  18.     clientSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  19.  
  20.     if ( clientSocket == INVALID_SOCKET )
  21.     {
  22.         printf( "\n Error at socket(): %ld\n", WSAGetLastError() );
  23.         WSACleanup();
  24.         return 0;
  25.     }
  26.  
  27.     // Bind the socket.
  28.     sockaddr_in clientService;
  29.  
  30.     clientService.sin_family = AF_INET;
  31.     clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
  32.     clientService.sin_port = htons( 4000 );
  33.  
  34.     if ( bind( clientSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR )
  35.     {
  36.         printf( "\n bind() failed.\n" );
  37.         closesocket(clientSocket);
  38.         return 0;
  39.     }
  40.     else
  41.     {
  42.         // Listen on the socket.
  43.         if ( listen( clientSocket, 1 ) == SOCKET_ERROR )
  44.             printf( "\n Error listening on socket.\n");
  45.         else
  46.         {
  47.             printf( "\n Listening for Client connection......\n" );
  48.  
  49.             // Accept connections.
  50.             while (true)
  51.             {
  52.                 acceptSocket = accept( clientSocket, NULL, NULL );
  53.  
  54.                 if(acceptSocket)
  55.                 {
  56.                     GridServerCThread client_conn_object;
  57.  
  58.                     client_conn_object.c_socket = acceptSocket;
  59.  
  60.                     hThreadC = CreateThread(NULL,
  61.                                             0,
  62.                                             runThreadC,
  63.                                             (LPVOID)&client_conn_object,
  64.                                             0,
  65.                                             &ThreadIdC);
  66.  
  67.                     client_conn_object.hThreadC = hThreadC;
  68.                     client_conn_object.ThreadIdC = ThreadIdC;
  69.  
  70.                 }
  71.                 else
  72.                     return 0;
  73.             }
  74.  
  75.         }
  76.     }
  77. return 0;
  78. }
  79.  
  80. //Constructor, coming from main() to create a
  81. //thread to accept connections from CLIENTS
  82. //for all the actual work.
  83. GridServerCThread :: GridServerCThread()
  84. {
  85.     HANDLE hThread;    //Handle to thread
  86.     DWORD ThreadId;    //used to store the thread id
  87.  
  88.     hThread = CreateThread(    NULL,
  89.                             0,
  90.                             runThread,
  91.                             (LPVOID)this,
  92.                             0,
  93.                             &ThreadId);
  94.  
  95.     this->hThreadC = hThreadC;
  96.     this->ThreadIdC = ThreadIdC;
  97.  
  98. }
  99.  
On compiling the program, I get the error :
Expand|Select|Wrap|Line Numbers
  1. ------ Build started: Project: GridServer, Configuration: Debug Win32 ------
  2. Linking...
  3. GridServerSThread.obj : error LNK2005: "unsigned long __stdcall runThread(void *)" (?runThread@@YGKPAX@Z) already defined in GridServerCThread.obj
  4. E:\SmartSafe\Debug\GridServer.exe : fatal error LNK1169: one or more multiply defined symbols found
  5. Build log was saved at "file://e:\SmartSafe\GridServer\Debug\BuildLog.htm"
  6. GridServer - 2 error(s), 0 warning(s)
  7. ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
  8.  
what should I do??
where is the error, even when I searched MSDN, it told that to get rid of LNK2005 use /FORCE in linker option. I did that, but then my program does not run properly.

Please reply
Pawan
Feb 22 '07 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
You have 2 source files GridServerSThread.c and GridServerCThread.c (note only having a single letter difference in file, function or variable names is poor a standard to follow)?

In both those source files you have a defined a function called runThread without making them static so when the linker tries to link the 2 compiled C files it does not know which function pointer to use.

Try either making these functions static or giving them different names.
Feb 22 '07 #2

P: 52
Thanks, great reply, exactly to the point and 100% useful. It worked for me.
Pawan
Feb 22 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.