在Bullseye下,从混合的C 与C++代码增量构建32位可执行文件时出现的编译错误

编程语言 2026-07-10

在通过 covc 使用Clang逐步把一个混合C/C++项目构建成一个32位可执行文件时,我在Linux的 BullseyeCoverage遇到了一个问题。第一次构建可以正常完成,但重新编译任意源文件后再尝试将其重新链接到最终可执行文件则不行。当这样做时,Bullseye在链接时编译它自己的 libcov-posix.c 时报告以下错误:

BullseyeCoverage Compile 9.0.7 Linux-x64 License 25590 
Copyright (c) Bullseye Testing Technology
warning: Ignoring source file directory './', coverage file contains '/home/data/u/tmp/Joshua/BullseyeTest/'
/usr/bin/ccache "/opt/BullseyeCoverage/bin/covc" /usr/bin/clang++-16 -m32 -o main main.o simple_math.o
BullseyeCoverage Compile 9.0.7 Linux-x64 License 25590 
Copyright (c) Bullseye Testing Technology
In file included from /opt/BullseyeCoverage/run/libcov-posix.c:139:
/opt/BullseyeCoverage/run/libcov-core-big.h:340:8: error: use of undeclared identifier 'program_invocation_name'
                        if (cov_imageName[0] != '\0') {
                            ^
/opt/BullseyeCoverage/run/libcov-posix.c:109:24: note: expanded from macro 'cov_imageName'
        #define cov_imageName program_invocation_name
                              ^
In file included from /opt/BullseyeCoverage/run/libcov-posix.c:139:
/opt/BullseyeCoverage/run/libcov-core-big.h:342:43: error: use of undeclared identifier 'program_invocation_name'
                                bi = stringCopy(buf, bi, sizeof(buf), cov_imageName);
                                                                      ^
/opt/BullseyeCoverage/run/libcov-posix.c:109:24: note: expanded from macro 'cov_imageName'
        #define cov_imageName program_invocation_name
                              ^
2 errors generated.
BullseyeCoverage error: status 1: /usr/bin/clang-16
make: *** [Makefile:2: main] Error 1

以下文件展示并重现了该问题:
simple_math.c:

extern int add(int a, int b) {
    return a + b;
}

simple_math.h:

#ifndef _SIMPLE_MATH_H
#define _SIMPLE_MATH_H
extern "C" int add(int a, int b);
#endif

main.cpp:

#include <iostream>
#include "simple_math.h"

int main() {
    int result = add(3, 2);
    std::cout << "The result is: " << result << std::endl;
    return 0;
}

test.h:

#ifndef hNesLibUT
#define hNesLibUT

#if !defined(__cplusplus) 
#include <stdlib.h>
#endif

#endif

Makefile:

main: main.o simple_math.o
    /usr/bin/ccache "/opt/BullseyeCoverage/bin/covc" /usr/bin/clang++-16 -m32 -o main main.o simple_math.o

simple_math.o: simple_math.c
    /usr/bin/ccache "/opt/BullseyeCoverage/bin/covc" /usr/bin/clang-16 -m32 -o simple_math.o -c simple_math.c -include test.h
main.o: main.cpp
    /usr/bin/ccache "/opt/BullseyeCoverage/bin/covc" /usr/bin/clang++-16 -m32 -o main.o -c main.cpp -include test.h

.PHONY: clean
clean:
    rm -f *.o *.d main

在包含这些文件的目录中运行以下命令时,上面显示的错误将会出现:

make
touch simple_math.c
make

显然,test.h 是这里的关键问题。将编译目标配方中的 -include test.h 参数删除即可解决增量构建的问题。然而,test.h 的preinclusion不幸地对我的实际项目提出了一个要求。

有人知道这是Bullseye的一个bug,还是我在做错什么吗?我能否以较为干净的方式来规避这个问题?

我正在使用Bullseye Coverage 9.0.7。

解决方案

看起来像是Bullseye的一个bug,covc 似乎在编译Bullseye自己的 libcov-posix.c 时应用了你强制的 -include test.h,因此 <stdlib.h> 会在Bullseye能启用用于 program\_invocation\_name 的GNU特性宏之前被包含进来。你应该让强制包含的头文件避免包含系统头文件,或者在包含 <stdlib.h> 之前定义 \_GNU\_SOURCE(或通过 -D\_GNU\_SOURCE)。我会把这个可重现的步骤报告给Bullseye。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章