1

I've come across rxi/vec library for dynamic array implementation for C on GitHub.

I'm trying to run the sample program that is given in the README's usage section.

I've implemented the code like this

#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;
}

But everytime I'm runnung the program it is throwing this error in the VS Code's terminal:

> 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).

On Visual Studio, the error looks something like this... Visual Studio Error Screenshot

The error appears to be from these two lines:

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

Also I have tried c-vector library and code from this answer but these are giving same kind of error .

I'm new to C programming so I'm not able to understand what's going on here and it's possible that I might be doing some silly mistake.

Thank you in advance.

Saad
  • 13
  • 3
  • 1
    Hi @Saad, your question is pretty good, however for the next time I suggest you to avoid add a picture, Have a look here => [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557) So remember you can fix the answer by editing – Federico Baù Jan 07 '21 at 15:53

1 Answers1

0

You have failed to link the library with your program.

Doing

#include "vec.h"

does nothing to being the actual code in, all it does is to paste in the text of the header (with declarations) at the point of the #include.

The exception is "header only" libraries, but it seems that library is not a header only implementation. The vec_init() function seems to be a macro (or an inline function) since you're not getting errors for it.

You must tell your linker to add the code from the library in question when creating your executable.

How this is done is compiler-specific.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • I'm using 'MingW gcc' compiler with VS Code on Windows. Can you guide me how it's done? Tysm for youe time. – Saad Jan 07 '21 at 13:53