(转)c++ libcurl一步一步问题解决

最近在研究openvpn,已经部署好服务器和客户端了。想对代码进行研究,故想跟踪调试openvpn代码看看。
下载openvpn最新版本,目前稳定版本是openvpn-2.0.9    http://openvpn.net/ 

我的机器环境是:vc6.0 + vc2003 platform_sdk
下载以后解压,main函数在openvpn.c文件中,vc6打开这文件,将其它*.h *.c文件全部加入到工程中,编译运行,报一大堆错:

include\winsock2.h(109) : error C2011: 'fd_set' : 'struct' type redefinition
include\winsock2.h(144) : warning C4005: 'FD_SET' : macro redefinition
include\winsock.h(88) : see previous definition of 'FD_SET'
include\winsock2.h(153) : error C2011: 'timeval' : 'struct' type redefinition
include\winsock2.h(209) : error C2011: 'hostent' : 'struct' type redefinition
include\winsock2.h(222) : error C2011: 'netent' : 'struct' type redefinition
include\winsock2.h(229) : error C2011: 'servent' : 'struct' type redefinition
include\winsock2.h(241) : error C2011: 'protoent' : 'struct' type redefinition
include\winsock2.h(397) : error C2011: 'sockaddr_in' : 'struct' type redefinition
include\winsock2.h(407) : error C2011: 'WSAData' : 'struct' type redefinition
.................................


这是由于<config-win32.h>文件中包含了

#include <windows.h>
#include <winsock2.h>

windows.h中又包含了<winsock.h>导致和winsock2.h冲突,解决方法是在include<windows.h>之前添加宏:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>

因为用到了winsock2.h 所以必须加入lib文件:ws2_32.lib 
再次编译,出现如下错误:

mudp.c
this compiler appears to lack vararg macros which will cause a significant degradation in efficiency
e:\libtools\openvpn-2.0.9\lzo.h(32) : fatal error C1083: Cannot open include file: 'lzo1x.h': No such file or directory
multi.c
this compiler appears to lack vararg macros which will cause a significant degradation in efficiency
e:\libtools\openvpn-2.0.9\lzo.h(32) : fatal error C1083: Cannot open include file: 'lzo1x.h': No such file or directory
ntlm.c
this compiler appears to lack vararg macros which will cause a significant degradation in efficiency
occ.c
this compiler appears to lack vararg macros which will cause a significant degradation in efficiency
e:\libtools\openvpn-2.0.9\lzo.h(32) : fatal error C1083: Cannot open include file: 'lzo1x.h': No such file or directory
openvpn.c

 

#ifdef USE_LZO

#ifdef LZO_HEADER_DIR
#include "lzo/lzoutil.h"
#include "lzo/lzo1x.h"
#else
#include "lzoutil.h"
#include "lzo1x.h"
#endif


因为openvpn默认配置用到了lzo压缩库,所以下载lzo压缩包:lzo-2.03,并且将lzo-2.03包中的include文件加入到工程中,注意文件目录层次,我是额外定义了lzo文件夹,将头文件全部加入进去的,因此必须加上编译选项:LZO_HEADER_DIR
另额外编译lzo lib:
    将lzo解压至C盘根目录,在命令行中跳至该目录,运行b\win32\mc120.bat,编译成功后生成lzo.lib
将生成的lzo.lib添加到工程中。
另如果不是用lzo可以修改<config_win32.h>头文件: (openssl ssl crypto也可以不使用,视具体情况)

/* Use OpenSSL crypto library */
#define USE_CRYPTO 1

/* Use LZO compression library */
#define USE_LZO 1

/* Use OpenSSL SSL library */
#define USE_SSL 1

/* Version number of package */
#define VERSION PACKAGE_VERSION



接着如果使用OpenSSL,那么首先要下载OpenSSL库,然后编译:
    a.将OpenSSL解压到C盘根目录下;
    b.在命令提示行中进入该目录,输入perl Configure VC-WIN32,检验perl是否正确安装;
    c.命令行中输入ms\do_ms;
    d.命令行中转到Microsoft Visual Studio\VC98\bin,运行vcvars32.bat以配置环境变量。
    e.命令行OpenSSL目录下,执行nmake –f ms\ntdll.mak 编译成功后,在out32dll目录下,会有四个文件libeay32.dll libeay32.lib ssleay32.dll和 ssleay32.lib,将编译成功的lzo.lib libeay32.lib ssleay32.lib添加到工程中,并将libeay32.dll ssleay32.dll拷贝到Debug目录下,并且加入openssl的头文件到工程中,和lzo库类似的过程。

另外因为VC中不识别unsigned long long int类型,需改为_int64。修改<common.h>头文件: 

#ifdef USE_64_BIT_COUNTERS
//  typedef unsigned long long int counter_type;
typedef _int64 counter_type;  //qj modify

# ifdef WIN32
#  define counter_format  "%I64u"
# else
#  define counter_format  "%llu"
# endif
#else
  typedef unsigned int counter_type;
# define counter_format   "%u"
#endif


再次编译,发现错误少多了,而且全部只是一些link错误了:

Linking
LINK : warning LNK4098: defaultlib "MSVCRT" conflicts with use of other libs; use /NODEFAULTLIB:library
cryptoapi.obj : error LNK2001: unresolved external symbol __imp__CertFreeCertificateContext@4
cryptoapi.obj : error LNK2001: unresolved external symbol __imp__CryptAcquireCertificatePrivateKey@24
cryptoapi.obj : error LNK2001: unresolved external symbol __imp__CertCloseStore@8
cryptoapi.obj : error LNK2001: unresolved external symbol __imp__CertOpenStore@20
cryptoapi.obj : error LNK2001: unresolved external symbol __imp__CertFindCertificateInStore@24
route.obj : error LNK2001: unresolved external symbol _GetIpForwardTable@12
route.obj : error LNK2001: unresolved external symbol _CreateIpForwardEntry@4
route.obj : error LNK2001: unresolved external symbol _DeleteIpForwardEntry@4
tun.obj : error LNK2001: unresolved external symbol _GetAdaptersInfo@8
tun.obj : error LNK2001: unresolved external symbol _IpReleaseAddress@4
tun.obj : error LNK2001: unresolved external symbol _GetInterfaceInfo@8
tun.obj : error LNK2001: unresolved external symbol _IpRenewAddress@4
tun.obj : error LNK2001: unresolved external symbol _AddIPAddress@20
tun.obj : error LNK2001: unresolved external symbol _FlushIpNetTable@4
tun.obj : error LNK2001: unresolved external symbol _DeleteIPAddress@4
tun.obj : error LNK2001: unresolved external symbol _GetAdapterIndex@8
Debug/openvpn.exe : fatal error LNK1120: 16 unresolved externals
Error executing link.exe.

openvpn.exe - 17 error(s), 1 warning(s)


 首先解决crytoapi.obj的lnk错误,找到crytoapi.c文件中的CertFreeCertificateContext 函数,找到其声明:WinCrypt.h文件,

#include <wincrypt.h>  //需要装PLATFORM SDK
#pragma comment (lib, "crypt32.lib")

 然后解决route.obj tun.obj的问题,也是通过route.c文件中的 GetIpForwardTable等其中一个link错误的函数,找到其声明文件:IPHlpApi.h

#include <iphlpapi.h>
#pragma comment (lib, "iphlpapi.lib")



最后再次编译,OK,开始你的openvpn代码调试吧!!!

posted @ 2017-08-02 20:02  星月相随  阅读(774)  评论(0编辑  收藏  举报