RPC通信框架——RCF介绍中说了,RCF本身是支持跨平台的,其代码放到Linux平台,是可以通过gcc、make等工具,编译通过的。
官方提供的源码中,只有cmake编译脚本,并没有提供Makefile,如果想直接使用make编译,就必须自己写Makefile。
抛开这些不说,本文主要介绍在Windows系统上,通过VS与VisualGDB来完成Linux版本的RCF库的编译和调试。
使用VS+VisualGDB编译调试Linux程序 ,文中已经简单介绍了VisualGDB的用法,本文直奔主题,介绍RCF静态库的编译方式。
通过向导配置项目通过向导创建项目时,比较重要的几个步骤如下,
选择项目类型为静态库:
通过Windows共享文件夹以及smb服务的方式在Windows与Linux之前共享代码:
具体命令为:
mount.cifs //192.168.3.125/rcf.linux '/code/rcf' -o user=hbccdf,pass='****',noperm配置目录结构
配置后的目录结构如下
D:/CODE/C++/RCF.LINUX├─Debug├─include│ ├─RCF│ │ ├─external│ │ │ └─asio│ │ │ └─asio│ │ │ ├─detail│ │ │ │ └─impl│ │ │ ├─impl│ │ │ ├─ip│ │ │ │ ├─detail│ │ │ │ │ └─impl│ │ │ │ └─impl│ │ │ ├─local│ │ │ │ └─detail│ │ │ │ └─impl│ │ │ ├─posix│ │ │ ├─ssl│ │ │ │ └─detail│ │ │ └─windows│ │ ├─test│ │ ├─thread│ │ │ └─impl│ │ ├─utf8│ │ │ └─detail│ │ └─util│ │ └─Platform│ │ └─OS│ │ ├─Unix│ │ └─Windows│ └─SF├─rcf.linux│ ├─Debug│ │ └─rcf.linux.tlog│ ├─obj│ │ └─Win32│ │ └─Debug│ └─VisualGDBCache│ └─rcf.linux-Debug└─src ├─RCF │ ├─test │ └─util └─SF修改项目配置
通过VisualGDB修改项目配置,也就是Makefile相关配置,也可以通过VS直接改文本内容:
添加RCF源代码由于RCF的源码全被包含在RCF.cpp文件中,所以,只需要在VS中添加RCF.cpp文件即可:
完成配置并进行编译至此,就完成了所有的配置,可以通过VS进行编译了,如下是编译信息:
1>------ 已启动生成: 项目: rcf.linux, 配置: Debug Win32 ------1> VisualGDB: Testing shared folder-based mapping D:/Code/C++/rcf.linux/rcf.linux <=> 192.168.3.128:/code/rcf/rcf.linux...1> VisualGDB: Trying to create D:/Code/C++/rcf.linux/rcf.linux/vgdb1889774204.tmp...1> VisualGDB: Run "make CONFIG=Debug" in directory "/code/rcf/rcf.linux" on root@192.168.3.128 (SSH)1> g++ -ggdb -ffunction-sections -O0 -I../include -DDEBUG -c ../src/RCF/RCF.cpp -o Debug/RCF.o -MD -MF Debug/RCF.dep1> ar -r Debug/librcf.a Debug/RCF.o1> ar: creating Debug/librcf.a========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========添加测试程序
通过VisualGDB向导添加测试程序rcftest,
然后进行项目配置:
添加测试代码——通过TCP进行通信RCF进程间通信Demo程序,文中说了通过RCF进行进程间通信的具体步骤,
首先添加接口 I_HELLO:
#PRagma once#include "rcf/rcf.hpp"#include <iostream>#include <string>using namespace std;RCF_BEGIN(I_HELLO, "I_HELLO") RCF_METHOD_V1(void, SayHello, const string&) RCF_METHOD_R1(int, add, int&) RCF_METHOD_V0(void, test)RCF_END(I_HELLO)class HelloImpl{public: void SayHello(const string& world) { cout << "hello " << world << endl; } int add(int& a) { a = a + a; return a + 2; } void test() {}};
然后添加测试代码:
#include <iostream>#include "hello.h"using namespace std;int main(int argc, char *argv[]){ RCF::RcfInitDeinit rcf_init; HelloImpl hello; RCF::RcfServer server(RCF::TcpEndpoint(50001)); server.bind<I_HELLO>(hello); server.start(); RcfClient<I_HELLO> client(RCF::TcpEndpoint(50001)); string str = "test"; client.SayHello(str); int a = 3; int b = client.add(a); cout << "a = " << a << ", b = " << b << endl; for (int i = 0; i < 20000; ++i) { client.test(); } cout << "完成" << endl;}
如下是编译信息:
1>------ 已启动生成: 项目: rcftest, 配置: Debug Win32 ------1> VisualGDB: Testing shared folder-based mapping D:/Code/C++/rcf.linux/rcftest <=> 192.168.3.128:/code/rcf/rcftest...1> VisualGDB: Trying to create D:/Code/C++/rcf.linux/rcftest/vgdb1568095730.tmp...1> Updating D:/Code/C++/rcf.linux/rcftest/Makefile1> VisualGDB: Updated source file list in D:/Code/C++/rcf.linux/rcftest/Makefile. Enable verbose mode for more details.1> VisualGDB: Run "make CONFIG=Debug" in directory "/code/rcf/rcftest" on root@192.168.3.128 (SSH)1> g++ -ggdb -ffunction-sections -O0 -I../include -DDEBUG -c rcftest.cpp -o Debug/rcftest.o -MD -MF Debug/rcftest.dep1> g++ -o Debug/rcftest -Wl,-gc-sections -L../rcf.linux/Debug -Wl,--start-group Debug/rcftest.o -lrcf -lpthread -ldl -Wl,--rpath='$ORIGIN' -Wl,--end-group========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========运行测试程序并查看测试结果
可以通过VS F5调试运行,查看运行结果:
这样,通过VS+VisualGDB编译RCF的工作就完成了。
VisualGDB生成的所有Makefile代码虽然是在Windows系统上,使用VS进行编译开发,实际上还是需要生成Makefile文件,然后通过make进行编译。
每个项目会生成几个与项目配置有关的makefile文件,比如debug.mak文件,以及一个与配置无关的文件Makefile,编译的时候会根据配置选择对应的debug.mak或者release.mak。
rcf.linux项目debug.mak文件:
#Generated by VisualGDB (http://visualgdb.com)#DO NOT EDIT THIS FILE MANUALLY UNLESS YOU ABSOLUTELY NEED TO#USE VISUALGDB PROJECT PROPERTIES DIALOG INSTEADBINARYDIR := Debug#ToolchainCC := gccCXX := g++LD := $(CXX)AR := arOBJCOPY := objcopy#Additional flagsPREPROCESSOR_MACROS := DEBUGINCLUDE_DIRS := ../includeLIBRARY_DIRS := LIBRARY_NAMES := pthreadADDITIONAL_LINKER_INPUTS := MACOS_FRAMEWORKS := LINUX_PACKAGES := CFLAGS := -ggdb -ffunction-sections -O0CXXFLAGS := -ggdb -ffunction-sections -O0ASFLAGS := LDFLAGS := -Wl,-gc-sectionsCOMMONFLAGS := START_GROUP := -Wl,--start-groupEND_GROUP := -Wl,--end-group#Additional options detected from testing the toolchainIS_LINUX_PROJECT := 1
Makefile文件:
#Generated by VisualGDB project wizard. #Note: VisualGDB will automatically update this file when you add new sources to the project.#All other changes you make in this file will be preserved.#Visit http://visualgdb.com/makefiles for more details#VisualGDB: AutoSourceFiles #<--- remove this line to disable auto-updating of SOURCEFILES and EXTERNAL_LIBSTARGETNAME := librcf.a#TARGETTYPE can be APP, STATIC or SHAREDTARGETTYPE := STATICto_lowercase = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))CONFIG ?= DEBUGCONFIGURATION_FLAGS_FILE := $(call to_lowercase,$(CONFIG)).makinclude $(CONFIGURATION_FLAGS_FILE)include $(ADDITIONAL_MAKE_FILES)ifeq ($(BINARYDIR),)error: $(error Invalid configuration, please check your inputs)endifsoURCEFILES := ../src/RCF/RCF.cppEXTERNAL_LIBS := EXTERNAL_LIBS_COPIED := $(foreach lib, $(EXTERNAL_LIBS),$(BINARYDIR)/$(notdir $(lib)))CFLAGS += $(COMMONFLAGS)CXXFLAGS += $(COMMONFLAGS)ASFLAGS += $(COMMONFLAGS)LDFLAGS += $(COMMONFLAGS)CFLAGS += $(addprefix -I,$(INCLUDE_DIRS))CXXFLAGS += $(addprefix -I,$(INCLUDE_DIRS))CFLAGS += $(addprefix -D,$(PREPROCESSOR_MACROS))CXXFLAGS += $(addprefix -D,$(PREPROCESSOR_MACROS))ASFLAGS += $(addprefix -D,$(PREPROCESSOR_MACROS))CXXFLAGS += $(addprefix -framework ,$(MACOS_FRAMEWORKS))CFLAGS += $(addprefix -framework ,$(MACOS_FRAMEWORKS))LDFLAGS += $(addprefix -framework ,$(MACOS_FRAMEWORKS))LDFLAGS += $(addprefix -L,$(LIBRARY_DIRS))ifeq ($(GENERATE_MAP_FILE),1)LDFLAGS += -Wl,-Map=$(BINARYDIR)/$(basename $(TARGETNAME)).mapendifLIBRARY_LDFLAGS = $(addprefix -l,$(LIBRARY_NAMES))ifeq ($(IS_LINUX_PROJECT),1) RPATH_PREFIX := -Wl,--rpath='$$ORIGIN/../ LIBRARY_LDFLAGS += $(EXTERNAL_LIBS) LIBRARY_LDFLAGS += -Wl,--rpath='$$ORIGIN' LIBRARY_LDFLAGS += $(addsuffix ',$(addprefix $(RPATH_PREFIX),$(dir $(EXTERNAL_LIBS)))) ifeq ($(TARGETTYPE),SHARED) CFLAGS += -fPIC CXXFLAGS += -fPIC ASFLAGS += -fPIC LIBRARY_LDFLAGS += -Wl,-soname,$(TARGETNAME) endif ifneq ($(LINUX_PACKAGES),) PACKAGE_CFLAGS := $(foreach pkg,$(LINUX_PACKAGES),$(shell pkg-config --cflags $(pkg))) PACKAGE_LDFLAGS := $(foreach pkg,$(LINUX_PACKAGES),$(shell pkg-config --libs $(pkg))) CFLAGS += $(PACKAGE_CFLAGS) CXXFLAGS += $(PACKAGE_CFLAGS) LIBRARY_LDFLAGS += $(PACKAGE_LDFLAGS) endif else LIBRARY_LDFLAGS += $(EXTERNAL_LIBS)endifLIBRARY_LDFLAGS += $(ADDITIONAL_LINKER_INPUTS)all_make_files := $(firstWord $(MAKEFILE_LIST)) $(CONFIGURATION_FLAGS_FILE) $(ADDITIONAL_MAKE_FILES)ifeq ($(STARTUPFILES),) all_source_files := $(SOURCEFILES)else all_source_files := $(STARTUPFILES) $(filter-out $(STARTUPFILES),$(SOURCEFILES))endifsource_obj1 := $(all_source_files:.cpp=.o)source_obj2 := $(source_obj1:.c=.o)source_obj3 := $(source_obj2:.s=.o)source_obj4 := $(source_obj3:.S=.o)source_obj5 := $(source_obj4:.cc=.o)source_objs := $(source_obj5:.cxx=.o)all_objs := $(addprefix $(BINARYDIR)/, $(notdir $(source_objs)))PRIMARY_OUTPUTS :=ifeq ($(GENERATE_BIN_FILE),1)PRIMARY_OUTPUTS += $(BINARYDIR)/$(basename $(TARGETNAME)).binendififeq ($(GENERATE_IHEX_FILE),1)PRIMARY_OUTPUTS += $(BINARYDIR)/$(basename $(TARGETNAME)).ihexendififeq ($(PRIMARY_OUTPUTS),)PRIMARY_OUTPUTS := $(BINARYDIR)/$(TARGETNAME)endifall: $(PRIMARY_OUTPUTS)$(BINARYDIR)/$(basename $(TARGETNAME)).bin: $(BINARYDIR)/$(TARGETNAME) $(OBJCOPY) -O binary $< $@$(BINARYDIR)/$(basename $(TARGETNAME)).ihex: $(BINARYDIR)/$(TARGETNAME) $(OBJCOPY) -O ihex $< $@ifeq ($(TARGETTYPE),APP)$(BINARYDIR)/$(TARGETNAME): $(all_objs) $(EXTERNAL_LIBS) $(LD) -o $@ $(LDFLAGS) $(START_GROUP) $(all_objs) $(LIBRARY_LDFLAGS) $(END_GROUP)endififeq ($(TARGETTYPE),SHARED)$(BINARYDIR)/$(TARGETNAME): $(all_objs) $(EXTERNAL_LIBS) $(LD) -shared -o $@ $(LDFLAGS) $(START_GROUP) $(all_objs) $(LIBRARY_LDFLAGS) $(END_GROUP)endif ifeq ($(TARGETTYPE),STATIC)$(BINARYDIR)/$(TARGETNAME): $(all_objs) $(AR) -r $@ $^endif-include $(all_objs:.o=.dep)clean:ifeq ($(USE_DEL_TO_CLEAN),1) del /S /Q $(BINARYDIR)else rm -rf $(BINARYDIR)endif$(BINARYDIR): mkdir $(BINARYDIR)#VisualGDB: FileSpecificTemplates #<--- VisualGDB will use the following lines to define rules for source files in subdirectories$(BINARYDIR)/%.o : %.cpp $(all_make_files) |$(BINARYDIR) $(CXX) $(CXXFLAGS) -c $< -o $@ -MD -MF $(@:.o=.dep)$(BINARYDIR)/%.o : %.c $(all_make_files) |$(BINARYDIR) $(CC) $(CFLAGS) -c $< -o $@ -MD -MF $(@:.o=.dep)$(BINARYDIR)/%.o : %.S $(all_make_files) |$(BINARYDIR) $(CC) $(CFLAGS) $(ASFLAGS) -c $< -o $@ -MD -MF $(@:.o=.dep)$(BINARYDIR)/%.o : %.s $(all_make_files) |$(BINARYDIR) $(CC) $(CFLAGS) $(ASFLAGS) -c $< -o $@ -MD -MF $(@:.o=.dep)$(BINARYDIR)/%.o : %.cc $(all_make_files) |$(BINARYDIR) $(CC) $(CFLAGS) $(CXXFLAGS) -c $< -o $@ -MD -MF $(@:.o=.dep)$(BINARYDIR)/%.o : %.cxx $(all_make_files) |$(BINARYDIR) $(CC) $(CFLAGS) $(CXXFLAGS) -c $< -o $@ -MD -MF $(@:.o=.dep)#VisualGDB: GeneratedRules #<--- All lines below are auto-generated$(BINARYDIR)/RCF.o : ../src/RCF/RCF.cpp $(all_make_files) |$(BINARYDIR) $(CXX) $(CXXFLAGS) -c $< -o $@ -MD -MF $(@:.o=.dep)
rcftest项目:
#Generated by VisualGDB (http://visualgdb.com)#DO NOT EDIT THIS FILE MANUALLY UNLESS YOU ABSOLUTELY NEED TO#USE VISUALGDB PROJECT PROPERTIES DIALOG INSTEADBINARYDIR := Debug#ToolchainCC := gccCXX := g++LD := $(CXX)AR := arOBJCOPY := objcopy#Additional flagsPREPROCESSOR_MACROS := DEBUGINCLUDE_DIRS := ../includeLIBRARY_DIRS := ../rcf.linux/DebugLIBRARY_NAMES := rcf pthread dlADDITIONAL_LINKER_INPUTS := MACOS_FRAMEWORKS := LINUX_PACKAGES := CFLAGS := -ggdb -ffunction-sections -O0CXXFLAGS := -ggdb -ffunction-sections -O0ASFLAGS := LDFLAGS := -Wl,-gc-sectionsCOMMONFLAGS := START_GROUP := -Wl,--start-groupEND_GROUP := -Wl,--end-group#Additional options detected from testing the toolchainIS_LINUX_PROJECT := 1
Makefile文件:
与rcf.linux项目的Makefile文件基本上是一样的,就不在这里列举了。
新闻热点
疑难解答