Xenial 下源码编译Ice3.6

下载安装Ice: https://zeroc.com/distributions/ice/3.6 (3.7版本改成3.7即可)

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 5E6DA83306132997
sudo apt-add-repository "deb http://zeroc.com/download/Ice/3.6/ubuntu$(lsb_release -rs) stable main"
sudo apt-get update
sudo apt-get install zeroc-ice-all-runtime zeroc-ice-all-dev

也可以从git仓库pull示例代码
A collection of sample programs are available on GitHub. You can download them by running the following command:

git clone -b 3.6 https://github.com/zeroc-ice/ice-demos.git

测试用例

建立一个print目录,在该目录下:

建立ice文件

demo.ice

module demo
{
    interface printer
    {
        void printerstr(string msg);    
    };
};

运行下面的命令,会在print目录下生成demo.h和demo.cpp。

$slice2cpp demo.ice
$ls print/
demo.ice   demo.h   demo.cpp

编写ice服务端server.cpp

#include <Ice/Ice.h>
#include <demo.h>
using namespace demo;
using namespace std;
class PrinterI : public printer {
    public:
        virtual void printerstr(const string & s,
                const Ice::Current &);
};
void
PrinterI::
printerstr(const string & s, const Ice::Current &)
{
    cout << s << endl;
}
int
main(int argc, char* argv[])
{
    int status = 0;
    Ice::CommunicatorPtr ic;
    try {
        ic = Ice::initialize(argc, argv);
        Ice::ObjectAdapterPtr adapter
            = ic->createObjectAdapterWithEndpoints(
                    "SimplePrinterAdapter", "default -p 10000");
        Ice::ObjectPtr object = new PrinterI;
        adapter->add(object,
                ic->stringToIdentity("SimplePrinter"));
        adapter->activate();
        ic->waitForShutdown();
    } catch (const Ice::Exception & e) {
        cerr << e << endl;
        status = 1;
    } catch (const char * msg) {
        cerr << msg << endl;
        status = 1;
    }
    if (ic)
        ic->destroy();
    return status;
}

编写ice客户端client.cpp

#include <Ice/Ice.h>
#include <demo.h>
using namespace demo;
using namespace std;

int
main(int argc, char * argv[])
{
    int status = 0;
    Ice::CommunicatorPtr ic;
    try {
        ic = Ice::initialize(argc, argv);
        Ice::ObjectPrx base = ic->stringToProxy(
                "SimplePrinter:default -p 10000");
        printerPrx printer = printerPrx::checkedCast(base);
        if (!printer)
            throw "Invalid proxy";
        printer->printerstr("Hello World!");
    } catch (const Ice::Exception & ex) {
        cerr << ex << endl;
        status = 1;
    } catch (const char * msg) {
        cerr << msg << endl;
        status = 1;
    }
    if (ic)
        ic->destroy();
    return status;
}

Finally:

编译并运行客户端和服务端,如果hello world打印出来,那么就是安装成功了。

$ g++ -I. -o server demo.cpp server.cpp -lIce -lIceUtil -lpthread
$ g++ -I. -o client demo.cpp client.cpp -lIce -lIceUtil -lpthread
$ ./server
$./client
Hello World!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值