分类: C

  • No CMAKE_CXX_COMPILER could be found.

    错误信息

    在使用cmake 编译 flex&bison程序时,使用cmake的project()指令,编译报以下错误,错误具体信息:

    CMake Error at CMakeLists.txt:3 (project):
      No CMAKE_CXX_COMPILER could be found.
    
      Tell CMake where to find the compiler by setting either the environment
      variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
      to the compiler, or to the compiler name if it is in the PATH.
    
    
    -- Configuring incomplete, errors occurred!

    原因是:cmake 的project() 命令在没有指定 LANGUAGES 选项时,默认是C和CXX(注意cmake 3.0之前的版本不支持LANGUAGES 关键字),CXX编译时需要指定 g++编译器。

    解决方法

    有2种方式:

    方式一

    在project() 命令中指定LANGUAGES 选项为C:

    project(test1 LANGUAGES C)

    方式二

    安装g++ :

    apt install g++
  • 使用cmake编译flex

    环境

    Linux系统

    cmake版本:cmake version 3.25.1

    make版本:GNU Make 4.3

    flex版本:flex 2.6.4

    flex源文件

    catcot.l 文件

    %{
        #include <stdio.h>    
    %}
    
    %%
    c.t { printf("mumble mumble"); }
    cot { printf("portable bed"); }
    cat { printf("thankless pet"); }
    cats { printf("anti-herd");}
    
    %%
    
    int main()
    {
        /* 调用获取token */
        yylex();
        return 0;
    }

    CMakeLists.txt文件

    # 指定cmake 最低版本号
    cmake_minimum_required(VERSION 3.20)
    
    # 指定项目名称 和 语言
    project(catcot1 LANGUAGES C)
    
    # 查找依赖包
    find_package(FLEX)
    
    # 使用宏定义生成规则
    FLEX_TARGET(catcotScanner catcot.l ${CMAKE_CURRENT_BINARY_DIR}/catcot.lex.c)
    
    # 设置主要包含的c文件
    set(MAIN_SRC ${CMAKE_CURRENT_BINARY_DIR}/catcot.lex.c)
    
    # 使用源文件生成可以执行文件
    add_executable(catcotTest ${MAIN_SRC})
    

    编译

    # 第一步
    mkdir build
    # 第二步
    cd build
    # 第三步
    cmake ../
    # 第四步
    make 

    问题

    在编译部分 第四步 时会出现如下的错误提示:

    /usr/bin/ld: CMakeFiles/catcotTest.dir/catcot.lex.c.o: in function `yylex':
    catcot.lex.c:(.text+0x4e0): undefined reference to `yywrap'
    /usr/bin/ld: CMakeFiles/catcotTest.dir/catcot.lex.c.o: in function `input':
    catcot.lex.c:(.text+0x10de): undefined reference to `yywrap'
    collect2: error: ld returned 1 exit status
    make[2]: *** [CMakeFiles/catcotTest.dir/build.make:101: catcotTest] Error 1
    make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/catcotTest.dir/all] Error 2
    make: *** [Makefile:91: all] Error 2

    问题解决

    解决上面的undefined reference to `yywrap’问题,有以下三种方法:

    • 方法1:在 catcot.l 文件头部添加:%option noyywrap
    • 方法2:在 catcot.l 文件中添加自己的yywrap()函数,返回值1
    int yywrap()
    {
       return 1;
    }
    • 方法3:在CMakeLists.txt文件末尾追加:
    # 添加 libfl.a 库
    find_library(LEX_LIB fl)
    # 链接 libfl.a 库
    TARGET_LINK_LIBRARIES(catcotTest ${LEX_LIB})

    这样可以使用默认flex的库libfl.a 生成的默认yywrap函数。

    以上3种方法,均已验证是可以的。

    推荐使用第 1 种方法。

  • 使用Flex&Bison时可能的问题和解决方法

    问题1:cannot find ll

    意思是找不到 libl.a库文件。

    问题2:cannot find fl

    意思是找不到 libfl.a库文件。

    问题3:cannot find ly

    意思是找不到 liby.a库文件。

    解决方法

    下面是在Ubuntu系统上的解决方法介绍:

    1. 安装apt-file:apt install apt-file
    2. 使用apt-file命令查找库在哪个工具包中:
    apt-file search libl.a
    apt-file search libfl.a
    apt-file search liby.a

    查找结果:

    3. 安装对应的工具包就可以了

    apt install libfl-dev libbison-dev

    apt-cache 是使用包名或包描述 检索仓库。

    apt-file 是 使用包中文件名检索存储库(工具包名)。

    Linux centos 对应的是 yum whatprovides ,具体使用方法请自行查阅。

  • 语法分析器Bison安装

    Bison源码地址:https://github.com/akimd/bison

    Linux环境安装

    方式一

    到Bison官网上给定的下载地址:https://ftp.gnu.org/gnu/bison/

    下载完成后,解压后进入文件夹目录按照INSTALL文件内容中的安装步骤即可。

    • 执行配置:./configure ,执行后可能会检查出来缺少依赖,按照提示的内容,安装缺少的依赖就可以了。
    • 执行编译和检查:make && make check
    • 执行安装:sudo make install ,默认是安装在 /usr/local/bin目录下。
    • 安装成功后,输入bison --version ,输出版本号就表示成功了。

    方式二

    直接命令安装:sudo apt install bison

    Windows环境安装

    在上一篇文章中,Flex安装中已经说明安装了MinGW。下面直接说安装Bison。

    选择最新的版本下载,并选择安装到和Flex一个目录中。之前flex的bin目录已经添加到了系统环境path中了。这里直接打开cmd控制台,输入:bison --version 输出正确的版本即成功安装。

  • 词法分析器Flex安装

    Flex源码地址:https://github.com/westes/flex/

    Linux环境环境安装

    方式一

    • 环境说明

    Linux Debian x86_64

    注意:系统需要安装 cmake 和 make.

    • 安装依赖:sudo apt install help2man m4
    • 下载flex压缩包:wget https://github.com/westes/flex/releases/download/v2.6.4/flex-2.6.4.tar.gz
    • 解压文件:sudo tar -xvf flex-2.6.4.tar.gz
    • 进入文件夹:cd flex-2.6.4
    • 查看INSTALL.md文件中的安装步骤
    • 执行配置命令:./configure 会把列出提示缺少的依赖包,需要把这些缺少的依赖逐个安装一下就行。help2man,m4等
    • 执行编译和检查:make && make check
    • 执行安装:sudo make install 默认是安装在/usr/local/bin 路径下面的。
    • 检查是否安装成:flex --version 成功输出版本号,则是成功。

    方式二

    直接安装:sudo apt install flex

    Windows环境安装

    • 环境说明:系统win10,需要安装MinGW(Minimalist GUN on Windows),是GCC的windows版本,MinGW有2个发行版本:MinGW-64 和 MinGW。区别在于MinGW只能编译生成32位可执行程序,而MinGW-64则可以 编译生成64位和32位的可执行程序。下载地址:https://sourceforge.net/projects/mingw-w64/files/

    可以选择以下2种方式安装,如下图:

    我选择的是【x86_64-win32-sjlj】,解压后直接将 bin目录配置到系统环境变量path中,然后将mingw32-make.exe重命名为make.exe。

    目前这里只提供了32位的工具,如果需要64位的文件需要自己手动编译源码生成。

    安装完成后,需要将安装目录下的bin文件夹目录添加到系统环境变量path中。

    完成系统环境配置后打开cmd控制台,输入:flex --version 可以打印出版本号。

  • C语言安全地释放指针

    最近在读有关C语言指针方面的书籍,看到有关malloc系列函数申请内存释放使用宏定义的方式。自己也动手试了一下,感觉还不错记录一下分享给小伙伴们。

    实现和应用

    #include <stdlib.h>
    #include <stdio.h>
    
    void safeFreePtr(void **p_ptr)
    {
        if (p_ptr != NULL && *p_ptr != NULL)
        {
            free(*p_ptr);
            *p_ptr = NULL;
        }
    }
    
    /** 宏定义 */
    #define safeFree(ptr)  safeFreePtr((void **)&(ptr))
    
    int main(int argc, char const *argv[])
    {
        int *a = (int *)malloc(sizeof(int));
        *a = 10;
        printf("Before: %p\n", a);
        safeFree(a);
        printf("After: %p\n", a);
        safeFree(a);
        return 0;
    }

    运行结果:

    Before: 0x5634b2e342a0
    After: (nil)

    可以看出第二次safeFree时打印是:nil,程序没有出错。

    对于申请的指针,使用free函数释放时只能free一次,第二次free会报错。

    指针free释放后,一定要记得把指针置为NULL,防止悬挂指针。

    好处

    如果程序中是直接调用safeFreePtr函数是需要显示转换为 void** 指针,若不显示转换则会报警告。在使用宏调用safeFreePtr函数可以避免强制转换。

  • 查看系统默认内存对齐字节数

    本文使用C语言查看系统默认内存对齐字节数。

    #pragma pack(show)

    实现当前系统默认对齐字节数,是以警告的方式显示。

    使用vscode编辑器,写入以下代码:

    // 默认是8
    #pragma pack(show)
    
    int main(int argc, char const *argv[])
    {
       return 0;
    }

    运行上面的代码后,会在vscode的控制台 【PROBLEMS】tab中显示下面的警告提示信息。

    value of #pragma pack(show) == 8 [Semantic Issue]

    表示当前内存对齐字节数8.

  • C基于链表实现栈

    本篇内容中的链表是之前的文章中实现的,请参阅文章 C-单链表的实现 。

    栈的特性

    先进后出(First-In-Last-Out, FILO).

    像队列实现一样,有2个主要的操作:

    一)push 操作将元素入栈。

    二)pop 操作将元素出栈。

    栈的实现

    基于单链表实现,push 入栈操作就是往链表的头部添加数据元素,pop 出栈操作就是从链表的头部删除数据元素。

    以下是栈的定义以及主要操作:

    // 定义栈
    typedef LinkedList Stack;
    // 初始化栈
    void initStack(Stack *stack)
    {
        init_linkedlist(stack);
    }
    // 入栈,在链表头部添加元素
    void push(Stack *stack, void *data)
    {
        addHead(stack, data);
    }
    // 出栈,从链表头部删除元素
    void* pop(Stack *stack)
    {
        pNode node = stack->head;
        void* data;
        if (stack->head == NULL) {
            data = NULL;
        } else if (stack->head == stack->tail) {
            stack->head = stack->tail = NULL;
            data = node->data;
            free(node);
        } else {
            stack->head = node->next;
            data = node->data;
            free(node);
        }
    
        return data;
    }

    示例说明

        Stack stack;
        // 初始化
        initStack(&stack);
        int a = 20;
        int b = 30;
        int c = 40;
        int *ptr_1 = &a;
        int *ptr_2 = &b;
        int *ptr_3 = &c;
    
        push(&stack, ptr_1);
        push(&stack, ptr_2);
        push(&stack, ptr_3);
    
        int *ptr = NULL;
        for (int i = 0; i < 3; i++)
        {
            ptr = (int *)pop(&stack);
            printf("Popped %d\n", *ptr);
        }

    运行结果:

    Popped 40
    Popped 30
    Popped 20


    以上内容如有错误,欢迎留言指出交流或者关注公众号交流。

  • C基于链表实现队列

    本章内容是基于上一篇文章单链表的实现 C-单链表的实现 。

    队列是先进先出(First in First out)的线性数据结构. 通常有2个主要的操作:enqueue 和 dequeue。

    enqueue操作是将元素添加到列队中。

    dequeue操作是将元素从队列中移除。

    链表经常用于队列的实现。enqueue 操作是从链表的头部(head)添加元素,dequeue 操作是从链表的尾部(tail)移除元素。

    队列定义

    定义队列和相关操作

    // 定义队列
    typedef LinkedList Queue;
    // 初始化队列
    void initQueue(Queue *queue)
    {
        init_linkedlist(queue);
    }
    
    // 添加元素
    void enqueue(Queue *queue, void *data) {
        addHead(queue, data);
    }
    
    // 移除元素
    void* dequeue(Queue *queue) 
    {
        pNode node = queue->head;
        void* data;
        if (queue->head == NULL){
            data = NULL;
        } else if (queue->head == queue->tail) {
            queue->head = queue->tail = NULL;
            data = node->data;
            free(node);
        } else {
            // 找到倒数第二个节点,由于之前实现的链表是单链表,只能这么找
            while (node->next != queue->tail)
            {
                node = node->next;
            }
            queue->tail= node;
            node = node->next;
            queue->tail->next = NULL;
    
            data = node->data;
            free(node);
        }
    
        return data;   
    }

    示例说明

        Queue queue;
        // 初始化
        initQueue(&queue);
        int a = 20;
        int b = 30;
        int c = 40;
        int *ptr_1 = &a;
        int *ptr_2 = &b;
        int *ptr_3 = &c;
    
        enqueue(&queue, ptr_1);
        enqueue(&queue, ptr_2);
        enqueue(&queue, ptr_3);
    
        void *data = dequeue(&queue);
        printf("Dequeued %d\n", *((int*) data));
    
        data = dequeue(&queue);
        printf("Dequeued %d\n", *((int*) data));
    
        data = dequeue(&queue);
        printf("Dequeued %d\n", *((int*) data));

    结构打印:

    Dequeued 20
    Dequeued 30
    Dequeued 40


    以上内容,如有错误,欢迎留言交流或添加公众号交流。

  • C-单链表的实现

    本文将介绍使用C中关键字struct 定义结构体实现单链表。

    在C语言中,结构体的自引用只能通过指针来实现,因为在系统中指针的大小是固定(32位系统时大小是4字节,64位系统时大小是8字节)。

    结构体自引用实现方式

    有2种实现方式,

    方式一:

    typedef struct tagNode
    {
        char *pItem;
        struct tagNode *next;
    } *pNode; // 注意结构体的结尾一定得有分号

    方式二:

    // typedef 一定要定义在struct之前
    typedef struct tagNode *pNode; 
    struct tageNode
    {
        char *pItem;
        pNode next;
    };

    链表定义

    定义链表和相关操作

    // 定义一个用显示数据的函数指针
    typedef void (*DISPLAY)(void*);
    // 定义一个用于比较数据大小的函数指针
    typedef int (*COMPARE)(void*, void*);
    
    // 节点定义
    typedef struct _node
    {
        void *data; // 节点数据
        struct _node *next; // 下一个节点
    } *pNode;
    
    // 链表定义
    typedef struct _linkedlist
    {
        pNode *head;
        pNode *tail;
        pNode *current;
    } LinkedList;
    
    // 初始化链表
    void init_linkedlist(LinkedList *list)
    {
        list->head = NULL;
        list->tail = NULL;
        list->current = NULL;
    }
    
    // 向链表头添加数据节点
    void addHead(LinkedList *list, void *data)
    {
        pNode node = (pNode) malloc(sizeof(Node));
        node->data = data;
        if (list->head == NULL) {
            list->tail = node;
            node->next = NULL;
        } else {
            node->next = list->head;
        }
        list->head = node;
    }
    
    // 向链表尾部添加数据节点
    void addTail(LinkedList *list, void *data)
    {
        pNode node = (pNode) malloc(sizeof(Node));
        node->data = data;
        node->next = NULL;
        if (list->head == NULL) {
            list->head = node;
        } else {
            list->tail->next = node;
        }
        list->tail = node;
    }
    
    // 获取指定的节点信息
    pNode getNode(LinkedList *list, COMPARE compare, void *data) 
    {
        pNode node = list->head;
        while (node != NULL) {
            if (compare(node->data, data)) {
                return node;
            }
            node = node->next;
        }
        return NULL;
    }
    // 删除指定的节点
    void deleteNode(LinkedList *list, pNode node)
    {
        if (node == list->head)
        {
            if (list->head->next == NULL) {
                list->head = list->tail = NULL;
            } else {
                list->head = list->head->next;
            }
        } else {
            pNode tmp = list->head;
            while (tmp != NULL && tmp->next != node)
            {
                tmp = tmp->next;
            }
            if (tmp != NULL) {
                tmp->next = node->next;
            }
        }
        
        free(node);
    }
    // 展示数据
    void displayList(LinkedList *list, DISPLAY display)
    {
        printf("\nLinkedList shown:\n");
        pNode node = list->head;
        while (node != NULL) {
            display(node->data);
            node = node->next;
        }
    }
    
    // 比较函数
    int compareInt(int *e1, int *e2)
    {
        return *e1 == *e2;
    }
    // 显示数据
    void displayIntVal(int *e)
    {
        printf("%d\n", *e);
    }

    使用示例

    1.从链表头部开始添加数据

        LinkedList linkedList;
        // 初始化
        init_linkedlist(&linkedList);
        int a = 20;
        int b = 30;
        int c = 40;
        int *ptr_1 = &a;
        int *ptr_2 = &b;
        int *ptr_3 = &c;
    
        addHead(&linkedList, ptr_1);
        addHead(&linkedList, ptr_2);
        addHead(&linkedList, ptr_3);
    
        displayList(&linkedList, (DISPLAY)displayIntVal);

    打印结果:

    LinkedList shown:
    40
    30
    20

    2.从链表尾部添加数据

        LinkedList linkedList;
        // 初始化
        init_linkedlist(&linkedList);
        int a = 20;
        int b = 30;
        int c = 40;
        int *ptr_1 = &a;
        int *ptr_2 = &b;
        int *ptr_3 = &c;
    
        addTail(&linkedList, ptr_1);
        addTail(&linkedList, ptr_2);
        addTail(&linkedList, ptr_3);
    
        displayList(&linkedList, (DISPLAY)displayIntVal);

    打印结果:

    LinkedList shown:
    20
    30
    40

    3.获取指定的节点

        LinkedList linkedList;
        // 初始化
        init_linkedlist(&linkedList);
        int a = 20;
        int b = 30;
        int c = 40;
        int *ptr_1 = &a;
        int *ptr_2 = &b;
        int *ptr_3 = &c;
    
        addTail(&linkedList, ptr_1);
        addTail(&linkedList, ptr_2);
        addTail(&linkedList, ptr_3);
    
        pNode node = getNode(&linkedList, (COMPARE) compareInt, ptr_2);
        printf("node val is %d\n", *((int *)node->data));

    打印结果:

    node val is 30

    4.删除指定的节点

        LinkedList linkedList;
        // 初始化
        init_linkedlist(&linkedList);
        int a = 20;
        int b = 30;
        int c = 40;
        int *ptr_1 = &a;
        int *ptr_2 = &b;
        int *ptr_3 = &c;
    
        addTail(&linkedList, ptr_1);
        addTail(&linkedList, ptr_2);
        addTail(&linkedList, ptr_3);
    
        pNode node = getNode(&linkedList, (COMPARE) compareInt, ptr_2);
        printf("node val is %d\n", *((int *)node->data));
    
        deleteNode(&linkedList, node);
        displayList(&linkedList, (DISPLAY)displayIntVal);

    打印结果:

    node val is 30

    LinkedList shown:
    20
    40


    以上就是全部的内容,如有错误,欢迎留言交流或者关注公众号交流。