首页 > 解决方案 > 未定义对“vec_expand_”的引用

问题描述

我在 GitHub 上遇到了用于 C 的动态数组实现的rxi/vec库。

我正在尝试运行README 的使用部分中给出的示例程序。

我已经实现了这样的代码

#include <stdio.h>
#include "vec.h"

int main()
{
    vec_int_t v;
    vec_init(&v);

    vec_push(&v, 123);
    vec_push(&v, 456);

    printf("%d\n", v.data[1]); /* Prints the value at index 1 */

    printf("%d\n", v.length); /* Prints the length of the vector */

    vec_deinit(&v);

    return 0;
}

但是每次我运行程序时,它都会在 VS Code 的终端中抛出这个错误:

> Executing task: C/C++: gcc.exe build active file <

Starting build...
Build finished with errors(s):
C:\Users\user\AppData\Local\Temp\cctdgiKc.o: In function `main':
D:/Test.c:9: undefined reference to `vec_expand_'
D:/Test.c:10: undefined reference to `vec_expand_'
collect2.exe: error: ld returned 1 exit status

The terminal process failed to launch (exit code: -1).

在 Visual Studio 上,错误看起来像这样... Visual Studio 错误屏幕截图

错误似乎来自这两行:

vec_push(&v, 123);
vec_push(&v, 456);

我也尝试过这个答案中的 c-vector库和代码,但这些都给出了同样的错误。

我是 C 编程的新手,所以我无法理解这里发生了什么,我可能会犯一些愚蠢的错误。

先感谢您。

标签: cvector

解决方案


您未能将库与您的程序链接起来。

正在做

#include "vec.h"

对成为实际代码没有任何作用,它所做的只是将标题的文本(带有声明)粘贴到#include.

例外是“仅标头”库,但似乎该库不是仅标头实现。该vec_init()函数似乎是一个宏(或内联函数),因为您没有收到错误。

创建可执行文件时,您必须告诉链接器添加相关库中的代码。

这是如何完成的是特定于编译器的。


推荐阅读