libuv undefined reference to uv_loop_new - c

After compiling, I am trying to run libuv sample program:
#include <stdio.h>
#include <uv.h>
int main() {
uv_loop_t *loop = uv_loop_new();
printf("Now quitting.\n");
uv_run(loop, UV_RUN_DEFAULT);
return 0;
}
But, when try to run, I get the following error:
**/tmp/ccHTpspB.o: In function `main':
main.c:(.text+0x9): undefined reference to `uv_loop_new'
main.c:(.text+0x28): undefined reference to `uv_run'
collect2: error: ld returned 1 exit status**
Where did I go wrong ?
PS: It doesn't work with #include "uv.h"

You need to link the libuv.a with your compiled code and the linker doesn't know where to find the compiled libuv.
To give you a better answer I would need to see you compile command but in the meantime I would strongly recommend this video where Ryan builds a sample libuv project.
The actual code he uses is a little out of date as the API has changed but I think you will find the start where he puts a project together very enlightening.
http://vimeo.com/24713213

In ubuntu I have used following command with success:
gcc sample.c -luv

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.

undefined reference to `vec_expand_'

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.

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

Correct command line parameters for gcc compilation of SDL

I recently started SDL2.0 programming.
I did a lot of researches and i tried all but i still get those "undefined reference" errors for all the SDL functions:
undefined reference to `SDL_Init'|
undefined reference to `SDL_GetError'|
undefined reference to `SDL_Quit'|
||=== Build finished: 3 errors, 0 warnings ===|
on that simple test program:
#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
return 0;
}
If i have to guess the problem occurs due to the wrong command line syntax.
In this case what should be the correct one?
You aren't linking to the SDL libraries correctly.
Add the following lines int Other Linker Option
-lSDL -lSDLmain
mingw32
SDLmain
SDL
Also You need to check setup for how to compile SDL in codeblock
http://wiki.codeblocks.org/index.php?title=Using_SDL_with_Code::Blocks
http://lazyfoo.net/SDL_tutorials/lesson01/windows/codeblocks/
If it's not too late then try from the beginging to how to set up SDL in codeblock and successfully run it? Below link provide you exact steps for it.
http://www.dreamincode.net/forums/topic/57275-setting-up-codeblocks-to-work-with-sdl/
You might have not linked SDL2 correctly to your CodeBlocks project and not referred to SDL2 correctly in your code.
1:
Go to "Linker options" in "Build Options" menu and make sure you have added these library's to your project like this:
Library's to include in linker options
Importent!: save project before running it after adding/changeing library's.
2:
Change:
#include "SDL.h"
to this:
#include <SDL2/SDL.h>
if you still encounter problems compiling and running it, it's most likely either, your SDL2 files not placed correctly in the compiler's folders, or your using an version of gcc with some missing tools.
These Youtube video's explain everything in great detail:
1: https://www.youtube.com/watch?v=x0LUf7Ibpi0
2: https://www.youtube.com/watch?v=EtUw_7CvRRo

Undefined reference to svc_create

My problem is the following: I'm trying to implement a C RPC example and I keep running into the following compiler error:
remote_exec.c: In function ‘main’:
remote_exec.c:13:3: warning: implicit declaration of function ‘svc_create’
remote_exec.o: In function `main':
remote_exec.c:(.text+0x31): undefined reference to `svc_create'
collect2: ld returned 1 exit status
My code:
#include <stdio.h>
#include <rpc/rpc.h>
#include "rls.h"
int main(int argc, char* argv[])
{
extern void execute();
const char* nettype = "tcp";
int no_of_handles;
no_of_handles = svc_create(execute, EXECPROG, EXECVERS, nettype);
svc_run();
return 0;
}
I really don't know how to solve this. The man page and all the examples I've studied just say to include rpc/rpc.h, however it doesn't seem to work. I am compiling with
gcc -Wall -c
There is no such function as svc_create on linux in libc. See the manpage for rpc. Your guide is using the transport independent(ti) RPC library of Solaris and other unixes. See this question for RPC guides for linux.
The RPC library in linux is based on a slightly different RPC library than the ti-RPC library from Sun, though there is a ti-RPC library here: http://nfsv4.bullopensource.org/doc/tirpc_rpcbind.php , if you use that library, you link with -ltirpc
You probably need svctcp_create or svcudp_create if you're using the standard RPC library included in glibc.
You probably have to link against the library when you create your program. If the library is called rpc, for example, this would be done by adding -lrpc to the compiler command line that creates the final executable.

Resources