linux删除编译中间件,Ubuntu上OmniORB中间件的编译过程

首先自己去下载omniorb(https://sourceforge.net/projects/omniorb/),解压后准备安装。我解压到了/root/omniorb4.1.5文件夹中。请一定要阅读README.unix,按照其指示的步骤,在终端中如下:

1) cd /root/omniorb4.1.5

2) 根据建议,创建一个独立的文件夹,如build:mkdir build

3) cd build

4) ../configure

5) make

6) make install

如果您刚通过ubuntu的光盘安装好ubuntu,那么您是到不了第六步的了,因为第5步的恶梦就开始了。在运行第5步时,提示找不到python.h。您可以通过whereis python找到路径/usr/include/python,可是到里面一瞅,并没有python.h,这是因为只安装了python的运行环境,没有安装python的开发版本。啥也不用想了,使用命令

apt-get install python-dev

如果当前用户不是管理员的话使用命令:

sudo apt-get install python-dev

安装python。不过我是下载了压缩包后,再安装的,具体步骤很简单,解压了python2.7.1后,运行如下命令:

1) ./configure

2) make

3) make install

就完了,而且这样环境变量也都设置好了。然后再回去执行omni安装的第5步就好了。如果一切顺利,则没问题了。

下面使用KDevelop创建一个运行omniorb的服务器项目。我还是喜欢使用IDE来管理技术验证性的小项目。新建一个C++项目。在其中添加Time.idl,填写其内容如下:

struct TimeOfDay {

short hour; short minute; short second;

};

interface Time {

TimeOfDay get_gmt();

};

该例子来自于《基于C++CORBA高级编程》(英文名为Advanced.CORBA.Programming.with.C++,在csdn即可下载到)。

然后创建一个TimeImpl类,可以使用KDevelop的类添加向导,无非就是帮你生成一个TimeImpl.h和一个TimeImpl.cpp文件。

代码如下:

// 这是头文件

#include "Time.hh"

class TimeImpl : public virtual POA_Time {

public: TimeImpl(void);

virtual TimeOfDay get_gmt() throw(CORBA::SystemException);

public: virtual ~TimeImpl(void);

};

// 这是源文件

#include

#include

#include

//#include

#include "Time.hh"

#include "timeimpl.h"

using namespace std;

TimeImpl::TimeImpl(void)

{

}

TimeImpl::~TimeImpl(void)

{

}

TimeOfDay TimeImpl::get_gmt() throw ( CORBA::SystemException)

{

time_t time_now;

time(&time_now);

struct tm *time_p = gmtime ( &time_now );

TimeOfDay tod;

tod.hour = time_p->tm_hour;

tod.minute = time_p->tm_min;

tod.second = time_p->tm_sec;

cout << time_p->tm_hour << ":" << time_p->tm_min << ":" << time_p->tm_sec << endl;

return tod;

}

// 最后是main函数所在的源文件

#include

#include

#include

#include "Time.hh"

#include "timeimpl.h"

using namespace std;

int main ( int argc, char **argv )

{

try

{

CORBA::ORB_var orb = CORBA::ORB_init ( argc, argv );

CORBA::Object_var obj = orb->resolve_initial_references ( "RootPOA" );

PortableServer::POA_var poa = PortableServer::POA::_narrow ( obj );

PortableServer::POAManager_var mgr = poa->the_POAManager();

mgr->activate();

TimeImpl time_servant;

Time_var tm = time_servant._this();

CORBA::String_var str = orb->object_to_string ( tm );

cout << str << endl;

orb->run();

}

catch ( const CORBA::SystemException & )

{

cerr << "Uncaught CORBA Exception." << endl;

return 1;

}

return 0;

}

完了后修改项目生成文件CMakeLists.txt文件,我自己编写如下生成脚本:

cmake_minimum_required(VERSION 2.8)

project(server)

# 发现并使用omniidl编译所有的idl文件

message("发现并使用omniidl编译所有的idl文件")

file(GLOB_RECURSE FILE_LIST "*.idl")

foreach(src ${FILE_LIST})

message("发现idl文件: " ${src})

execute_process(COMMAND omniidl -C${PROJECT_SOURCE_DIR} -bcxx ${src})

endforeach()

# 发现并拼接所有使用omniidl生成的cc源文件

message("发现并拼接所有使用omniidl生成的cc源文件")

file(GLOB IDL_GEN_CPP_LIST "*SK.cc")

FOREACH(src ${IDL_GEN_CPP_LIST})

MESSAGE("发现omniidl生成的文件: " ${src})

STRING( REGEX MATCH "[^/]*//.cc$" ccFileName ${src} )

LIST(APPEND ccFileNameList ${ccFileName})

MESSAGE("截取其文件名为: " ${ccFileName} )

ENDFOREACH(src ${IDL_GEN_CPP_LIST})

add_executable(server ${ccFileNameList} timeimpl.cpp main.cpp)

target_link_libraries(server libomnithread.so libomniORB4.so libomniDynamic4.so libomniCodeSets4.so libCOS4.so libCOSDynamic4.so)

这里记着,在最后一行中,千万不要加上omnisslTP.so,这个万恶的ssl库,你暂时是找不到的。而omniorb中的README.unix中也写的很清楚,如果openSSL可用的话,添加该库,否则不要添加。当然“否则”后面的叙述是我说的。但是当你检查系统时,你会发现你的系统中确实是有openSSL,但那不是开发版,你可能会想下载libssl-dev来代替,那你就得重新编译一遍omniorb,还是别废这洋劲了。按上面的写吧。

在KDevelop中添加一个新类TimeImpl继承自POA_Time如下:

这样服务器就OK了,Build吧。完了后进入build子目录运行刚编译的可执行文件即可创建服务器。 客户端我是在windows上运行的,所以用Visual Studio创建的客户端,依然要在windows上安装omniorb,一定要看说明,其它项目配置之类的我实在不愿多说了,说明里写的很清楚,我还自己翻译了给同事使用。

只贴一下源码吧

// main函数所在的源文件为

#include "stdafx.h"

#include

#include

#include "../OMNIIDLTest/TimeH.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

try {

if (argc != 2) {

cerr << "Usage: client IOR_string" << endl;

throw 0;

}

CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

CORBA::Object_var obj = orb->string_to_object(argv[1]);

if (CORBA::is_nil(obj)) {

cerr << "Nil time referenc." << endl;

throw 0;

}

Time_var tm = Time::_narrow(obj);

if (CORBA::is_nil(tm)) {

cerr << "Argument is not a Time reference" << endl;

throw 0;

}

TimeOfDay tod = tm->get_gmt();

cout << "Time in Greenwich is "

<< setw(2) << setfill('0') << tod.hour << ":"

<< setw(2) << setfill('0') << tod.minute << ":"

<< setw(2) << setfill('0') << tod.second << endl;

} catch (const CORBA::SystemException &) {

cerr << "Uncaught CORBA exception." << endl;

return 1;

}

return 0;

}

当然一定要添加入刚刚在windows平台上通过omniidl编译出来的头文件与源文件。编译完了后,把服务器运行时输出的从IOR:开始那一长串字符串都复制过来,然后所以客户端运行的参数就可以,如:

client IOR:010000000d00000049444c3a54696d653a312e30000000000100000000000000680000000

10102000f0000003139322e3136382e31352e3132390000989d00000e000000fe578cd24d00006c1600

0000000000000200000000000000080000000100000000545441010000001c0000000100000001000

1000100000001000105090101000100000009010100

完了,写的比较粗,以后再慢慢细化吧,主要是留个记录,也是为了给同事们讲解用的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值