cmake应用笔记


CMakeLists.txt文件格式:

# 设置运行此配置文件所需的CMake最低版本
cmake_minimum_required(VERSION 3.6)    
# 设置项目名称
project(ProName)
# 添加一个可执行文件
add_executable(ExeName main.cpp)

target_link_libraries(rtmp ${XHR_LIBS} ${OpenCV_LIBS})

编译:

$ cd build
$ cmake ..
$ make

指定头文件路径: 可多次调用

include_directories(/usr/include)
include_directories(${PROJECT_SOURCE_DIR}/include)

指定库文件路径: 可多次调用

link_directories(${PROJECT_SOURCE_DIR})

添加编译参数

add_compile_options(-wall -std=c++11 -o2)

设置用户自定义变量: 多次设置同一变量会被覆盖

set(XHR_SRC  main.cpp utils.cpp)

环境变量:

${PROJECT_SOURCE_DIR}  - 源代码目录
${PROJECT_BINARY_DIR}  - 生成文件目录

设置OpenCV

find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIBS})

设置CUDA

# cuda
include_directories(/usr/local/cuda/include)
link_directories(/usr/local/cuda/lib64)

find_package会导出xxx_INCLUDE_DIRS和xxx_LIBS两个相关的变量,用于编译
find_package区分大小写(OpenCV, CUDA)


宏定义说明: 通过cmake命令指定的宏,必须再CMakeLists中重新定义,才能实现全局宏定义

if(HELLO)
message(STATUS "Hello")
add_definitions(-DHELLO=${HELLO})
endif(HELLO)
$ cmake -DHELLO=1 ..

提示信息:

message(STATUS "HelloWorld")

查找所有源文件

aux_source_directory(. DIR_SRCS)

生成动态库: libxxx.so
add_library(xxx SHARED bbb.c)

生成静态库: libxxx.a

add_library(xxx STATIC aaa.c)

生成可执行文件:

add_executable(xxx SHARED aaa.c)
# 需要链接
target_link_libraries(xxx ${USER_LIBS})

- 阅读全文 -

Linux系统应用--USB设备文件绑定


本文主要实现了针对同一厂家USB设备的区分(通过不同的USB口区分)

先查看USB设备的KERNEL信息:

$ udevadm info --attribute-walk --name=/dev/ttyUSB0 |grep KERNELS
    KERNELS=="ttyUSB0"
    KERNELS=="1-5:1.0"
    KERNELS=="1-5"
    KERNELS=="usb1"
    KERNELS=="0000:00:14.0"
    KERNELS=="pci0000:00"

新建规则文件:/etc/udev/rules.d/usbserial

# 通过不同的KERNELS(端口)来实现不同的绑定
KERNEL=="ttyUSB*",KERNELS=="1-5.1.0", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", MODE:="0777", SYMLINK+="gps1"
KERNEL=="ttyUSB*",KERNELS=="1-5.2.0", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", MODE:="0777", SYMLINK+="gps2"

通过不同的端口来绑定网卡名称:

KERNEL=="eth*",KERNELS=="2-3.1.2",NAME="eth8"
KERNEL=="eth*",KERNELS=="2-3.1.3",NAME="eth9"

也可以指定厂商及设备ID

KERNEL=="eth*",KERNELS=="2-3.1.3",ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="8153",NAME="usb5"

网卡没有设备文件,可通过/sys/bus/usb中的idProduct来查看。

- 阅读全文 -

Linux应用程序--inotify事件监测


#include <stdio.h>   
#include <unistd.h>   
#include <sys/select.h>   
#include <errno.h>   
#include <sys/inotify.h>
#include <string.h>

#if 0
#define IN_ACCESS           0x00000001  /* 1 << 0  File was accessed         */
#define IN_MODIFY           0x00000002  /* 1 << 1  File was modified         */
#define IN_ATTRIB           0x00000004  /* 1 << 2  Metadata changed          */
#define IN_CLOSE_WRITE      0x00000008  /* 1 << 3  Writtable file was closed */
#define IN_CLOSE_NOWRITE    0x00000010  /* 1 << 4  Unwrittable file closed   */
#define IN_OPEN             0x00000020  /* 1 << 5  File was opened           */
#define IN_MOVED_FROM       0x00000040  /* 1 << 6  File was moved from X     */
#define IN_MOVED_TO         0x00000080  /* 1 << 7  File was moved to Y       */
#define IN_CREATE           0x00000100  /* 1 << 8  Subfile was created       */
#define IN_DELETE           0x00000200  /* 1 << 9  Subfile was deleted       */
#define IN_DELETE_SELF      0x00000400  /* 1 << 10 Self was deleted          */
#define IN_MOVE_SELF        0x00000800  /* 1 << 11 Self was moved           */

