Linux自主学习 - C语言库Melon的快速入门

备注:ubuntu-20.04.3-desktop-amd64.iso

一、C语言库melon的安装

1、选择一个合适的文件夹用于下载Melon,本文以~/ProjectCode为例 在~/ProjectCode下输入git clone https://github.com/Water-Melon/Melon.git 然后,可以使用ls命令查看到Melon下载成功
2、使用cd Melon/将当前工作目录切换至Melon 然后,可以使用ls命令查看Melon文件夹包含的内容
3、输入./configure运行可执行文件configure
4、输入make
5、输入sudo make install,然后,输入密码
6、依次输入: sudo sh -c "echo /usr/local/melon/lib/ >> /etc/ld.so.conf" sudo ldconfig 安装完成!

二、C语言库Melon的使用(以使用内存池为例)

1、选择一个合适的文件夹用于编写C源程序,本文以~/ProjectCode/TestMelon为例 在~/ProjectCode/TestMelon下输入vim testMln.c创建C源程序,程序内容如下所示 然后,可以使用ls命令查看到testMln.c创建成功
#include <stdio.h>
#include "mln_core.h"
#include "mln_alloc.h"
#include "mln_log.h"

int main(int argc, char *argv[])
{
    char *ptr;
    mln_alloc_t *pool;
    struct mln_core_attr cattr;

    cattr.argc = argc;
    cattr.argv = argv;
    cattr.global_init = NULL;
    cattr.main_thread = NULL;
    cattr.master_process = NULL;
    cattr.worker_process = NULL;

    if (mln_core_init(&cattr) < 0) {
       fprintf(stderr, "Melon init failed.
");
       return -1;
    }

    pool = mln_alloc_init(NULL);

    ptr = mln_alloc_m(pool, 1024);
    mln_log(debug, "%X
", ptr);
    mln_alloc_free(ptr);

    mln_alloc_destroy(pool);
    return 0;
}
2、输入gcc testMln.c -o testMln -I ~/ProjectCode/Melon/include -lmelon -L /usr/local/melon/lib 然后,可以使用ls命令查看到已生成可执行文件testMln -I 的作用是指定include包含文件的搜索目录;-L 的作用是指定链接库文件的搜索目录
3、输入./testMln运行可执行文件testMln,可得到如下运行结果
经验分享 程序员 微信小程序 职场和发展