How to link Libbluetooth for GCC? - gcc

I am trying to use hci.h, hci_lib.h etc... in my c files. The final command to link the object files into exec look like this:
gcc -g3 -o execfile ./dev1.o ./dev2.o ./main.o ./nets.o `pkg-config --cflags --libs glib-2.0 gio-unix-2.0`
However I am getting linking error like this:
undefined reference to `hci_open_dev'
undefined reference to `hci_le_set_scan_parameters'
undefined reference to `hci_le_set_scan_enable'
undefined reference to `hci_le_set_scan_enable'
undefined reference to `hci_close_dev'
I know that this is because I am not specifying -lbluetooth to gcc.
Is there a way to specify the libbluetooth-dev to pkg-config like I do for glib & gio-unix above?

Related

Linker error undefined reference to `timer_getoverrun' (-lrt linker option doesn't work)

I am trying to compile example code from timer_create() Linux>man_page. using following command line:
gcc example.c -o example -lrt
Nevertheless, I have linker error:
undefined reference to `timer_getoverrun'
Why -lrt couldn't work.
Sorry - timer_getoverrun() isn't implemented under Cygwin.

Libxml2: undefined reference to xmlTextReaderConstName

I have installed the latest libxml2-2.8.0, as usual: $ ./configure, $ make, $ make install.
The $ xml2-config --cflags --libs gives this output:
-I/usr/local/include/libxml2
-L/usr/local/lib -lxml2 -lm
But trying to compile any example...
$ gcc `xml2-config --cflags --libs` xmltest.c
The linker says:
/tmp/cc8ezrPl.o: In function `processNode':
xmltest.c:(.text+0x19): undefined reference to `xmlTextReaderConstName'
xmltest.c:(.text+0x38): undefined reference to `xmlTextReaderConstValue'
...etc.
Anything I've googled can be solved by xml2-config --cflags --libs flags, or upgrading to the latest version of libxml2, or something. Unfortunately, neither works for me.
What can be the steps to identify the problem?
Using Ubuntu 12.04 64-bit.
The libraries should be specified only after the source file so that the linker can resolve the undefined references in the source file. Try compiling the example with this
gcc -I/usr/local/include/libxml2 -L/usr/local/lib xmltest.c -lxml2 -lm

Errors Creating A Shared Library DLL for SWIG Packaged Simple Java Example (Windows)

I've gone over the SWIG FAQ and dynamic module documentation and there seems to be a wide array of options when deciding on how to build the JNI shared library dll for Windows. The wiki describes the process using very old versions of visual c++. What is everyone using?
I've installed MinGW and the below commands on Windows 7 and ran the below commands on the SWIG simple java example, see below:
C:\swig\swigwin-2.0.4\Examples\java\simple>swig -java example.i
C:\swig\swigwin-2.0.4\Examples\java\simple>gcc -c example_wrap.c -I C:\Program F
iles\Java\jdk1.6.0_30\include -I C:\Program Files\Java\jdk1.6.0_30\include\win32
C:\swig\swigwin-2.0.4\Examples\java\simple>gcc -shared example_wrap.o -o example.dll
Last command errors out with:
example_wrap.o:example_wrap.c:(.text+0xa9): undefined reference to `gcd'
example_wrap.o:example_wrap.c:(.text+0xe2): undefined reference to `Foo'
example_wrap.o:example_wrap.c:(.text+0xe8): undefined reference to `Foo'
example_wrap.o:example_wrap.c:(.text+0x107): undefined reference to `Foo'
example_wrap.o:example_wrap.c:(.text+0x10d): undefined reference to `Foo'
collect2: ld returned 1 exit status
It looks like you've either not compiled or not linked example.c, only example_wrap.c. You need to do something like:
swig -java example.i
gcc -c example_wrap.c -I somepath -I someotherpath
gcc -c example.c -I somepath -I someotherpath
gcc -shared example_wrap.o example.o -o example.dll

changing gcc parameters in netbeans 7.0.1

I have a single c file that can be compiled by the following command without any error:
gcc -o largeread main.c -L. -lftd2xx -Wl,-rpath /usr/local/lib
But when I try to build this single file in one netbeans 7.0.1 application project I get the following error:
/home/mohsen/NetBeansProjects/largeread/main.c:45: undefined reference to `FT_Open'
/home/mohsen/NetBeansProjects/largeread/main.c:57: undefined reference to `FT_ResetDevice'
/home/mohsen/NetBeansProjects/largeread/main.c:58: undefined reference to `FT_SetBaudRate'
It means that some libraries are not added to my project. I have added -L. -lftd2xx -Wl,-rpath /usr/local/lib to:
File->Project Properties->Categories->Build->C Compiler->Command Line->Additional Options
but I still have the same problem. Does anyone have any idea?
thanks.

Linking the static version of a library instead of the dynamic one

I am trying to use libjpeg in my application. Making the project yields a libjpeg.a in .libs folder. What I would like to do is to use this file in the linkage stage. I have tried the following: I copied the libjpeg.a into the folder where my C code resides. Trying to link with
gcc libjpeg.a mycode.c -o executable_name
fails. If I do gcc -ljpeg mycode.c, the compilation is successful when I change my header to point to instead of "libjpeg.h", but this obviously links to the system-wide dynamic version of the library.
Trying to link with relative or absolute path also fails:
gcc ./libjpeg.a mycode.c -o executable_name
I have tried the static option as well:
gcc -static libjpeg.a mycode.c -o executable_name
The linker error is the following:
Linking...
gcc -std=c99 -Wall -Wextra -g -pedantic ./libjpeg.a ./libjpeg.a -lm obj/read_jpeg.o obj/utils.o -o test_jpeg
obj/read_jpeg.o: In function `read_JPEG_file':
/home/ustun/Downloads/jpeg_test/read_jpeg.c:37: undefined reference to `jpeg_std_error'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:45: undefined reference to `jpeg_CreateDecompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:46: undefined reference to `jpeg_stdio_src'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:47: undefined reference to `jpeg_read_header'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:48: undefined reference to `jpeg_start_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:62: undefined reference to `jpeg_read_scanlines'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:74: undefined reference to `jpeg_finish_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:75: undefined reference to `jpeg_destroy_decompress'
obj/read_jpeg.o: In function `read_JPEG_file_props':
/home/ustun/Downloads/jpeg_test/read_jpeg.c:93: undefined reference to `jpeg_std_error'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:100: undefined reference to `jpeg_CreateDecompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:101: undefined reference to `jpeg_stdio_src'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:102: undefined reference to `jpeg_read_header'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:103: undefined reference to `jpeg_start_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:113: undefined reference to `jpeg_read_scanlines'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:116: undefined reference to `jpeg_finish_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:117: undefined reference to `jpeg_destroy_decompress'
collect2: ld returned 1 exit status
make: *** [test_jpeg] Error 1
You can download a simple project with a Makefile here.
You'd have to give the full path to libjpeg.a, If you have libjpeg.a in a .libs folder relative to where you compile:
gcc mycode.c -o executable_name .libs/libjpeg.a
If your special libjpeg.a is elsewhere, give a full path to it.
If this fails, you have to tell us what happens. (the details are important, so please copy paste the exact errors and the exact command line that is run).
You need to use -static:
gcc -static -o exec_name mycode.c -ljpeg
No need to copy the archive (.a). You could have found out by reading man ld.
This may help you if you have the same problem as mine. In my system, I have
libjpeg.so.62 -> libjpeg.so.62.0.0
libjpeg.so.62.0.0
After I added symbolic link:
sudo ln -s libjpeg.so.62 libjpeg.so
My problem got solved.

Resources