模块划分

合理编写模块的 demo.h、demo.cc

下例为C++为后端服务编写的探活检测服务

  • health_server.h
#ifndef  HEALTH_SERVER_H
#define HEALTH_SERVER_H
#include <iostream>
//#include "utils/flags.h"
void health_server( const std::string &health_host , const std::string &health_port );
#endif
  1. 必加#ifndef: 预处理功能(宏定义,文件包含和条件编译)中的条件编译,主要用来防止重复编译,“multiple define”错误
  2. .h中的其它预处理功能include***,遵循最小使用原则,如上例 仅使用std::string,则对应 仅加入#include<iostream>
  3. .cc文件也需要添加 对应#include "health_server.h" 文件

    health_server.cc
#include <dirent.h>
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> #include <atomic>
#include <memory>
#include <thread>
#include <iostream>
#include "health_server.h"
#include "./http_server.h" mg_serve_http_opts oppo::HttpServer::s_server_option;
std::atomic<bool> server_stop(true);
std::unordered_map<std::string, oppo::ReqHandler> oppo::HttpServer::s_handler_map;
std::unordered_set<mg_connection *> oppo::HttpServer::s_websocket_session_set; static bool HandleHealth(std::string url, std::string body,
mg_connection *c, oppo::OnRspCallback rsp_callback)
{
if (!server_stop)
rsp_callback(c, "OK");
else
rsp_callback(c, "STOP"); return true;
} static bool HandleStop(std::string url, std::string body,
mg_connection *c, oppo::OnRspCallback rsp_callback)
{
if (!server_stop) {
server_stop = true;
rsp_callback(c, "STOP OK");
}
else {
rsp_callback(c, "IS STOP");
} return true;
} static bool HandleStart(std::string url, std::string body,
mg_connection *c, oppo::OnRspCallback rsp_callback)
{
if (server_stop) {
server_stop = false;
rsp_callback(c, "START OK");
}
else {
rsp_callback(c, "IS START");
} return true;
} void health_server( const std::string &health_host , const std::string &health_port ){
std::string health_address(health_host + ":" + health_port);
auto health_server = std::unique_ptr<oppo::HttpServer>(new oppo::HttpServer);
std::thread http_thread([&health_server, health_address]() {
health_server->Init(health_address);
health_server->AddHandler("/asr/health", HandleHealth);
health_server->AddHandler("/asr/stop", HandleStop);
health_server->AddHandler("/asr/start", HandleStart);
health_server->Start();
});
http_thread.join();
}

依赖库部分:

  • gflags使用DEFINE_int32(port, 10086, "grpc listening port")
#include <memory>
#include <gflags/gflags.h>
DEFINE_int32(port, 10086, "grpc listening port");
  • atomic:原子变量,一般在多线程作为锁使用 依赖:#include <atomic>

ERROR

  • 未定义的引用
  • 命令行编写:g++ speech-service-main.cc http_server.cc mongoose.cc -lpthread,严格按照依赖顺序写,-l链接动态库
  • CMakeList.txt:
  add_library(health_server STATIC
http/health_server.cc
http/http_server.cc
http/mongoose.cc
)
target_link_libraries(health_server PUBLIC pthread) add_executable(grpc_server_main bin/grpc_server_main.cc)
target_link_libraries(grpc_server_main PUBLIC health_server)

