接口编程
作者:互联网
App调用接口,依赖头文件和库。
库文件更新,App代码不需要重新编译,直接就可以运行。
//main.cpp #include "Student.h" #include <iostream> void Student::TellStory(){ std::cout<<"hello world"<<std::endl; } Student::Student(/* args */) { } Student::~Student() { } void test(){std::cout<<"ss222ss"<<std::endl;} IStudent* getIStudent(){ IStudent* I = new Student(); return I; }
//IStudent.h
//#pragma once #ifndef IS_H #define IS_H #include <iostream> class IStudent { public: IStudent(){} virtual ~IStudent(){} virtual void TellStory() = 0; }; IStudent* getIStudent(); void test(); #endif
//Student.h //#pragma once #ifndef S_H #define S_H #include "IStudent.h" #include <iostream> class Student: public IStudent { private: /* data */ public: Student(/* args */); ~Student(); void TellStory(); }; #endif
//Student.cpp #include "Student.h" #include <iostream> void Student::TellStory(){ std::cout<<"hello world"<<std::endl; } Student::Student(/* args */) { } Student::~Student() { } void test(){std::cout<<"ss222ss"<<std::endl;} IStudent* getIStudent(){ IStudent* I = new Student(); return I; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.5) project(student) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") add_library(Istudent SHARED Student.cpp) #set(SOURCE_FILES main.cpp) #add_executable(student ${SOURCE_FILES}) #target_link_libraries(student Istudent)
先全编译,生成库和可执行程序。
然后不需要再编译可执行程序了,修改Student.cpp 的打印。编译生成新的库文件
运行可执行程序。可执行程序可以打印新的更改。
标签:IStudent,void,编程,接口,Student,cpp,include,可执行程序 来源: https://www.cnblogs.com/yuguangyuan/p/16479185.html