You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译报错undefined reference to `concatf'的排查求助

编译报错:undefined reference to 'concatf' 问题排查

我已经查阅过同类问题的解决方案,但仍未定位到问题根源。编译代码时持续触发undefined reference to 'concatf'错误,而该函数明明已经在头文件中正确声明。

我的环境中代码可正常运行,但在另一Windows环境及同事的环境中编译失败。编译步骤如下:

  • 准备script.php文件,执行命令:ld -r -b binary script.php data.o将PHP文件编译为目标文件
  • 执行链接命令:gcc build.c data.o -o runme生成可执行文件

相关代码

concatf.c

#include "concatf.h"

/*
 * vscprintf:
 * MSVC implements this as _vscprintf, thus we just 'symlink' it here
 * GNU-C-compatible compilers do not implement this, thus we implement it here
 */
#ifdef _MSC_VER
#define vscprintf _vscprintf
#endif

#ifdef __GNUC__
int vscprintf(const char *format, va_list ap)
{
    va_list ap_copy;
    va_copy(ap_copy, ap);
    int retval = vsnprintf(NULL, 0, format, ap_copy);
    va_end(ap_copy);
    return retval;
}
#endif


int vasprintf(char **strp, const char *format, va_list ap)
{
    int len = vscprintf(format, ap);
    if (len == -1)
        return -1;
    char *str = (char*)malloc((size_t) len + 1);
    if (!str)
        return -1;
    int retval = vsnprintf(str, len + 1, format, ap);
    if (retval == -1) {
        free(str);
        return -1;
    }
    *strp = str;
    return retval;
}

int concatf(char **strp, const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    int retval = vasprintf(strp, format, ap);
    va_end(ap);
    return retval;
}

concatf.h

#ifndef CONCATF_H
#define CONCATF_H

#if defined(__GNUC__) && ! defined(_GNU_SOURCE)
#define _GNU_SOURCE /* needed for (v)asprintf, affects '#include <stdio.h>' */
#endif
#include <stdio.h>  /* needed for vsnprintf    */
#include <stdlib.h> /* needed for malloc, free */
#include <stdarg.h> /* needed for va_*         */

int concatf(char **strp, const char *format, ...);

#endif // CONCATF_H

build.c

#include "concatf.h"

#include <stdio.h>
#include <stdlib.h>

extern char _binary_script_php_start;
extern char _binary_script_php_end;

int main(int argc, char *argv[]) {
    // EXTRACT OUR RESOURCE OBJECT INTO /tmp/test.php
    char *p = &_binary_script_php_start;
    FILE *fp = fopen("/tmp/test.php","wb");
    while ( p != &_binary_script_php_end ) {
        fputc(*p++,fp);
    }
    fclose(fp);
    // NOW READ IN OUR STANDARD ARGUMENTS AND LAUNCH OUR COMMAND
    int i = 1;
    char *cmd = "php /tmp/test.php";
    char *s = NULL;
    // asprintf(&s, "%s",cmd);
    // for(i = 1; i < argc; i++) {
    //     asprintf(&s, "%s \"%s\"",s,argv[i]);
    // }
    // concatf("%s",cmd);
    // for(i = 1; i < argc; i++) {
    //     concatf("%s \"%s\"",s,argv[i]);
    // }
    concatf(&s, "%s",cmd);
    for(i = 1; i < argc; i++) {
        concatf(&s, "%s \"%s\"",s,argv[i]);
    }
    system(s);
    free(s);
    unlink("/tmp/test.php"); // comment me out for debugging if you want
}

内容的提问来源于stack exchange,提问作者Sammy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 23:45:42