之前有写过一篇《OpenDDS笔记(一) Windows环境下的开发实例》,本篇以相同的IDL,h,Cpp,ini文件在Linux下进行实现。即Demo.idl,Publisher.cpp,Subscriber.cpp,DataReaderListener.h,DataReaderListener.cpp,config_Pub.ini,config_Sub.ini沿用《OpenDDS笔记(一) Windows环境下的开发实例》中的代码,需要的客官请移步:https://blog.csdn.net/jam12315/article/details/113388662
一、定义IDL文件
将Demo.idl,Publisher.cpp,Subscriber.cpp,DataReaderListener.h,DataReaderListener.cpp,config_Pub.ini,config_Sub.ini拷贝到ubuntu文件夹下,本篇路径为:/home/jamie/DevOpenDDS/Demo
二、生成Export头文件
cd到IDL文件所在目录下,通过以下命令自动生成xxxx_Export.h
perl $ACE_ROOT/bin/generate_export_file.pl DemoIDL > DemoIDL_Export.h
三、定义Demo.mwc、Demo.mpc
Demo.mwc定义如下:
workspace {
// the -relative and -include cmdlines make it so this workspace
// does not have to be in the $DDS_ROOT directory tree.
// tell MPC to substitute our DDS_ROOT environment variables for relative paths
cmdline += -relative DDS_ROOT=$DDS_ROOT
// tell the projects where to find the DDS base projects (*.mpb)
cmdline += -include $DDS_ROOT/MPC/config
}
这段代码直接复制过去就可以了,一般不用更改。
Demo.mpc定义如下:
下面的代码包含3个工程。*.Idl,Publisher,Subscriber,代码看起来很唬人,不要怕,我们把主要的一说,其他都拷贝过去就行了,记得使用时删除//及后面的注释。
project(*Idl) : dcps { //IDL工程
sharedname = DemoIdl //库名称
dynamicflags = DEMOIDL_BUILD_DLL //宏定义前面几个字符与库名称保持一致,便于理解。
libout = .
requires += tao_orbsvcs
requires += no_opendds_safety_profile
after += Svc_Utils
includes += $(TAO_ROOT)/orbsvcs
idlflags += -I$(TAO_ROOT)/orbsvcs \
-Wb,export_macro=DemoIDL_Export \ //与第二步所述的头文件保持一致就可以了。
-Wb,export_include=DemoIDL_Export.h //与第二步所述的头文件保持一致就可以了。
dcps_ts_flags += -Wb,export_macro=DemoIDL_Export //与第二步所述的头文件保持一致就可以了。TypeSupport_Files {
Demo.idl //与第一步的IDL文件名保持一致
}IDL_Files {
Demo.idl //与第一步的IDL文件名保持一致
}// We only want the generated files
Header_Files {
}// We only want the generated files
Source_Files {
}
}project(Publisher) : dcpsexe, dcps_tcp, svc_utils { //Publisher工程
after += *Idl //编译依赖于上述IDL工程
exename = Publisher //生成的可执行程序的名称
requires += tao_orbsvcs
requires += no_opendds_safety_profileincludes += $(TAO_ROOT)/orbsvcs
libs += DemoIdl //依赖库,为上面飙蓝的库名称IDL_Files {
}TypeSupport_Files {
}Header_Files {
}Source_Files {
Publisher.cpp //已经写好的Publisher.cpp文件
}
}project(Subscriber) : dcpsexe, dcps_tcp { //Subscriber工程
after += *Idl //编译依赖于上述IDL工程
exename = Subscriber //生成的可执行程序的名称
requires += tao_orbsvcs
requires += no_opendds_safety_profileincludes += $(TAO_ROOT)/orbsvcs
libs += DemoIdl //依赖库,为上面飙蓝的库名称TypeSupport_Files {
}IDL_Files {
}Header_Files {
}Source_Files {
Subscriber.cpp //已经写好的Subscriber.cpp文件
DataReaderListener.cpp //已经写好的DataReaderListener.cpp文件
}}
四、生成代码
1. cd到IDL目录下,执行$ACE_ROOT/bin/mwc.pl -type make产生Makefile
2. 执行make产生libDemoIdl.so库,及可执行程序Publicsher,Subscriber
五、执行
根据本人《OpenDDS笔记(一) Windows环境下的开发实例》中所述,配置文件已将发现方式设置为对等发现,因此不分先后得运行Publisher,Subscriber,就可以实现同样得功能。