Visual Studio 2022 + Ubuntu跨平台开发libmicrohttpd出现错undefined reference to ‘xxxx‘处理

本文提供了解决SVN证书验证错误的方法,包括通过本地保存证书信息、配置文件修改等手段。同时,介绍了如何在Leopard中正确配置SVN,排除不适合加入SVN管理的文件类型。此外,解决了config文件中出现的前置空格导致的配置错误问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在对libmicrohttpd进行跨平台开发时,由于使用的是VS2022开发,而libmicrohttpd又属于第三方库的模式,因此在编译调试的时候出现undefined reference to 'xxxx'错误,引用未定义,说明找不到头文件或库文件,本篇记录这种错误处理方案。

首先我们创建一个工程,添加源文件代码:

#include <microhttpd.h>  // 引入Libmicrohttpd库
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

//#define PORT 8888
const int PORT = 8888;

// 处理每个连接请求的函数
static int answer_to_connection(void* cls, struct MHD_Connection* connection,
    const char* url, const char* method,
    const char* version, const char* upload_data,
    size_t* upload_data_size, void** con_cls) {
    printf("url==========================%s\n", url);
    printf("method=======================%s\n", method);
    printf("version======================%s\n", version);
    printf("upload_data==================%s\n", upload_data);
    printf("upload_data_size=============%ld\n", *upload_data_size);
    char* page;  // 存储生成的HTML页面内容
    FILE* fp;    // 文件指针,用于执行命令
    char path[1035];  // 存储命令输出的缓冲区

    // 执行系统命令top,以批处理模式运行一次
    //fp = popen("top -b -n 1", "r");
    // 执行系统命令top,以批处理模式运行一次
    fp = popen("ls -alh ~/projects/", "r");

    if (fp == NULL) {
        printf("Failed to run command\n");
        exit(1);
    }

    // 动态分配空间来存储HTML页面内容
    size_t page_size = 10000;
    page = (char *)malloc(page_size);
    if (!page) {
        perror("Malloc failed");
        exit(1);
    }

    strcpy(page, "<html><head><meta charset=\"UTF-8\"></head><body>");

    // 读取命令输出并添加到HTML页面
    while (fgets(path, sizeof(path) - 1, fp) != NULL) {
        // 检查是否需要扩展缓冲区
        if (strlen(page) + strlen(path) + 8 > page_size) {
            page_size *= 2; // 加倍页面大小
            char* new_page = (char *)realloc(page, page_size);
            if (!new_page) {
                perror("Realloc failed");
                free(page);
                exit(1);
            }
            page = new_page;
        }
        strcat(page, path);
        strcat(page, "<br>");
    }
    strcat(page, "</body></html>");

    struct MHD_Response* response;
    int ret;

    // 创建响应对象,其中包含生成的HTML页面
    response = MHD_create_response_from_buffer(strlen(page), (void*)page, MHD_RESPMEM_MUST_FREE);
    ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
    MHD_destroy_response(response);

    pclose(fp);
    return ret;
}

int main() {
    struct MHD_Daemon* daemon;  // 定义服务器守护进程

    // 启动守护进程,监听指定端口
    daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL,
        (MHD_AccessHandlerCallback)answer_to_connection, NULL, MHD_OPTION_END);
    if (NULL == daemon) return 1;  // 如果守护进程启动失败,返回1

    getchar();  // 等待用户输入以退出

    MHD_stop_daemon(daemon);  // 停止守护进程
    return 0;
}

编译:

microhttpd.h是第三方头文件,是找不到ubuntu下的头文件和库目录吗,我们可以设置头文件和库目录

右键项目名=》【属性】=》【VC++目录】

在【包含目录】右边填写头文件目录,在【库目录】右边填写库文件的目录,【应用】=》【确定】,编译:

还是报错。

在Ubuntu系统里的编译命令是

gcc -o command command.c -lmicrohttpd

必须显示引用第三方库,因此我们加把这个引用库加上

加上引用库后,再次编译

编译成功。

参考:

Visual Studio 2022 跨平台开发Linux C程序环境搭建_vs2022 linux 编程需要-优快云博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值