-
下载安装源
cd /etc/yum.repos.d sudo wget https://zeroc.com/download/Ice/3.7/el7/zeroc-ice3.7.repo
-
Ice所需库
-
安装lmdb-devel mcpp-devel(其他库系统可能自带有,可先不安装,等make报错再安装)
sudo yum install lmdb-devel mcpp-devel
-
下载Ice源码ice-3.7.3.tar.gz,解压
cd /home/user/ tar -zxf ice-3.7.3.tar.gz cd ice-3.7.3/cpp
-
编译(默认C++98,可设置CXXFLAGS修改为c++11)
sudo make -j4 sudo make CXXFLAGS=-std=c++11 -j4
-
src/Ice/SHA1.cpp:14:31: 致命错误:openssl/sha.h:没有那个文件或目录
sudo yum install openssl-devel
-
src/Ice/ConnectionI.cpp:27:21: 致命错误:bzlib.h:没有那个文件或目录
sudo yum install bzip2-devel
-
src/IceXML/Parser.cpp:7:19: 致命错误:expat.h:没有那个文件或目录
sudo yum install expat-devel
-
- 安装(默认安装在/opt/Ice-3.7.3)
sudo make install
- 测试
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!则成功。
linux安装Ice3.7 c++
最新推荐文章于 2022-10-08 18:28:25 发布