gpt4 book ai didi

linux - 如何 : Conditional include of header file

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:18 25 4
gpt4 key购买 nike

我有 test.c,我想在其中声明

#include "abc.h"(libabc的头文件)

仅当 test.c 已使用 libabc 编译为:

gcc 测试.c -labc

如果test.c简单编译为

gcc test.c , abc.h 不应包含在内。

我该怎么做?

最佳答案

一种方法是使用-D 来定义一个宏:

gcc test.c -labc -DABC

#ifdef ABC
#include "abc.h"
#endif

如果您从 Bash 运行此命令,那么原则上您可以围绕 gcc 创建一个 shell 函数包装器,以便在 - 时自动添加 -DABC labc 指定:

function gcc ()
{
local arg
for arg in "$@" ; do
if [[ $arg = -labc ]] ; then
command gcc "$@" -DABC
return
fi
done
command gcc "$@"
}

. . .但我真的不推荐这样做。

关于linux - 如何 : Conditional include of header file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9449087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com