/* the following are legal events.  they are sent as needed to any watch */
#define IN_UNMOUNT          0x00002000  /* Backing fs was unmounted */
#define IN_Q_OVERFLOW       0x00004000  /* Event queued overflowed */
#define IN_IGNORED          0x00008000  /* File was ignored */
/* helper events */
#define IN_CLOSE        (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* close */
#define IN_MOVE         (IN_MOVED_FROM | IN_MOVED_TO) /* moves */
/* special flags */
#define IN_ONLYDIR         0x01000000   /* only watch the path if it is a directory */
#define IN_DONT_FOLLOW     0x02000000   /* don't follow a sym link */
#define IN_EXCL_UNLINK     0x04000000   /* exclude events on unlinked objects */
#define IN_MASK_CREATE     0x10000000   /* only create watches */
#define IN_MASK_ADD        0x20000000   /* add to the mask of an already existing watch */
#define IN_ISDIR           0x40000000   /* event occurred against dir */
#define IN_ONESHOT         0x80000000   /* only send event once */
#endif

char mask_msg[][100] = {
    "File was accessed        ",
    "File was modified        ",
    "Metadata changed         ",
    "Writtable file was closed",
    "Unwrittable file closed  ",
    "File was opened          ",
    "File was moved from X    ",
    "File was moved to Y      ",
    "Subfile was created      ",
    "Subfile was deleted      ",
    "Self was deleted         ",
    "Self was moved           ",    
};

/// 通过掩码显示操作内容
int show_mask_info(int mask)
{
    int i;
    for(i = 0; i < 16; i++)
    {
        if(mask & (1 << i))
        {
            printf("%s\r\n", mask_msg[i]);
        }       
    }   
    return 0;
}

//、从buf中取出一个事件。
static void inotify_event_handler(struct inotify_event *event)
{
  if (strcmp(event->name, "aaa.txt") != 0)
  {
    return;
  }
  printf("filename:[%s]", event->name);
  printf("mask: 0x%08x ", event->mask);
  show_mask_info(event->mask);
}

int main(int argc, char **argv)
{
  int i;
  if (argc != 2)
  {
    printf("Usage: %s <file/dir>\n", argv[0]);
    return -1;
  }
  unsigned char buf[1024]     = {0};
  struct inotify_event *event = NULL;
  int fd = inotify_init();                                //初始化
  int wd                      = inotify_add_watch(fd, argv[1], IN_ALL_EVENTS); //监控指定文件的ALL_EVENTS。

  int len;
  int index = 0;
  while (1)
  {
    while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR))
    {
    }
    index = 0;
    while (index < len)
    {
      event = (struct inotify_event *)(buf + index);
      inotify_event_handler(event);                       //获取事件。
      index += sizeof(struct inotify_event) + event->len; //移动index指向下一个事件。
    }
  }
  inotify_rm_watch(fd, wd); //删除对指定文件的监控。
  return 0;
}

- 阅读全文 -

Windows常用命令列表


任务管理器: taskmgr.exe
远程桌面 : mstsc
计划任务 :taskschd.msc


查看当前进程:

tasklist

查看指定进程:

tasklist | findstr "SerialLogs"

- 阅读全文 -

Windows应用--常用批处理


字符串处理:

echo %date:~0,4%%date:~5,2%%date:~8,2%

显示时间:

echo %date% %time%

显示主机名:

echo %computername%

延时处理:

choice /t 5 /d y /n >nul

t - 指定超时时间(秒)
d - 指定默认选项
n - 隐藏选项列表

延时处理:带倒计时

timeout /t 5

删除60天以前的文件

forfiles /p D:\logs /s /m *.* /d -60 /c "cmd /c del @path"

p - 指定路径(默认为当前目录)
s - 递归子目录
m - 指定搜索掩码
d - 上次修改日期大于或等于(+),小于或等于(-)
c - 对符合文件所执行的命令(需要用双引号圈起来)


判断语句:

if %ERRORLEVEL% equ 0 (
    echo Successful
)else(
    echo Failed
)

注意事项:else必须跟if的括号在同一行


比较:
相等:equ
大于:grt
小于等于: leq
大于等于: grt


for函数说明:

for %%i in (list) do (cmd2) 

从文件中读取每一行并显示:

for /f %%i in (awkfile.awk) do (
   echo %%i
)

f 延迟参数(从文件中读出),如果不加f则直接对列表内容展开

找出最新的文件: 通过DIR命令排序

for /f %%a in ('dir M*.bin /o-d /tc /b') do (
echo 文件名称: %%a
set filename=%%a
goto next
)
:next
echo %filename%

b - 只显示文件名
o - 排序 D日期,N名称
t - 时间域,C创建时间

获取指定格式的文件并去除扩展名

for /f %%i in ('dir *.pro /b') do (
    set NAME=%%~ni
    echo %NAME%
)

循环指定次数: —L参数 (start, step end)

for /L %%i in (1,1,10) do (
    echo %%i
)

- 阅读全文 -


Copyright©2024 春天花会开, All Rights Reserved. Email: webmaster@oroct.com