gpt4 book ai didi

c++ - 对通过静态库调用的同一 .c 文件中定义的函数的 undefined reference

转载 作者:行者123 更新时间:2023-11-28 04:53:56 25 4
gpt4 key购买 nike

此静态 C 库 (libabc.a) 链接到 C++ 程序。

在我的 qmake pro 文件中,我使用了
LIBS += pathToLib/libabc.a我在创建静态库时没有遇到任何问题。使用 qmake 和 gmake 编译和运行 cpp 应用程序时,创建了其他 cpp 文件的目标文件,但出现以下错误:

../abc/libabc.a(mdl.o):在函数“SetExt”中:
abc/src/mdl.c:2186: 未定义对“func1”的引用

在 mdl.c 中,声明和定义都在那里。
static void func1(int *, char *, char *);

static void func1(int *m, char *p, char *s)
{
.....

该函数的函数调用在C文件mdl.c中。我在创建静态库(使用 gcc 和 ar)时犯了错误吗?或者,我错过了什么?

最佳答案

你声明你的函数为

static void func1(int *, char *, char *);

根据 6.2.2 标识符的链接the C Standard 的第 3 段:

If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

因此,func1() 无法从编译单元外部访问 - 它只能在其所在的源文件中使用。

关于c++ - 对通过静态库调用的同一 .c 文件中定义的函数的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47592352/

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