Not able to link gcc against libbluetooth library - c

I am using BlueZ library for developing bluetooth based application for linux using C. I am trying to link libbluetooth-dev to my C file but it is not working.
/usr/bin/ld: cannot find -libbluetooth-dev
collect2: error: ld returned 1 exit status
My project directory is bluez-5.45, i am trying to build the project from this folder and added my .C file in this main folder
I installed libbluetooth-dev library and tryed that linking but not able to do that.
Command for linking:
gcc -o output myfile.c -libbluetooth
please let me know the mistake done by me.

The "lib" is implied, so link against "bluetooth" instead of "libbluetooth":
$ gcc -o output myfile.c -lbluetooth

Related

GCC:- unable to find libpthread_nonshared.a file while linking libraries to create a so file on Linux

Hi I'm trying to compile a c project with gcc 10.2 in Ubuntu 20.04.
But at the end it gives me error as below
/usr/bin/ld: cannot find /usr/lib/x86_64-linux-gnu/libpthread_nonshared.a
collect2: error: ld returned 1 exit status
Below are the libraries I'm trying to link while creating a so file
-lpthread -ldl -lm -lstdc++ -lrt
When i looked into /usr/lib64 i could see only *.so files but no *.a files.
Do we need to install them separately?
Could someone help with this.
Do we need to install them separately?
You need to install glibc-devel (or similar) package.

How to fix "cannot find -lz"

I am working on code have Zlib.h header, This header is found in my code folder, I compile this code by using
gcc -o x xx.c -lz
but I get on this
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status
This happen just with Linux that I installed in a VBox.
How to fix that.
Try installing 'zlib1g-dev'. On Ubuntu this following command will install the library.
sudo apt install zlib1g-dev
When you type gcc foo.c, you ask gcc to compile and link the given file.
1. Compilation
Compilation consist of transforming the source file into an object file.
This step need the included files, like zlib.h to be found by gcc.
This step seems to be correct on system.
NB: You can ask gcc to only do this step typing gcc -c foo.c, or better gcc -Wall -c foo.c
2. Link
Once the object files have be created, then need to be linked to create an executable file.
It's that step that failed for you: your linked can't find the already compiled functions needed by your code.
When linking with option -lz, you tell your linker "search for libz.so file to find the missing functions"
On current linux distribution, you can install package like libz-dev to install the .so file in well known places. (/lib, /usr/lib, /usr/local/lib...)
If you don't have the libz.so file installed on the library search path, you can specify where is the library to your linker.
For instance, if libz.so is if /bar/baz directory, you can type gcc foo.c /bar/baz/libz.so. The same for libz.a.
In any case, you'll need the libz.so file or at least the libz.a file
See also What's the difference between .so, .la and .a library files?

Cross-compiling PortAudio for MIPS fails with "cannot find -lasound" despite setting -L/usr/lib

I'm trying to compile an example C program that links against PortAudio for a MIPSEL OpenWRT architecture targeting the MT7688 chip.
My starting point is this 351MB Docker image that has a working MIPSEL GNU uclibc toolchain (run source env.sh to set environment variables).
I tried to cross-compile PortAudio at first, but I couldn't get it to output MIPS binaries despite configuring Makefile for --host=mips-openwrt-linux-uclibc. So I copied working libasound.so.2, libportaudio.so.2 and libportaudio.so.2 files from my MIPS device and placed them in both the source folder and /usr/lib, then passed explicit include and linker paths:
$CC I/snowboy/examples/C/portaudio/install/include -L/usr/lib -lasound demo.c -o demo -v
> /bin/ld: cannot find -lasound
collect2: error: ld returned 1 exit status
No dice. I am too dumb to understand GCC linker paths, but I've come this far. Can anyone help me to navigate this cross-compilation minefield?

makefile works with cygwin on windows but not ubuntu

I have the following folder on my git repo - https://github.com/ryu577/base/tree/master/numerical/c/NumericalRecipiesCode/lib
On my windows machine which has cygwin installed, I can run make in that directory, which triggers the command -
gcc -o ../bin/lib/tst_libfns ../obj/nrutil.o ../obj/fileio.o ../obj/tst_libfns.o -I ../include -lm
This puts the tst_libfns.o and fileio.o in the obj directory.
Now, I pulled this repository into my ubuntu machine and tried the same thing. However, when the same command is generated there, it gives me the following error:
gcc -o ../bin/lib/tst_libfns ../obj/nrutil.o ../obj/fileio.o ../obj/tst_libfns.o -I../include -lm
../obj/fileio.o:fileio.c:(.text+0x52): undefined reference to `__getreent'
../obj/tst_libfns.o:tst_libfns.c:(.text+0x10): undefined reference to `__main'
collect2: error: ld returned 1 exit status
make: *** [../bin/lib/tst_libfns] Error 1
Am I missing something obvious?
I just figured it out. I had pulled in the .o files through git in the obj directory and gcc was somehow trying to use the existing files (which had been generated in Cygwin + Windows). Somehow, those .o files don't seem to be compatible with linux. When I delete them and run the make command again, the .o files are re-generated. I guess the moral of the story here is that binaries and executables generated by GCC in Windows are incompatible with Linux (and I'll guess vice versa).

ld: library not found for -lplot

I am new here. I recently installed plotutils-dev on my mac using fink, but when I try to compile a little program I have by doing
gcc -g -o atomos.o atomos.c -lplot
it says
ld: library not found for -lplot
collect2: ld returned 1 exit status
I have searched the problem on the web with little success. The only thing I know is that when I type
dpkg -S libplot.dylib
it says
plotutils-dev: /sw/lib/libplot.dylib
which I believe it means I have installed libplot on my mac. So I don't know what is the problem. Any help is welcomed. I am a beginner so It would be nice if some guidelines are provided in a user-friendly way.
The linker can't find the libplot library. I'm not familiar with mac, but with gcc you can tell it the path to the library with the -L flag, e.g.:
gcc -g -o atomos.o atomos.c -lplot -L/sw/lib/
(I'm guessing at that path, but you can probably figure out the path to the library if that isn't right.)
Also, it's probably a typo in your question, but I changed it to -lplot (note extra -l). You want the -l to link with the plot library.

Resources