Home

Forums

Web development

 

 

 

 
     
 
dna88 Web development and Technology Forum
 
Profile   Register   Memberlist   Usergroups   FAQ   Search  Log in
VC++ runtime error, unresolved external symbol

 
Post new topic   Reply to topic    dna88 Forum Index -> Programming in Java, C, C#, VB, .NET Discussion Forum
Author Message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: VC++ runtime error, unresolved external symbol Reply with quote

#include <stdio.h>
#include <winsock.h>
#include <inetreg.h>
#include <inetsdk.h>
#include <time.h>
//#include <unistd.h>
#define MAX_BUFFER 128
#define DAYTIME_SERVER_PORT 13


Im using winsock.h instead of including

#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

since I am using Visual Studio.
It compiles well, but When i try to
execute, I get unresolved external errors.
Here they are below

Linking...
Server.obj : error LNK2001: unresolved external symbol _snprintf
Server.obj : error LNK2001: unresolved external symbol _accept@12
Server.obj : error LNK2001: unresolved external symbol _listen@8
Server.obj : error LNK2001: unresolved external symbol _bind@12
Server.obj : error LNK2001: unresolved external symbol _htons@4
Server.obj : error LNK2001: unresolved external symbol _htonl@4
00030: Server.obj : error LNK2001: unresolved external symbol _socket@12
Debug/Server.exe : fatal error LNK1120: 7 unresolved externals
Error executing link.exe.

Server.exe - 8 error(s), 0 warning(s)


And here is the complete code for your checking.
Quote:
int main()
{

00050: int serverFd, connectionFd;
struct sockaddr_in servaddr;
char timebuffer[MAX_BUFFER+1];


time_t currentTime;

serverFd = socket(AF_INET,SOCK_STREAM,0);
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
00060: servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(DAYTIME_SERVER_PORT);

bind(serverFd,(struct sockaddr *)&servaddr,sizeof(servaddr));

listen(serverFd,5);

while(1){

connectionFd = accept(serverFd,(struct sockaddr *)NULL,NULL);
00070:
if(connectionFd >= 0 ){
currentTime = time(NULL);
snprintf(timebuffer,MAX_BUFFER, "%s\n", ctime(¤tTime));

write(connectionFd,timebuffer,strlen(timebuffer));

close(connectionFd);

}
00080:

}


}

_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Mon Jun 07, 04 6:21 pm
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
dinangkur
Super Moderator
Super Moderator


Joined: 24 Mar 2004
Posts: 491
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

The reason you're getting this is you didn't add correct lib for API snprintf, bind and others. Those are part of POSIX C and part of following header.

#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

You've to find similier API from winsock.h to work with it.

-DK.
_________________
...we too are stardust...
Tue Jun 08, 04 12:50 am
Back to top
dinangkur View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Tousif
Beginner User
Beginner User


Joined: 25 May 2004
Posts: 20
Location: Nashville, Tennessee, USA

Post Post subject: Reply with quote

DK:
But do you think if this was due to those function not being in the API, he would have been able to compile this program to begin with? I didn't have time to check others but I've checked one of them and "bind" exist under winsock.h. He shouldn't have gotten that error at least.

To me, it seems more like a linking issue than anything else. Are you sure quantum, you have all the linking done correctly?

Tousif
Tue Jun 08, 04 3:26 am
Back to top
Tousif View user's profile Send private message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

The code compiled alright, without any error.

I am more or less sure the linking is okay. Could you please check the code your machine?
_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Tue Jun 08, 04 6:40 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
dinangkur
Super Moderator
Super Moderator


Joined: 24 Mar 2004
Posts: 491
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Sorry for the mistake Mr. Tousif. I never did any works with winsock, so don't have any deep knowledge about it. It seems to me it's linking error. When it's try to create .exe file, it fails to link with the libs.

I ran the program:

I've got 13 errors and 1 warning.

Code:

