C接口实现任务记录
main—>c接口---->c++接口
由c接口的实现记录任务taskID
//main.cc
# include "c_opt.h"
# include <iostream>
int main() {
int task1 = 11;
int task2 = 22;
int task3 = 33;
//传入taskid用于区分任务
c_init(7,task1);
c_init(56, task2);
c_init(33, task3);
c_pushframe(8,task1);
c_pushframe(9, task3);
c_pushframe(73, task2);
return 0;
}
//copt.h
# ifndef _COPT_H_
# define _COPT_H_
void c_init(int ct, int taskid);
void c_pushframe(int cpf, int taskid);
# endif
//copt.cc
# include "algo.h"
# include <memory>
# include "copt.h"
# include <map>
# include <iostream>
std::shared_ptr<Algo> algo_;
//记录任务map
std::map<int, std::shared_ptr<Algo>> taskMap_;
void c_init(int ct,int taskid) {
auto iter = taskMap_.find(taskid);
if (iter != taskMap_.end()) {
std::cout << "already inited, do nothing and return!" << std::endl;;
} else {
algo_ = std::make_shared<Algo>();
algo_->init(ct);
taskMap_.insert(std::pair<int, std::shared_ptr<Algo>>(taskid,algo_));
}
}
void c_pushframe(int cpf,int taskid) {
auto iter = taskMap_.find(taskid);
if (iter != taskMap_.end()) {
algo_ = iter->second;
algo_->pushFram(cpf);
} else {
std::cout << "error, do init first!" << std::endl;
}
}
//algo.h
# ifndef _ALGO_H_
# define _ALGO_H_
class Algo {
public:
Algo();
~Algo();
void init(int param);
void pushFram(int t);
private:
int param_;
};
# endif // !_ALGO_H_
//algo.cc
# include <iostream>
# include "algo.h"
Algo::Algo() {
std::cout << "algo init" << std::endl;
}
Algo::~Algo() {
}
void Algo::init(int param) {
param_ = param;
std::cout << "init success" << std::endl;
}
void Algo::pushFram(int t) {
std::cout << "param_:" << param_ << std::endl;
std::cout << "input param:" << t << std::endl;
}
编译libalgo.so : g++ algo.cc -o libalgo.so -shared -fPIC
编译libcopt.so : g++ copt.cc -o libcopt.so -std=c++11 -shared -fPIC -L./ -lalgo (此处链接上libalgo.so后,main函数编译时就不需要再去链接了)
编译main函数 : g++ main.cc -o mian.cc -L./ -lcopt