zoukankan      html  css  js  c++  java
  • 自己给libnfc库添加一个头文件 很波折

    //log.h

    /*
    - * Free/Libre Near Field Communication (NFC) library * * Libnfc historical contributors: * Copyright (C) 2009 Roel Verdult * Copyright (C) 2009-2013 Romuald Conty * Copyright (C) 2010-2012 Romain Tartière * Copyright (C) 2010-2013 Philippe Teuwen * Copyright (C) 2012-2013 Ludovic Rousseau * Additional contributors of this file: * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/> */ #ifndef __LOG_H__ #define __LOG_H__ #ifdef HAVE_CONFIG_H # include "config.h" #endif // HAVE_CONFIG_H #include "nfc-internal.h" //#include "MycharToInt.h" // by wangyong 2013-6-8
    //上面就是我准备添加的头文件,头文件里定义了一个函数int MycharToInt(char *pch) ;
    //
    MycharToInt.c实现了这个函数
    
    

    #define NFC_LOG_PRIORITY_NONE 0 #define NFC_LOG_PRIORITY_ERROR 1 #define NFC_LOG_PRIORITY_INFO 2 #define NFC_LOG_PRIORITY_DEBUG 3 #define NFC_LOG_GROUP_GENERAL 1 #define NFC_LOG_GROUP_CONFIG 2 #define NFC_LOG_GROUP_CHIP 3 #define NFC_LOG_GROUP_DRIVER 4 #define NFC_LOG_GROUP_COM 5 #define NFC_LOG_GROUP_LIBUSB 6 /* To enable log only for one (or more) group, you can use this formula: log_level = NFC_LOG_PRIORITY(main) + NFC_LOG_PRIORITY(group) * 2 ^ (NFC_LOG_GROUP(group) * 2) Examples: * Main log level is NONE and only communication group log is set to DEBUG verbosity (for rx/tx trace): LIBNFC_LOG_LEVEL=3072 // 0+3072 * Main log level is ERROR and driver layer log is set to DEBUG level: LIBNFC_LOG_LEVEL=769 // 1+768 * Main log level is ERROR, driver layer is set to INFO and communication is set to DEBUG: LIBNFC_LOG_LEVEL=3585 // 1+512+3072 */ //int log_priority_to_int(const char* priority); const char *log_priority_to_str(const int priority); char *getDataFrom_acBuf; #if defined LOG # ifndef __has_attribute # define __has_attribute(x) 0 # endif # if __has_attribute(format) || defined(__GNUC__) # define __has_attribute_format 1 # endif void log_init(const nfc_context *context); void log_exit(void); void log_put(const uint8_t group, const char *category, const uint8_t priority, const char *format, ...) # if __has_attribute_format __attribute__((format(printf, 4, 5))) # endif ; #else // No logging #define log_init(nfc_context) ((void) 0) #define log_exit() ((void) 0) #define log_put(group, category, priority, format, ...) do {} while (0) #endif // LOG /** * @macro LOG_HEX * @brief Log a byte-array in hexadecimal format * Max values: pcTag of 121 bytes + ": " + 300 bytes of data+ "\0" => acBuf of 1024 bytes */ # ifdef LOG # define LOG_HEX(group, pcTag, pbtData, szBytes) do { \ size_t __szPos; \ char __acBuf[4096]; \ size_t __szBuf = 0; \ if ((int)szBytes < 0) { \ fprintf (stderr, "%s:%d: Attempt to print %d bytes!\n", __FILE__, __LINE__, (int)szBytes); \ log_put (group, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s:%d: Attempt to print %d bytes!\n", __FILE__, __LINE__, (int)szBytes); \ abort(); \ break; \ } \ snprintf (__acBuf + __szBuf, sizeof(__acBuf) - __szBuf, "%s: ", pcTag); \ __szBuf += strlen (pcTag) + 2; \ for (__szPos=0; (__szPos < (size_t)(szBytes)) && (__szBuf < sizeof(__acBuf)); __szPos++) { \ snprintf (__acBuf + __szBuf, sizeof(__acBuf) - __szBuf, "%02x ",((uint8_t *)(pbtData))[__szPos]); \ __szBuf += 3; \ } \ log_put (group, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "acBuf: %s endofacBuf", __acBuf); \ getDataFrom_acBuf = __acBuf; \ if (getDataFrom_acBuf[0] == 'R' && getDataFrom_acBuf[1] == 'X' && getDataFrom_acBuf[2] == 'X' && getDataFrom_acBuf[3] == ':' \ && getDataFrom_acBuf[4] == ' ' && getDataFrom_acBuf[5] == 0x30 && getDataFrom_acBuf[6] == 0x30 && getDataFrom_acBuf[7] == ' ' \ && getDataFrom_acBuf[8] == 0x34 && getDataFrom_acBuf[9] == 0x33 && getDataFrom_acBuf[10] == ' ' && getDataFrom_acBuf[11]==0x32 \ && getDataFrom_acBuf[12]==0x30 && getDataFrom_acBuf[13]==' ' && getDataFrom_acBuf[14]==0x30 && getDataFrom_acBuf[15]==0x30) { \ printf("log.h __acBuf:%s\n",__acBuf); \ int len = strlen(__acBuf); \ int i; \ for (i=68;i<len;) \ { \ char tmp[3]; \ strncpy(tmp,__acBuf+i,2); \ tmp[2]='\0'; \ int res = MycharToInt(tmp); \ //这里对 MycharToInt 调用
    printf("%c",res); \ i += 3; \ } \ }\ } while (0); # else # define LOG_HEX(group, pcTag, pbtData, szBytes) do { \ (void) group; \ (void) pcTag; \ (void) pbtData; \ (void) szBytes; \ } while (0); # endif #endif // __LOG_H__

    头文件MycharToInt.h:

    #ifndef __MYCHARTOINT_H__
    #define __MYCHARTOINT_H__
    #endif
    
    int MycharToInt(char *pch) ;

    MycharToInt.c:

    #include "MycharToInt.h"
    
    //十六进制字符数组转为int型 int MycharToInt(char *pch) { int result=0; char ch1 = pch[0]; char ch2 = pch[1]; int shiwei,gewei; switch(ch1) { case '0': shiwei=0; break; case '1': shiwei=1; break; case '2': shiwei=2; break; case '3': shiwei=3; break; case '4': shiwei=4; break; case '5': shiwei=5; break; case '6': shiwei=6; break; case '7': shiwei=7; break; case '8': shiwei=8; break; case '9': shiwei=9; break; case 'a': shiwei=10; break; case 'b': shiwei=11; break; case 'c': shiwei=12; break; case 'd': shiwei=13; break; case 'e': shiwei=14; break; case 'f': shiwei=15; break; } switch(ch2) { case '0': gewei=0; break; case '1': gewei=1; break; case '2': gewei=2; break; case '3': gewei=3; break; case '4': gewei=4; break; case '5': gewei=5; break; case '6': gewei=6; break; case '7': gewei=7; break; case '8': gewei=8; break; case '9': gewei=9; break; case 'a': gewei=10; break; case 'b': gewei=11; break; case 'c': gewei=12; break; case 'd': gewei=13; break; case 'e': gewei=14; break; case 'f': gewei=15; break; } result = shiwei*16+gewei; return result; }

    添加好了,然后在libnfc目录下:

    sudo ./configure --with-drivers=pn532_uart --enable-debug

    sudo make clean all

    sudo make install

    总算没有报错,之前一直报错说“multiple definition of MychatToInt”,因为之前我没有MychatToInt.c这个文件,函数定义和实现都在MychatToInt.h里。

    虽然这里没报错,但是运行这个库上的函数就报错了,在libllcp的examples下:

    sudo ./npp-server

    就会报“ no symbol of MychatToInt”。

    最后解决方法是,在log.h包含的头文件nfc-internal.h里添加我们自己定义的函数:

    int MycharToInt(char *pch) ;

    然后在nfc-internal.c里面加上我们的 MycharToInt(char *pch)具体实现。

    这样之后在log.h里就能使用 MycharToInt 这个方法了。

  • 相关阅读:
    一个C++程序员学习C#语言
    C#入门教程笔记
    完全卸载mysql 停止服务、卸载相关程序、删除注册表
    C++结构简介
    babun,windows shell
    无限极设计以及随意移动节点(树结构)
    springboot 配置访问外部静态资源详解
    mysql8+keepalived 双主高可用搭建
    mysql 双主复制搭建
    mysql 主备搭建
  • 原文地址:https://www.cnblogs.com/duanguyuan/p/3127396.html
Copyright © 2011-2022 走看看