--------------------Configuration: test1 - Win32 Debug--------------------
Compiling...
test.cpp
e:\prog_b\cforum\test1\test.cpp(13) : error C2143: syntax error : missing ';' before ':'
e:\prog_b\cforum\test1\test.cpp(20) : error C2065: 'serverFd' : undeclared identifier
e:\prog_b\cforum\test1\test.cpp(23) : error C2143: syntax error : missing ';' before ':'
e:\prog_b\cforum\test1\test.cpp(32) : error C2065: 'connectionFd' : undeclared identifier
e:\prog_b\cforum\test1\test.cpp(33) : error C2143: syntax error : missing ';' before ':'
e:\prog_b\cforum\test1\test.cpp(34) : error C2143: syntax error : missing ';' before '{'
e:\prog_b\cforum\test1\test.cpp(36) : error C2065: 'snprintf' : undeclared identifier
e:\prog_b\cforum\test1\test.cpp(36) : error C2018: unknown character '0xa4'
e:\prog_b\cforum\test1\test.cpp(36) : error C2065: 'tTime' : undeclared identifier
e:\prog_b\cforum\test1\test.cpp(38) : error C2065: 'write' : undeclared identifier
e:\prog_b\cforum\test1\test.cpp(40) : error C2065: 'close' : undeclared identifier
e:\prog_b\cforum\test1\test.cpp(43) : error C2041: illegal digit '8' for base '8'
e:\prog_b\cforum\test1\test.cpp(43) : error C2143: syntax error : missing ';' before ':'
e:\prog_b\cforum\test1\test.cpp(48) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

test1.exe - 13 error(s), 1 warning(s)


I changed few things, and following:
Code:

#include <stdio.h>
#include <winsock.h>
#include <inetreg.h>
#include <inetsdk.h>
#include <time.h>
//#include <unistd.h>
#define MAX_BUFFER 128
#define DAYTIME_SERVER_PORT 13


int main()
{

int serverFd, connectionFd;
struct sockaddr_in servaddr;
char timebuffer[MAX_BUFFER+1];


time_t currentTime;

serverFd = socket(AF_INET,SOCK_STREAM,0);
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(DAYTIME_SERVER_PORT);

bind(serverFd,(struct sockaddr *)&servaddr,sizeof(servaddr));

listen(serverFd,5);

while(1){

connectionFd = accept(serverFd,(struct sockaddr *)NULL,NULL);
 
if(connectionFd >= 0 ){
currentTime = time(NULL);
snprintf(timebuffer,MAX_BUFFER, "%s\n", ctime(tTime));

write(connectionFd,timebuffer,strlen(timebuffer));

close(connectionFd);

}


}

return 0;

}


Getting following errors:
Code:

E:\Prog_B\Cforum\test1\test.cpp(36) : error C2065: 'snprintf' : undeclared identifier
E:\Prog_B\Cforum\test1\test.cpp(36) : error C2065: 'tTime' : undeclared identifier
E:\Prog_B\Cforum\test1\test.cpp(38) : error C2065: 'write' : undeclared identifier
E:\Prog_B\Cforum\test1\test.cpp(40) : error C2065: 'close' : undeclared identifier
Error executing cl.exe.

test1.exe - 4 error(s), 0 warning(s)


I think if you fix those undeclared indetifier, it will work.

-DK.
_________________
...we too are stardust...
Tue Jun 08, 04 1:58 pm
Back to top
dinangkur View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Tousif
Beginner User
Beginner User


Joined: 25 May 2004
Posts: 20
Location: Nashville, Tennessee, USA

Post Post subject: Reply with quote

Hi Guys,
I've changed it to the following and it compiles and runs..at least on my machine.

Here is the code:
Code:


#include <stdio.h>
#include <winsock.h>
#include <inetreg.h>
#include <inetsdk.h>
#include <time.h>
//#include <unistd.h>
#define MAX_BUFFER 128
#define DAYTIME_SERVER_PORT 13

#define snprintf   _snprintf

 int main()
{

int serverFd, connectionFd;
struct sockaddr_in servaddr;
char timebuffer[MAX_BUFFER+1];

time_t currentTime;

serverFd = socket(AF_INET,SOCK_STREAM,0);
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(DAYTIME_SERVER_PORT);

bind(serverFd,(struct sockaddr *)&servaddr,sizeof(servaddr));

listen(serverFd,5);

while(1){

connectionFd = accept(serverFd,(struct sockaddr *)NULL,NULL);

if(connectionFd >= 0 ){
currentTime = time(NULL);
snprintf(timebuffer,MAX_BUFFER, "%s\n", ctime(&currentTime));

send(connectionFd,timebuffer,strlen(timebuffer), MSG_DONTROUTE);

closesocket(connectionFd);

}

}


}




Now,

