linux安装Ice3.7 c++

  1. 下载安装源

    cd /etc/yum.repos.d
    sudo wget https://zeroc.com/download/Ice/3.7/el7/zeroc-ice3.7.repo
  2. Ice所需库 

    1. bzip2 1.0
    2. expat 2.1 or later
    3. LMDB 0.9 (LMDB is not required with the C++11 mapping)
    4. mcpp 2.7.2 with patches
    5. OpenSSL 1.0.0 or later (on AIX and Linux)
  3. 安装lmdb-devel mcpp-devel(其他库系统可能自带有,可先不安装,等make报错再安装)

    sudo yum install lmdb-devel mcpp-devel
  4. 下载Ice源码ice-3.7.3.tar.gz,解压

    cd /home/user/
    tar -zxf ice-3.7.3.tar.gz
    cd ice-3.7.3/cpp
  5. 编译(默认C++98,可设置CXXFLAGS修改为c++11)

    sudo make -j4
    sudo make CXXFLAGS=-std=c++11 -j4
    1. src/Ice/SHA1.cpp:14:31: 致命错误:openssl/sha.h:没有那个文件或目录

      sudo yum install openssl-devel
    2. src/Ice/ConnectionI.cpp:27:21: 致命错误:bzlib.h:没有那个文件或目录

      sudo yum install bzip2-devel
    3. src/IceXML/Parser.cpp:7:19: 致命错误:expat.h:没有那个文件或目录

      sudo yum install expat-devel

       

  6. 安装(默认安装在/opt/Ice-3.7.3)
    sudo make install

     

  7. 测试

    1)编写ice的slice配置文件

    module Demo {
        interface Printer {
            void printString(string s);
        };
    };

     2)编译Slice定义文件(生成Printer.h 和Printer.cpp)找不到slice2cpp,自己配置运行环境在/opt/Ice-3.7.3/bin中

    slice2cpp Printer.ice

    3)编写服务端,命名为Server.cpp

    #include <Ice/Ice.h>
    #include <Printer.h>
    
    using namespace std;
    using namespace Demo;
    
    class PrinterI : public Printer {
        public:
            virtual void printString(const string& s, const Ice::Current&);
    };
    
    void 
    PrinterI::printString(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) {
            try {
                ic->destroy();
            } catch (const Ice::Exception& e) {
                cerr << e << endl;
                status = 1;
            }
        }
        return status;
    }

    4)编译服务端

    c++ -I. -I/opt/Ice-3.7.3/include -c Printer.cpp Server.cpp
    c++ -o server Printer.o Server.o -L/opt/Ice-3.7.3/lib64 -lIce -lpthread

    5)编写客户端,命名为Client.cpp

    #include <Ice/Ice.h>
    #include <Printer.h>
    
    using namespace std;
    using namespace Demo;
    
    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->printString("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;
    }

    6)编译客户端

    c++ -I. -I/opt/Ice-3.7.3/include -c Printer.cpp Client.cpp
    c++ -o client Printer.o Client.o -L/opt/Ice-3.7.3/lib64 -lIce -lpthread

    7)运行Server,然后运行Client,如果看到输出Hello World!则成功。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值