MinGW环境使用gSOAP
2009-04-01 11:47 Jaypei 阅读(1252) 评论(0) 编辑 收藏 举报环境
WindowsXP SP3,MinGW 5.1.4,Cygwin,CMake 2.6(可有可无)
编译
首先,下载gsoap:http://sourceforge.net/project/showfiles.php?group_id=52781
解压缩后执行
cd /libpath/
./configure --enable-samples=no
./configure --enable-samples=no
做如下修改:
/* Portability: define SOAP_SOCKLEN_T */
#if defined(_AIX)
# if defined(_AIX43)
# define SOAP_SOCKLEN_T socklen_t
# else
# define SOAP_SOCKLEN_T int
# endif
#elif defined(SOCKLEN_T)
# define SOAP_SOCKLEN_T SOCKLEN_T
#elif defined(__socklen_t_defined) || defined(_SOCKLEN_T) || defined(CYGWIN) || defined(FREEBSD) || defined(__FreeBSD__) || defined(__QNX__) || defined(QNX) || defined(OS390)
# define SOAP_SOCKLEN_T socklen_t
#elif defined(IRIX) || defined(WIN32) || defined(__APPLE__) || defined(HP_UX) || defined(SUN_OS) || defined(OPENSERVER) || defined(TRU64) || defined(VXWORKS)
# define SOAP_SOCKLEN_T int
#else
# define SOAP_SOCKLEN_T size_t
#endif
// 修改为
/* Portability: define SOAP_SOCKLEN_T */
#if defined(_AIX)
# if defined(_AIX43)
# define SOAP_SOCKLEN_T socklen_t
# else
# define SOAP_SOCKLEN_T int
# endif
#elif defined(SOCKLEN_T)
# define SOAP_SOCKLEN_T SOCKLEN_T
#elif defined(__socklen_t_defined) || defined(_SOCKLEN_T) || defined(CYGWIN) || defined(FREEBSD) || defined(__FreeBSD__) || defined(__QNX__) || defined(QNX) || defined(OS390)
# define SOAP_SOCKLEN_T int
#elif defined(IRIX) || defined(WIN32) || defined(__APPLE__) || defined(HP_UX) || defined(SUN_OS) || defined(OPENSERVER) || defined(TRU64) || defined(VXWORKS)
# define SOAP_SOCKLEN_T int
#else
# define SOAP_SOCKLEN_T size_t
#endif
#if defined(_AIX)
# if defined(_AIX43)
# define SOAP_SOCKLEN_T socklen_t
# else
# define SOAP_SOCKLEN_T int
# endif
#elif defined(SOCKLEN_T)
# define SOAP_SOCKLEN_T SOCKLEN_T
#elif defined(__socklen_t_defined) || defined(_SOCKLEN_T) || defined(CYGWIN) || defined(FREEBSD) || defined(__FreeBSD__) || defined(__QNX__) || defined(QNX) || defined(OS390)
# define SOAP_SOCKLEN_T socklen_t
#elif defined(IRIX) || defined(WIN32) || defined(__APPLE__) || defined(HP_UX) || defined(SUN_OS) || defined(OPENSERVER) || defined(TRU64) || defined(VXWORKS)
# define SOAP_SOCKLEN_T int
#else
# define SOAP_SOCKLEN_T size_t
#endif
// 修改为
/* Portability: define SOAP_SOCKLEN_T */
#if defined(_AIX)
# if defined(_AIX43)
# define SOAP_SOCKLEN_T socklen_t
# else
# define SOAP_SOCKLEN_T int
# endif
#elif defined(SOCKLEN_T)
# define SOAP_SOCKLEN_T SOCKLEN_T
#elif defined(__socklen_t_defined) || defined(_SOCKLEN_T) || defined(CYGWIN) || defined(FREEBSD) || defined(__FreeBSD__) || defined(__QNX__) || defined(QNX) || defined(OS390)
# define SOAP_SOCKLEN_T int
#elif defined(IRIX) || defined(WIN32) || defined(__APPLE__) || defined(HP_UX) || defined(SUN_OS) || defined(OPENSERVER) || defined(TRU64) || defined(VXWORKS)
# define SOAP_SOCKLEN_T int
#else
# define SOAP_SOCKLEN_T size_t
#endif
因为在编译的过程中发现make一直创建快捷方式,都指向stdsoap2.cpp和stdsoap2.c两个文件,而在make的过程中对快捷方式又总提示not found,于是这一步可以这么做:
cd /libpath/gsoap
cp stdsoap2.cpp stdsoap2_cpp.cpp
cp stdsoap2.c stdsoap2_ck.c
cp stdsoap2.cpp stdsoap2_ck_cpp.cpp
cp stdsoap2.c stdsoap2_ssl.c
cp stdsoap2.cpp stdsoap2_ssl_cpp.cpp
cp stdsoap2.cpp stdsoap2_cpp.cpp
cp stdsoap2.c stdsoap2_ck.c
cp stdsoap2.cpp stdsoap2_ck_cpp.cpp
cp stdsoap2.c stdsoap2_ssl.c
cp stdsoap2.cpp stdsoap2_ssl_cpp.cpp
然后执行
cd /libpath
make
make
结果是报错的,提示找不到yacc。但是这个时候已经生成了所需要的库,于是我就没继续往下编译。
使用
使用就很简单了,网上的相关文章很多,简单用PHP的nuSOAP写一个服务端gSOAP做客户端测试一下。
PHP代码如下:
<?php
require_once 'nusoap/nusoap.php';
function hello() {
return '中文?';
}
function test1($str1, $str2) {
return 'S1:'.$str1.',S2:'.$str2;
}
$soap = new soap_server();
$soap->soap_defencoding = 'UTF-8';
$soap->decode_utf8 = false;
$soap->configureWSDL('test1');
$soap->register('hello',
array(),
array("return"=>"xsd:string"));
$soap->register('test1',
array("str1"=>'xsd:string', "str2"=>"xsd:string"),
array("result"=>"xsd:string"));
$soap->service($HTTP_RAW_POST_DATA);
require_once 'nusoap/nusoap.php';
function hello() {
return '中文?';
}
function test1($str1, $str2) {
return 'S1:'.$str1.',S2:'.$str2;
}
$soap = new soap_server();
$soap->soap_defencoding = 'UTF-8';
$soap->decode_utf8 = false;
$soap->configureWSDL('test1');
$soap->register('hello',
array(),
array("return"=>"xsd:string"));
$soap->register('test1',
array("str1"=>'xsd:string', "str2"=>"xsd:string"),
array("result"=>"xsd:string"));
$soap->service($HTTP_RAW_POST_DATA);
把gsoap下的bin设置环境变量,然后执行:
wsdl2h -o hello.h http://jaypei.cnblogs.com/.../nusoap_test1.php?wsdl
soapcpp2 -IX:\...\gsoap\import -C -i hello.h
soapcpp2 -IX:\...\gsoap\import -C -i hello.h
可以把它放到一个单独目录中然后写一个批处理去做。
main.cc如下:
#include <iostream>
#include <string>
#include "soaptest1BindingProxy.h"
#include "test1Binding.nsmap"
#include "stdsoap2.h"
using namespace std;
int main(int argc, char** argv)
{
test1BindingProxy proxy(SOAP_C_UTFSTRING);
string result;
if (proxy.hello(result) == SOAP_OK) {
cout << result << endl;
}
return 0;
}
#include <string>
#include "soaptest1BindingProxy.h"
#include "test1Binding.nsmap"
#include "stdsoap2.h"
using namespace std;
int main(int argc, char** argv)
{
test1BindingProxy proxy(SOAP_C_UTFSTRING);
string result;
if (proxy.hello(result) == SOAP_OK) {
cout << result << endl;
}
return 0;
}
一定要加SOAP_C_UTFSTRING,说明使用UTF8.
假设目录service_header为批处理的目录,执行后,主目录CmakeLists.txt如下:
SET(SRC_FILES main.cc)
INCLUDE_DIRECTORIES(
"service_header"
"X:\...\gsoap"
)
LINK_DIRECTORIES(
"service_header"
"X:\...\gsoap"
)
ADD_SUBDIRECTORY(
"service_header"
)
ADD_EXECUTABLE(main ${SRC_FILES})
TARGET_LINK_LIBRARIES(main
"wservice_test1"
"gsoap++"
"wsock32.lib"
)
INCLUDE_DIRECTORIES(
"service_header"
"X:\...\gsoap"
)
LINK_DIRECTORIES(
"service_header"
"X:\...\gsoap"
)
ADD_SUBDIRECTORY(
"service_header"
)
ADD_EXECUTABLE(main ${SRC_FILES})
TARGET_LINK_LIBRARIES(main
"wservice_test1"
"gsoap++"
"wsock32.lib"
)
service_header/CmakeLists.txt内容如下:
SET(SRC_FILES
"soaptest1BindingProxy.cpp"
"soapC.cpp"
)
ADD_LIBRARY(wservice_test1 STATIC ${SRC_FILES})
"soaptest1BindingProxy.cpp"
"soapC.cpp"
)
ADD_LIBRARY(wservice_test1 STATIC ${SRC_FILES})
调试成功!
链接的时候记得要加上gsoap++库、wsock32.lib和webservice编译生成的库
---
以上就是我全部的测试过程,使用不当的地方,还请留言指正。