Linux programming in C & socket.h

I am a new stackoverflow user! I am writing because I have problems programming Bluetooth with C on my KUbuntu 12.04.

I am trying to connect a device (LEGO Mindstorm Brick) to my laptop through the program I found in this pdf (Initiate connection): NXT_Bluetooth_Handout

I installed the following packages: - bluez-hcidump, debugging tool - bluez, Linux Bluetooth stack and related tools - libBluetooth3, BlueZ library - libBluetooth-dev, development files for communicating with the BlueZ library

// Socket, used for Bluetooth socket #include <sys/socket.h> #include <sys/types.h> // Bluetooth headers #include <bluetooth/bluetooth.h> #include <bluetooth/rfcomm.h> 

Basically, I ran into two problems: 1) If I just try to compile the program, I get:

 $ gcc -lm -lbluetooth nxt_bt_connect.c -o nxt_bt_connect /tmp/ccSLdkpn.o: In function `init_bluetooth': nxt_bt_connect.c:(.text+0x60): undefined reference to `str2ba' collect2: ld returned 1 exit status 

Where str2ba is the BlueZ function that should work ... ba2str is another function that this library provides, and it works without problems.

2) If I modify the code to use ba2str instead of the str2ba function, I get an error message related to the socket:

 $ gcc -lm -lbluetooth 1.c -o nxt_bt_connect2 1.c: In function 'main': 1.c:101:23: error: called object 'socket' is not a function 

The problem is that I don't have the sys / socket header, in fact, if I run:

 find /usr/include/ -name socket.h /usr/include/gtkmm-2.4/gtkmm/socket.h /usr/include/linux/socket.h /usr/include/asm-generic/socket.h /usr/include/giomm-2.4/giomm/socket.h /usr/include/x86_64-linux-gnu/sys/socket.h /usr/include/x86_64-linux-gnu/asm/socket.h /usr/include/x86_64-linux-gnu/bits/socket.h 

Is there anyone who knows how to solve these problems? I hope to describe the whole situation in a fairly clear way ... I apologize for my poor English!

Many thanks!

+4
source share
2 answers

For the second question: you have to publish the code you wrote. Otherwise, we cannot understand what is bad.

For the first question: you need to put the library linker flags as the last parameters when calling GCC:

 gcc nxt_bt_connect.c -o nxt_bt_connect -lm -lbluetooth 
+4
source

Assuming KUbuntu is using Debian packaging, <sys/socket.h> aka /usr/include/sys/socket.h comes with the libc6-dev package.

0
source

All Articles