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.
Related
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.
I am trying to learn Cgo, so i tried accessing aerospike client from Cgo
package main
// #cgo CFLAGS: -g -Wall
// #include <stdlib.h>
// #include <string.h>
// #include "aerospike-client-c/examples/put/example_utils.h"
import "C"
import (
"unsafe"
)
func main() {
retvals := C.putitnew()
_=retvals
}
But i get below errors. ( Please note the C program runs successfully when i do make and make run ).
undefined reference to `example_get_opts'
./aerospike-client-c/examples/put/example.c:66: undefined reference to
`example_connect_to_aerospike'
./aerospike-client-c/examples/put/example.c:69: undefined reference to
`example_remove_test_record'
./aerospike-client-c/examples/put/example.c:78: undefined reference to
`as_record_init'
./aerospike-client-c/examples/put/example.c:79: undefined reference to
`as_record_set_int64'
/tmp/go-build283334635/b046/_x002.o: In function `as_record_set_str':
....
So i believe the issue is with the configuration in Makefile. I have searched for entire day and tried many solutions but invain. Can you help me how i could import Makefile in Cgo? Or an alternative to help me execute this successfully..
You need to link with the relevant library. I believe the library is called -laerospike. In this case, the cgo directive would look like this:
// #cgo LDFLAGS: -laerospike
See the cgo documentation.
Furthermore, you need to link in the relevant example code. I don't see a put example in the official repository. You will likely have to copy part of its sources directly in the cgo section of your Go file because examples are usually not intended for direct linking.
I am struggling to compile a simple C program from RFC 2617. The program is digtest.c and it uses digcalc.c, another file from the sample implementation. The latter one depends on two files that my compiler doesn't know about:
#include <global.h>
#include <md5.h>
At first I got this error:
digcalc.c:5:20: fatal error: global.h: No such file or directory
I resolved that by changing <global.h> to <stddef.h>, it seems. But I still get this error:
digcalc.c:7:17: fatal error: md5.h: No such file or directory
Now, md5.h seems to refer to the file found in libbsd. So I installed libbsd-dev and tried to compile the files like this:
gcc digcalc.c digtest.c -o digtest -L/usr/lib/x86_64-linux-gnu -lbsd
where /usr/lib/x86_64-linux-gnu is the location of libbsd.so and libbsd.a files. However, this does not resolve the last compilation error.
Could anyone point out what am I missing here?
Figured it out. Had to change <md5.h> to <bsd/md5.h>, as noted on libbsd page.
So instead of the original headers in digcalc.c:
#include <global.h>
#include <md5.h>
I used:
#include <stddef.h>
#include <bsd/md5.h>
Also had to change function stricmp to strcasecmp, its POSIX equivalent. After that the sample code compiled seamlessly.
I've added a new function wiringPiVersion() to wiringPi, but after I build and install the shared library, when I attempt to compile a small C program around it, I get:
wpi_ver.c:(.text+0xc): undefined reference to `wiringPiVersion'
However, when I include it in an XS based Perl module, all works well. I don't know enough about C to figure out what's going wrong here, and I've been searching for the better part of two hours trying different things to no avail.
Here's my small C program to test the new function:
#include <stdio.h>
#include <wiringPi.h>
int main (){
char * ver = wiringPiVersion();
printf("wiringPi version: %s\n", ver);
return 0;
}
Compilation that throws the error:
gcc -o ver wpi_ver.c -lwiringPi
The addition to wiringPi's header file:
extern char * wiringPiVersion(void);
The wiringPi's .c file addition:
#define WPI_VERSION "2.36"
char * wiringPiVersion(void){
return WPI_VERSION;
}
In my Perl module's XS file, I have:
char *
wiringPiVersion()
...and my Perl module's Makefile.PL
LIBS => ['-lwiringPi'],
...and after re-installing the Perl module, I can access the function without any issues in a test script.
I'm hoping this is something simple I'm overlooking which someone may be able to point out. My question is, how do I rectify this?
So it turned out that there were two .so files generated when I rebuilt wiringPi... one in the wiringPi's build directory way under my home directory, and the other in /usr/local/lib.
After a tip in comments, I added the library path explicitly:
gcc -o ver wpi_ver.c -L/usr/local/lib -lwiringPi
...and it all fell together and works as expected:
$ ./ver
wiringPi version: 2.36
Note: I have sent Gordon the patch in hopes it gets included in the next wiringPi cut.
Update: I received an email back from Gordon and he stated that currently, only the gpio application has the ability to report the version, so he advised that he's going to add something similar to my patch in a future release.
Although already solved, I added this answer to show what gave me the hint.
Error message "undefined reference" points to a linker error (cf. answer on SO), so its about checking if the correct library is drawn.
#include <stdio.h>
#include <stdlib.h>
#include <winscard.h>
#include <wintypes.h>
int main(void){
SCARDCONTEXT hContext;
SCARDHANDLE hCard;
DWORD dwActiveProtocol;
LONG rv;
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM,NULL,NULL,&hContext);
rv = SCardConnect(hContext,"Reader X", SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);
printf("Hello world!\n");
}
There are errors like this:
test.c:(.text+0x2e): undefined reference to `SCardEstablishContext'
test.c:(.text+0x5b): undefined reference to `SCardConnect'
xcollect2: error: ld returned 1 exit status
The functions are included in 'winscard.h' but it seems I cannot use them.
I don't know how to solve it.
Including a header file usually just informs your translation unit (your program in this case) that certain things exist, in order than the code can be compiled,
To actually use those things, you need to do more than just figure out that they exist, you need to actually incorporate the code for them within your executable.
This is generally the responsibility of the link stage and, as per the Microsoft documentation, the code for these functions are to be found in winscard.lib/.dll. You need to modify your project so that those libraries are included in your build.