如何确定声明的外部变量是否在某些 C 文件中定义或初始化

How to determine if a declared extern variable is defined or initialized in some C file

我有一些 table 结构根据我使用的驱动程序进行不同的编译。 我希望能够检查某个驱动程序是否已编译(或通过关系,如果其 table 已定义)。

我找遍了,但我似乎无法找到一种方法来确定声明的外部变量是否已定义或 是否有某种方法可以检查是否已在给定应用程序中编译(和链接)c 源文件。我曾研究过使用一些宏魔术,例如 container_of 宏,但到目前为止我还没有找到。

例如说我有以下内容:

checkFile.c

#include "neutral.h"
#include "fileA.h"
#include "fileB.h"

bool setTable(int type, someStruct_t *table) {

    /*
    This doesn't work
    fails to compile with:
    undefined reference to fileA_Table
    ironically this is what I'm trying to check
    */
    switch (type) {
        case FILEA:
            if (fileA_Table) {
                someTable = &fileA_Table;
                return true;
            }
            else {
                someTable = NULL;
                return false; 
            }
        break;
        case FILEB:
            if (fileB_Table) {
                someTable = &fileB_Table;
                return true;
            }
            else {
                someTable = NULL;
                return false; 
            } 
        break;
        default:
            someTable = NULL;
            return false;   
    }
}

neutral.h

typedef struct {
    int memberA;
    bool memberB;
} someStruct_t;

extern someStruct_t fileA_table[];
extern someStruct_t fileB_table[];

fileA.c

#include "neutral.h"

someStruct_t fileA_table[] = {
    {
        .memberA = 0;
        .memberB = false;
    },
    {
        .memberA = 5;
        .memberB = false;
    }
}

fileB.c

#include "neutral.h"
someStruct_t fileB_table[] = {
    {
        .memberA = 14;
        .memberB = true;
    },
    {
        .memberA = 5;
        .memberB = true;
    }
}

我什至不确定这是不是我可以用 C 语言做的事情,实际上我要解决的根本问题是初始化某种依赖于 fileA 或 fileB 的接口类型,并确保哪些数组可用从 fileA and/or fileB 使用。注意这个

理想情况下,如果我可以只使用 file_table,我真的很喜欢,但我认为如果 fileA 和 fileB 都被编译,那是不可能的。

请注意,无论文件 A 或文件 B 或两个文件 是否已编译,这都应该有效。

非常感谢任何帮助。

您的代码可以安全地假定该变量存在。

如果没有,当您尝试将目标文件 link 转换为可执行文件时,编译器会在 linking 阶段收到错误消息。

如果您想要某种条件编译,您需要根据可用的内容设置宏,然后在代码的各个部分检查该宏。

例如:

#include "neutral.h"

#ifdef USE_FILE_A
#include "fileA.h"
#elif defined USE_FILE_B
#include "fileB.h"
#endif

bool setTable(someStruct_t **table)
{
#ifdef USE_FILE_A
    *table= &fileA_Table;
    return true;
#elif defined USE_FILE_B
    *table= &fileB_Table;
    return true;
#else
    *table = NULL;
    return false;
}

使用更新后的代码:

#include "neutral.h"

#ifdef HAS_FILE_A
#include "fileA.h"
#endif
#ifdef HAS_FILE_B
#include "fileB.h"
#endif

bool setTable(int type, someStruct_t *table)
{
    switch (type) {
        case FILEA:
            #ifdef HAS_FILE_A
                someTable = &fileA_Table;
                return true;
            #else
                someTable = NULL;
                return false; 
            #endif
        break;
        case FILEB:
            #ifdef HAS_FILE_B
                someTable = &fileB_Table;
                return true;
            #else
                someTable = NULL;
                return false; 
            #endif
        break;
        default:
            someTable = NULL;
            return false;   
    }
}

根据 Bill Lynch 的建议,我研究了强符号和弱符号 here and here 并找到了一个有吸引力的解决方案。大概这就是我想出的。

checkFile.c

#include "neutral.h"
#include "fileA.h"
#include "fileB.h"

bool setTable(int type, someStruct_t *table) {
    /*
     * This will compile and if fileA_table has not been
     * defined in fileA then it will evaluate to 0
     */
    switch (type) {
        case FILEA:
            if (fileA_Table) {
                someTable = &fileA_Table;
                return true;
            }
            else {
                someTable = NULL;
                return false; 
            }
        break;
        case FILEB:
            if (fileB_Table) {
                someTable = &fileB_Table;
                return true;
            }
            else {
                someTable = NULL;
                return false; 
            } 
        break;
        default:
            someTable = NULL;
            return false;   
    }
}

neutral.h

typedef struct {
    int memberA;
    bool memberB;
} someStruct_t;

__attribute__((weak)) extern someStruct_t fileA_table[];
__attribute__((weak)) extern someStruct_t fileB_table[];

fileA.c

#include "neutral.h"

someStruct_t fileA_table[] = {
    {
        .memberA = 0;
        .memberB = false;
    },
    {
        .memberA = 5;
        .memberB = false;
    }
}

fileB.c

#include "neutral.h"
someStruct_t fileB_table[] = {
    {
        .memberA = 14;
        .memberB = true;
    },
    {
        .memberA = 5;
        .memberB = true;
    }
}

我喜欢这个,因为它意味着我可以获得我想要的结果,同时对我的界面影响最小,对编译器没有影响 options/commands。

我希望对 fileA 和 fileB 使用相同的符号来使通用界面层和中性 header 更干净,以获得如下内容:

neutral.h

typedef struct {
    int memberA;
    bool memberB;
} someStruct_t;

__attribute__((weak)) extern someStruct_t file_table[];

checkFile.c

#include "neutral.h"
#include "fileA.h"
#include "fileB.h"

bool setTable(int type, someStruct_t *table) {

    if (file_Table) {
        someTable = &file_table;
        return true;
    }
    
    someTable = NULL;
    return false; 
}

只要我们将从 checkFile.c 开始的接口编译为两个单独的二进制文件,就可以做到这一点。一次与 fileA.c 链接,一次与 fileB.c 链接。这确实增加了构建选项的复杂性,增加了代码大小,而且我不确定对性能的影响,但是它使代码更易于维护,如果有多个潜在文件和几种类型的表需要已检查。