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

CppUTest -p选项提示无fork,但自行测试fork功能正常求助

问题:CppUTest -p选项提示不支持fork但自行调用fork()正常工作

问题现象

  • 使用CppUTest的-p选项(用于在子进程中运行测试)时,触发错误提示:

-p doesn't work on this platform, as it is lacking fork.

  • 但自行编写包含fork()系统调用的代码可以正常编译并运行,能正确创建父子进程。

测试代码及运行结果

测试代码

#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

#include "CppUTest/CommandLineTestRunner.h"
#include "MemoryLeakDetectorNewMacros.h"

int main(int argc, char** argv)
{
    pid_t pid = fork();

    if (pid < 0) {
        printf("Fork failed\n");
        return 1;
    }

    if (pid == 0) {
        printf("This is the child process. PID=%d\n", getpid());
    } else {
        printf("This is the parent process. PID=%d, Child PID=%d\n", getpid(), pid);
        int ret = 0;    
        //ret = CommandLineTestRunner::RunAllTests(argc, argv);
    }
    return 0;
}

运行输出

This is the parent process. PID=17605, Child PID=17606
This is the child process. PID=17606

当前CppUTest版本信息

$ sudo apt list | grep cpputest

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

cpputest/jammy,now 4.0-2 all [installed]
libcpputest-dev/jammy,now 4.0-2 amd64 [installed,automatic]

解决方案

  1. 检查预编译库的编译配置
    查看系统中CppUTest的平台相关头文件(如/usr/include/CppUTest/PlatformSpecificFunctions.h),确认是否定义了CPPUTEST_NO_FORK宏。如果存在该宏,说明apt安装的预编译版本在编译时主动禁用了fork支持。

  2. 从源码编译CppUTest
    预编译版本可能为了兼容性默认禁用了fork功能,建议从源码编译安装最新版本:

    # 克隆源码
    git clone https://github.com/cpputest/cpputest.git
    cd cpputest
    # 编译安装
    mkdir build && cd build
    cmake ..
    make
    sudo make install
    

    编译时默认会启用fork支持,无需额外配置。

  3. 确保链接正确的库
    编译测试代码时,确认链接的是自己编译的CppUTest库而非apt安装的版本。可以通过ldd命令验证:

    g++ your_test.cpp -o test -lCppUTest
    ldd test | grep CppUTest
    

    输出应指向/usr/local/lib下的库文件(源码编译默认安装路径)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 14:32:58