under windows, snprinf is defined as _snprintf. So that was one reason and as a general practice, I've added the #def. Also you don't have tTime declared but have used it in ctime, so I've used currentTime. Change it to your linking.

You should not treat a socket just like a normal file and thus shouldn't be applying write() and close() on a socket. Instead use send() and closesocket(). You might wanna use sendto() instead of send() depending on your need but the point is look it up in the winsock API and use the appropriate function.

I hope this will run on your machine.


Tousif
Wed Jun 09, 04 2:56 am
Back to top
Tousif View user's profile Send private message
Tousif
Beginner User
Beginner User


Joined: 25 May 2004
Posts: 20
Location: Nashville, Tennessee, USA

Post Post subject: Reply with quote

One more thing, I had to manually add the linking library wsock32.lib . To do this, in Visual C++, go to project->settings, click on the link tab and then under object/library modules, add that library name at the end. remember to keep each library module seperate:)

If you are using 16 bit application, add winsock.h instead.

Tousif
Wed Jun 09, 04 3:06 am
Back to top
Tousif View user's profile Send private message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Thanks Tousif. I really appreciate that. Will come back to this tomorrow. Gentle smile
_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Thu Jun 10, 04 1:15 pm
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
Guest






Post Post subject: Reply with quote

Soory, I am little late. Got caught up.

After fixing a few minor syntex error your code compiled as is:
Code:
#include <stdio.h>
#include <winsock.h>
#include <inetreg.h>
#include <inetsdk.h>
#include <time.h>
//#include <unistd.h>
#define MAX_BUFFER 128
#define DAYTIME_SERVER_PORT 13

#define snprintf   _snprintf

 int main1()
{

int serverFd, connectionFd;
struct sockaddr_in servaddr;
char timebuffer[MAX_BUFFER+1];

time_t currentTime;

serverFd = socket(AF_INET,SOCK_STREAM,0);
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(DAYTIME_SERVER_PORT);

bind(serverFd,(struct sockaddr *)&servaddr,sizeof(servaddr));

listen(serverFd,5);

while(1){

connectionFd = accept(serverFd,(struct sockaddr *)NULL,NULL);

if(connectionFd >= 0 ){
currentTime = time(NULL);
snprintf(timebuffer,MAX_BUFFER, "%s\n", ctime(&currentTime));

send(connectionFd,timebuffer,strlen(timebuffer), MSG_DONTROUTE);

closesocket(connectionFd);

}

}


}


I manually added the winsock32.lib and I get the following error now.

--------------------Configuration: try - Win32 Debug--------------------
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/try.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

try.exe - 2 error(s), 0 warning(s)

Now bear with me. I am NOT a VC++ guy. So treat me as a novice. Gentle smile

Quantum
Mon Jun 14, 04 12:53 am
Back to top
Guest
Tousif
Beginner User
Beginner User


Joined: 25 May 2004
Posts: 20
Location: Nashville, Tennessee, USA

Post Post subject: Reply with quote

Hi,
Well..first of all I am not a fan of $M :) So, if there are alternatives I would not program in VC++.I use it only when I must and not an expert in it.

Anyways..as it looked to me it doesn't know "winmain", so basically you are running a program that takes the windows application main program "winmain" as default instead normal "main".

I was able to produce your error...I guess when you've started this in File->New->Project, you've selected it as Win32 Application or something that requires "winmain".

What I did was just a simple Win32 Console application..ones that take "main" as the default starting point.

This should solve your problem.

Tousif
Mon Jun 14, 04 1:24 am
Back to top
Tousif View user's profile Send private message
dinangkur
Super Moderator
Super Moderator


Joined: 24 Mar 2004
Posts: 491
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Yes Mr. Tousif you're right.

-DK.
_________________
...we too are stardust...
Mon Jun 14, 04 1:51 am
Back to top
dinangkur View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Very nice...that was it.

The main problem that needed to be solved here was adding the wsock32.lib in the link library as you said. And turning it into console mode did the job. Thanks.

Yeah, I agree with your opinion about M$ VC++. I am turning to borland from now on for windows.
_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Mon Jun 14, 04 1:53 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
Display posts from previous:   
Post new topic   Reply to topic    dna88 Forum Index -> Programming in Java, C, C#, VB, .NET Discussion Forum All times are GMT - 7 Hours
Page 1 of 1

 

Partners and Resources

Bangladesh hosting company

Bangladesh web design

Driven by phpBB © phpBB Group