合理编写C++模块(.h、.cc)的更多相关文章

  1. [转]使用 C 编写 Lua 模块

    Lua 作为一种小巧的语言,一般都是嵌入到 C/C++ 中作为扩展语言,但是也可以作为独立的脚本语言使用,并且可以使用 C/C++ 编写扩展模块.在参考资料 [1] 中有怎样用 C/C++ 编写模块的 ...

  2. 为Lua5.3编写C模块简单示例

    为Lua5.3编写C模块简单示例 一.编译安装Lua5.3 MSVC 命令行安装脚本: @echo off md bin md lib md include cd src cl /c /nologo ...

  3. Saltstack_使用指南09_远程执行-编写执行模块

    1. 主机规划 salt 版本 [root@salt100 ~]# salt --version salt (Oxygen) [root@salt100 ~]# salt-minion --versi ...

  4. 【转】PowerShell入门(十一):编写脚本模块

    转至:http://www.cnblogs.com/ceachy/archive/2013/03/08/PowerShell_Script_Module.html 现在通过编写模块就可以在PowerS ...

  5. 用Perl编写Apache模块续二 - SVN动态鉴权实现SVNAuth 禅道版

    代码地址:https://code.csdn.net/x3dcn/svnauth 以禅道项目管理系统的数据库结构为标准,实现了可用的svn authz验证功能. 以用户名.密码.项目的acl开发程度o ...

  6. 用Perl编写Apache模块

    前言 Apache被许多大流量网站所嫌弃,但很多企业级的场景则更为适用. Apache httpd 从 2.0 之后,已经不仅仅局限于一个 http 的服务器,更是一个完善而强大.灵活而健壮且容易扩展 ...

  7. 使用AndroidStudio编写APICloud模块需要注意的地方,解决模块未定义。

    在新的版本下,使用AndroidStudio编写APICloud模块,已经非常简单了,解决模块未定义,最重要的就是要先看官方的视频! 注意在模块的module.json中name很重要,建议做到三统一 ...

  8. 使用F#编写PowerShell模块

    ▲F#和PowerShell模块 作为可能是人类世界最强大的Shell,PowerShell最大的特点是能够直接在命令间传递.NET对象,而支持这种能力的命令被称作cmdlet.自己编写PowerSh ...

  9. 26-ESP8266 SDK开发基础入门篇--编写WIFI模块 SmartConfig/Airkiss 一键配网

    https://www.cnblogs.com/yangfengwu/p/11427504.html SmartConfig/Airkiss 配网需要APP/微信公众号,这节大家先使用我做好的APP/ ...

随机推荐

  1. Entry键值对对象和Map集合遍历键值对方式

    我们已经知道,Map中存放的是两种对象,一种称为key(键),一种称为value(值),它们在在IMap 中是一一对应关系, 这一对对象又称做Map 中的一个Entry(项).Entry将键值对的对应 ...

  2. ubuntu 20.04 安装 vim8.2

    由于ubuntu 20.04自带的vim版本比较老了,有些新装的插件适配不上,所以需要安装最新版本的vim.在网上找了很久也没有比较官方的安装教程所以记录一下. 安装依赖库 sudo apt inst ...

  3. idea 生成方法注释

    /* * * @description: * @author: xuetong.yang * @date: $date$ $time$ $params$ * @return: $return$ */ ...

  4. HTML及HTTP协议

    web服务的过程: 浏览器发请求 --> HTTP协议 --> 服务端接收请求 --> 服务端返回响应 --> 服务端把HTML文件内容发给浏览器 --> 浏览器渲染页面 ...

  5. 使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏 🌐

    声明:本文涉及图文和模型素材仅用于个人学习.研究和欣赏,请勿二次修改.非法传播.转载.出版.商用.及进行其他获利行为. 背景 近期工作有涉及到数字大屏的需求,于是利用业余时间,结合 Three.js ...

  6. 性能浪费的日志案例和使用Lambda优化日志案例

    有些场景的代码执行后,结果不一定会被使用,从而造成性能浪费.而Lambda表达式是延迟执行的,这正好可以作为解决方案,提升性能 性能浪费的日志案例 日志可以帮助我们快速的定位问题,记录程序运行过程中的 ...

  7. 在Webpack 5 中如何进行 CSS 常用配置?

    本文摘要:主要通过实操讲解运用Webpack 5 CSS常用配置的方法步骤 前文已谈到可以通过配置 css-loader 和 style-loader,使 webpack5 具有处理 CSS 资源的能 ...

  8. 获取某个html元素相对于视窗的位置集合

    getBoundingClientRect() getBoundingClientRect()获取元素位置,这个方法没有参数 getBoundingClientRect()用于获得页面中某个元素的左, ...

  9. Serverless之Knative部署应用实例;

    1.什么是Knative? Knative是Google2018的Google Cloud Next大会上发布的一款基于kubernetes的Serverless框架. knative的目的是在kub ...

  10. mosquitto使用的基本流程以及一些遇见的问题

    改配置文件 以记事本的方式打开mosquitto.conf更改部分内容,找到# listener port-number [ip address/host name/unix socket path] ...