模块划分

合理编写模块的 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. 挑战30天写操作系统-day3-进入32位模式并导入C语言

    目录 1.制作真正的IPL IPL:启动区,启动程序装载器完整代码: ; haribote-ipl ; TAB=4 CYLS EQU 10 ; 声明CYLS=10 ORG 0x7c00 ; 指明程序装 ...

  2. 禁用Chrome自动更新

    删除下Update目录 C:\Program Files (x86)\Google\Chrome\

  3. APISpace 未来7天生活指数API接口 免费好用

    随着经济的发展,我们的生活水平在不断的提高,生活指数在我们的生活中也越来越受到关注,根据当天的生活指数,我们就可以知道在今天我们可以干什么比较好.   未来7天生活指数API,支持国内3400+个城市 ...

  4. 千万小心,99%的Java程序员会踩这些坑

    前言 作为Java程序员的你,不知道有没有踩过一些基础知识的坑. 有时候,某个bug查了半天,最后发现竟然是一个低级错误. 有时候,某些代码,这一批数据功能正常,但换了一批数据就出现异常了. 有时候, ...

  5. filebeat + logstash 日志采集链路配置

    1. 概述 一个完整的采集链路的流程如下: 所以要进行采集链路的部署需要以下几个步聚: nginx的配置 filebeat部署 logstash部署 kafka部署 kudu部署 下面将详细说明各个部 ...

  6. win10搜索功能用不了

    这玩意搞了我今天,直接裂开!系统更新根本解决不了 好在查了相关资料才知道,原来微软在 Win10 的更新中,将搜索功能和语音助手 Cortana 进行了拆分,搜索成了一个独立的功能,还好有外媒发现问题 ...

  7. 【docker专栏7】容器自启动与守护进程停止后容器保活

    本文为大家介绍容器自启动以及docker 守护进程挂掉或者docker升级的情况下,如何保证容器服务的正常运行.主要包含三个部分 一.守护进程开机自启 在我们安装docker的时候,介绍过启动dock ...

  8. Thread类的常用方法_获取线程名称的方法和Thread类的常用方法_设置线程名称的方法

    构造方法: public Thread();分配一个新的线程对象 public Thread(String name);分配一个指定名字的新的线程对象 public Thread(Runnable t ...

  9. 心动不如行动,基于Docker安装关系型数据库PostgrelSQL替代Mysql

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_171 最近"全栈数据库"的概念甚嚣尘上,主角就是PostgrelSQL,它最近这几年的技术发展不可谓不猛,覆盖 ...

  10. Python基础之dict和set的使用

    dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言种也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例子,假设要根据同学的名字 ...