有网友问到,他在链接的时候,产生一堆错误
我们还是先看他的代码吧.
1
// acetest2.cpp : Defines the entry point for the console application.
2
//
3
4
// client.cpp
5
6
#include "ace/OS_main.h"
7
#include "ace/OS_NS_string.h"
8
#include "ace/OS_NS_sys_socket.h"
9
#include "ace/OS_NS_unistd.h"
10
#include "ace/OS_NS_stdlib.h"
11
#include "ace/OS_NS_stdio.h"
12
#include "ace/OS_NS_netdb.h"
13
#include "ace/Default_Constants.h"
14
15
ACE_RCSID(SOCK_SAP, client, "client.cpp,v 4.10 2005/01/21 02:19:20 ossama Exp")
16
17
/**//* BSD socket client */
18
19
int
20
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
21

{
22
// Initialize WinSock DLL on Win32
23
ACE_OS::socket_init (ACE_WSOCK_VERSION);
24
25
struct sockaddr_in saddr;
26
struct hostent *hp;
27
const ACE_TCHAR *host = argc > 1 ? argv[1] : ACE_DEFAULT_SERVER_HOST;
28
u_short port_num =
29
htons (argc > 2 ? ACE_OS::atoi (argv[2]) : ACE_DEFAULT_SERVER_PORT);
30
int sockbufsize = argc > 3 ? ACE_OS::atoi (argv[3]) : 0;
31
char buf[BUFSIZ];
32
ACE_HANDLE s_handle;
33
int w_bytes;
34
int r_bytes;
35
int n;
36
37
// Create a local endpoint of communication.
38
if ((s_handle = ACE_OS::socket (PF_INET, SOCK_STREAM, 0)) == ACE_INVALID_HANDLE)
39
ACE_OS::perror (ACE_TEXT("socket")), ACE_OS::exit (1);
40
41
// If a sockbufsize was specified, set it for both send and receive.
42
if (sockbufsize > 0)
43
{
44
if (ACE_OS::setsockopt (s_handle, SOL_SOCKET, SO_SNDBUF,
45
(const char *) &sockbufsize,
46
sizeof (sockbufsize)) != 0)
47
ACE_OS::perror (ACE_TEXT("SO_SNDBUF")), ACE_OS::exit (1);
48
if (ACE_OS::setsockopt (s_handle, SOL_SOCKET, SO_RCVBUF,
49
(const char *) &sockbufsize,
50
sizeof (sockbufsize)) != 0)
51
ACE_OS::perror (ACE_TEXT("SO_RCVBUF")), ACE_OS::exit (1);
52
}
53
54
// Determine IP address of the server.
55
if ((hp = ACE_OS::gethostbyname (ACE_TEXT_ALWAYS_CHAR(host))) == 0)
56
ACE_OS::perror (ACE_TEXT("gethostbyname")), ACE_OS::exit (1);
57
58
// Set up the address information to contact the server.
59
ACE_OS::memset ((void *) &saddr, 0, sizeof saddr);
60
saddr.sin_family = AF_INET;
61
saddr.sin_port = port_num;
62
ACE_OS::memcpy (&saddr.sin_addr, hp->h_addr, hp->h_length);
63
64
// Establish connection with remote server.
65
if (ACE_OS::connect (s_handle,
66
reinterpret_cast<sockaddr *> (&saddr),
67
sizeof saddr) == -1)
68
ACE_OS::perror (ACE_TEXT("connect")), ACE_OS::exit (1);
69
70
// Send data to server (correctly handles "incomplete writes" due to
71
// flow control).
72
73
while ((r_bytes = ACE_OS::read (ACE_STDIN, buf, sizeof buf)) > 0)
74
for (w_bytes = 0; w_bytes < r_bytes; w_bytes += n)
75
if ((n = ACE_OS::send (s_handle, buf + w_bytes,
76
r_bytes - w_bytes)) < 0)
77
ACE_OS::perror (ACE_TEXT("write")), ACE_OS::exit (1);
78
79
if (ACE_OS::recv (s_handle, buf, 1) == 1)
80
ACE_OS::write (ACE_STDOUT, buf, 1);
81
82
// Explicitly close the connection.
83
if (ACE_OS::closesocket (s_handle) == -1)
84
ACE_OS::perror (ACE_TEXT("close")), ACE_OS::exit (1);
85
return 0;
86
}
87
88
89
直接编译,产生如下错误
------ Build started: Project: acetest2, Configuration: Debug Win32 ------

Compiling
acetest2.cpp
Linking
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ace_os_main_i(class ACE_Main_Base &,int,char * * const)" (__imp_?ace_os_main_i@@YAHAAVACE_Main_Base@@HQAPAD@Z) referenced in function _main
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall ACE_Main_Base::ACE_Main_Base(void)" (__imp_??0ACE_Main_Base@@QAE@XZ) referenced in function "public: __thiscall ACE_Main::ACE_Main(void)" (??0ACE_Main@@QAE@XZ)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ACE_OS::closesocket(void *)" (__imp_?closesocket@ACE_OS@@YAHPAX@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ACE_OS::write(void *,void const *,unsigned int)" (__imp_?write@ACE_OS@@YAHPAXPBXI@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ACE_OS::recv(void *,char *,unsigned int,int)" (__imp_?recv@ACE_OS@@YAHPAXPADIH@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ACE_OS::send(void *,char const *,unsigned int,int)" (__imp_?send@ACE_OS@@YAHPAXPBDIH@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ACE_OS::read(void *,void *,unsigned int)" (__imp_?read@ACE_OS@@YAHPAX0I@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ACE_OS::connect(void *,struct sockaddr *,int)" (__imp_?connect@ACE_OS@@YAHPAXPAUsockaddr@@H@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void * __cdecl ACE_OS::memcpy(void *,void const *,unsigned int)" (__imp_?memcpy@ACE_OS@@YAPAXPAXPBXI@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void * __cdecl ACE_OS::memset(void *,int,unsigned int)" (__imp_?memset@ACE_OS@@YAPAXPAXHI@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) struct hostent * __cdecl ACE_OS::gethostbyname(char const *)" (__imp_?gethostbyname@ACE_OS@@YAPAUhostent@@PBD@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ACE_OS::setsockopt(void *,int,int,char const *,int)" (__imp_?setsockopt@ACE_OS@@YAHPAXHHPBDH@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl ACE_OS::exit(int)" (__imp_?exit@ACE_OS@@YAXH@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl ACE_OS::perror(char const *)" (__imp_?perror@ACE_OS@@YAXPBD@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void * __cdecl ACE_OS::socket(int,int,int)" (__imp_?socket@ACE_OS@@YAPAXHHH@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ACE_OS::atoi(char const *)" (__imp_?atoi@ACE_OS@@YAHPBD@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
acetest2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ACE_OS::socket_init(int,int)" (__imp_?socket_init@ACE_OS@@YAHHH@Z) referenced in function "int __cdecl ace_main_i(int,char * * const)" (?ace_main_i@@YAHHQAPAD@Z)
Debug/acetest2.exe : fatal error LNK1120: 17 unresolved externals解决方法,是正确的链接ace(x).lib
其中, release版对应的是ace.lib,debug对应的是aced.lib
同时,还要确保编辑器能正确找到ace(x).lib的位置.
简单的办法是加入下面的代码
#ifdef _DEBUG
#pragma comment (lib,"aced.lib")
#else
#pragma comment (lib,"ace.lib")
#endif
