undefined reference to `vec_expand_' - c

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.

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.

Related

Undefined reference ld error when using Windows <bluetoothapis.h>

I am new to programming and want to work with the Windows BluetoothApi.h library in C. I've written smaller programs that reference header files I've created, but none of the APIs given by windows.
I am attempting to return information from a local bluetooth speaker to a terminal session on my PC. I've been referencing the BluetoothFindFirstRadio and BLUETOOTH_FIND_RADIO_PARAM documentation, as well as some posts on Stack to see some viable examples. I believe I'm close to being able to compile but I keep getting an error about an undefined reference to the functions I'm calling that I do believe are in the BluetoothAPI.h header file.
From what I've seen, again on Stack, it seems that it's possible that "there is not enough space left at \user\tmp"?
or
Looking at the documentation for ld, it may be possible I need to try to compile using a different command altogther?
PS C:\scripts\C_Lang\Bluetooth> gcc bluetest.c -o test
C:\Users\Ryan\AppData\Local\Temp\cce0FxKH.o:bluetest.c:(.text+0x2b): undefined reference to `BluetoothFindFirstRadio#8'
C:\Users\Ryan\AppData\Local\Temp\cce0FxKH.o:bluetest.c:(.text+0x48): undefined reference to `BluetoothFindRadioClose#4'
collect2.exe: error: ld returned 1 exit status
Code is below:
#include <stdio.h>
#include <string.h>
#include <Windows.h> //not sure if needed
#include <Ws2bth.h> //not sure if needed
#include <bthsdpdef.h>
#include bluetoothapis.h>
//#include <bluetoothleapis.h>
#pragma comment(lib, "Bthprops.lib");
int main(void)
{
BLUETOOTH_FIND_RADIO_PARAMS btfrp; // structure
btfrp.dwSize = sizeof(btfrp); // creating space in memory for parameters?
HANDLE hRadio; // not sure what a handle is, something similar to a pointer?
HBLUETOOTH_RADIO_FIND hFind = BluetoothFindFirstRadio(&btfrp, &hRadio);
// BluetoothGetDeviceInfo(hRadio, &pbtdi);
printf("Bluetooth test!");
BluetoothFindRadioClose(hFind);
return 0;
}
It seems that my issue was not 100% my code, but about how I was attempting to compile my code. After looking further into the documentation I read the line, "Link only to Bthproprs.lib, and avoid linking to Ilprops.lib." So, I don't fully understand why I would need to link, when I have a #pragma comment(lib, "Bthprops.lib"); but that is most likely due to my own ignorance. I did notice the answer on this post which helped clear up my ignorance of HOW to link the Bthproprs.lib library. So, my code didn't change, but my compile did, gcc bluetest.c -o test -lbthprops.
Now, to return something actually useful.

fatal error: sodium.h: No such file or directory

Thank you #stark I was unaware you had to link the directory as part of #include. This part is now working though when I include.
(sodium_init() < 0)
{
printf("Sodium could not be initialized");
return 1;
}
I now receive the error "undefined reference to 'sodium_init'.
I have tried adding -lsodium to the compile command (gcc -g -lsodium file1.c file2.c file1.h -o file1.c.exe) which gives the error.
"cannot find -lsodium collect2.exe: error: ld returned 1 exit status"
Through further searching I believe I need to tell the compiler where to find -lsodium though cannot find out how.
-- below has been solved using #stark advice --
I have been trying to get the sodium library working for the last 3 days. I have followed the instillation instructions here(https://libsodium.gitbook.io/doc/installation) which all appear to have completed successfully, but still receive the error.
I then found information suggesting I have not linked Visual Studio (community edition) to the library location.
I have attempted to follow the instructions here(https://www.learncpp.com/cpp-tutorial/a2-using-libraries-with-visual-studio-2005-express/) but cannot see "VC++ Directories". I have tried several other sets of directions with the same outcome.
Is anyone able to help getting the sodium library working so I can get access to the RtlGenRandom() function?

Why is the error "collect2.exe: error: ld returned 1 exit status" showing when I run my c code

How to fix the error "collect2.exe: error: ld returned 1 exit status", please do not downvote this because it is too trivial I have just started to learn my first language.
#include <stdio.h>
int value()
{
printf("Twinkle Twinkle little star");
printf("How I wonder what you are");
printf("Up above the sky so high");
printf("Like a diamong in the sky!");
return 0;
}
The collect2.exe: ld returned 1 exit status error is a consequence of an error that came before it. Not sure if this'll help, but try re-naming int value() to int main().
ld is a linker whose purpose is to link multiple object files. In your code, you used #include <stdio.h> which is a header file containing IO operations. So the linker at this point will combine that header file with the object file created from your source code. But linker needs to have an entry point in your source code relative to which it can link all object files. That entry point in C is provided by using main() function. As #TDO mentioned, replace value() with main().
Edit: If you are unsure about any of the above terminology, I would advise to use search engine to read more about them.

undefined reference to c function when calling from go code

Im trying to call a simple c function from within go with cgo
The files are as follows:
goFile.go:
package main
//#include "cFile.h"
import "C"
func main() {
C.printInC()
}
cFile.h:
void printInC();
cFile.c:
#include "cFile.h"
#include <stdio.h>
void printInC(){
printf("Test");
}
running go build goFile.go throws the following exception:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: $WORK\b001\_x002.o: in function `_cgo_f9774dcf54b4_Cfunc_printInC':
/tmp/go-build/cgo-gcc-prolog:49: undefined reference to `printInC'
collect2.exe: error: ld returned 1 exit status
I'm not really sure why this isn't working, I've looked at multiple tutorials for cgo that implement calling c functions exactly the same way without a problem.
I've found the problem.
When running go build goFile.go the go tool apparently only builds goFile.go, when i run go build on the entire directory everything works fine..

Executing legacy opengl code in codeblocks

I have an old program that uses legacy opengl. I am attempting to run the code, but I have been stumbling into some errors. I installed freeglut and attempted to link it and add the includes, and while it seems to execute, I get an error in the process that states:
undefined reference to `_imp___glutInitWinExit#12
The bit of code in the freeglut_std.h header file that is causing the error is located at:
static void FGAPIENTRY FGUNUSED glutInit_ATEXIT_HACK(int *argcp, char **argv_ { _glutInitWithExit(argcp, argv, exit); }
This is line 637 of the code, so I presume I linked freeglut properly, but for some reason is stopping just short of execution. I am not sure what steps I should take from here, and any help is appreciated. Thank you.
Edit: Build log (a snippet of it) -
C:/Program Files (x86)/CodeBlocks/MinGW/include/GL/freeglut_std.h:637: undefined reference to `_imp____glutInitWithExit#12'
obj\Debug\curve.o: In function `glutCreateWindow_ATEXIT_HACK':
C:/Program Files (x86)/CodeBlocks/MinGW/include/GL/freeglut_std.h:639: undefined reference to `_imp____glutCreateWindowWithExit#8'
obj\Debug\curve.o: In function `glutCreateMenu_ATEXIT_HACK':
C:/Program Files (x86)/CodeBlocks/MinGW/include/GL/freeglut_std.h:641: undefined reference to `_imp____glutCreateMenuWithExit#8'
obj\Debug\curve.o: In function `curve_buildLine':

Resources