下载安装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!