gSOAP下载:https://www.genivia.com/downloads.html
编译安装:请自行查看源码包里的 INSTALL.txt
(一)Getting Started with gSOAP 之 Hello world: getting started with SOAP APIs
// hello.h
int ns__hello(std::string name, std::string& greeting);
soapcpp2 hello.h
soapcpp2 在 源码目录的 gsoap/src/ 目录下。
执行后生成文件如下:
// hello.cpp
#include "soapH.h" // include the generated source code headers
#include "ns.nsmap" // include XML namespaces
int main()
{
return soap_serve(soap_new());
}
int ns__hello(struct soap *soap, std::string name, std::string& greeting)
{
greeting = "Hello " + name;
return SOAP_OK;
}
c++ -o hello.cgi hello.cpp soapC.cpp soapServer.cpp stdsoap2.cpp
报错:
从源码目录拷贝 stdsoap2.cpp 文件,再次编译:
从源码目录拷贝 stdsoap2.h 文件,再次编译,编译通过。
由于我想在HI3559AV100的板子上运行,所以交叉编译:
aarch64-himix100-linux-c++ -o hello.cgi hello.cpp soapC.cpp soapServer.cpp stdsoap2.cpp
httpd -h $WEB-SERVICE-PATH
httpd 是板子自带的,在$WEB-SERVICE-PATH 下新建 cgi-bin 目录,并拷贝 hello.cgi
#include "soapH.h"
#include "ns.nsmap" // include XML namespaces
struct soap *soap = soap_new(); // new context
std::string greeting;
if (soap_call_ns__hello(soap, "http://yourdomain/cgi/hello.cgi", NULL, "world", greeting) == SOAP_OK)
std::cout << greeting << std::endl;
else
soap_stream_fault(soap, std::cerr);
soap_destroy(soap); // delete managed deserialized C++ instances
soap_end(soap); // delete other managed data
soap_free(soap); // free context
教程提供的代码看上去有点问题,没有main函数,先编译看看。
c++ -o myapp myapp.cpp soapC.cpp soapClient.cpp stdsoap2.cpp
#include "soapH.h"
#include "ns.nsmap" // include XML namespaces
int main() {
struct soap *soap = soap_new(); // new context
std::string greeting;
if (soap_call_ns__hello(soap, "http://192.168.1.161/cgi-bin/hello.cgi", NULL, "world", greeting) == SOAP_OK)
std::cout << greeting << std::endl;
else
soap_stream_fault(soap, std::cerr);
soap_destroy(soap); // delete managed deserialized C++ instances
soap_end(soap); // delete other managed data
soap_free(soap); // free context
}
定义main函数,并将 http://yourdomain/cgi/hello.cgi 替换为 http://192.168.1.161/cgi-bin/hello.cgi
再次编译,正常通过。
hello.cgi 调用成功。