How can I compile the Mongodb-c-driver program? - c

I use
gcc -o mongotest mongotest.c $(pkg-config --cflags --libs libmongoc-1.0)
for compilation mongodb c driver code, and then
LD_LIBRARY_PATH=/usr/local/lib ./mongotest
for run.
If I try without LD_LIBRARY_PATH=/usr/local/lib I have
./mongotest: error while loading shared libraries: libmongoc-1.0.so.0: cannot open shared object file: No such file or directory
How can I run program without LD_LIBRARY_PATH=/usr/local/lib,
Is it correct to do so? How can I debug mongodb c driver programs?

The path /usr/local/lib should be in /etc/ld.so.conf or in one of the files in /etc/ld.so.conf.d/ directory. So the library mongoc will get into the cache when you issue command ldconfig as root.
You can check if library mongoc is in dynamic linker cache by executing this command
ldconfig -p | grep mongoc
If it is you are safe to run your program without explicitly specifying LD_LIBRARY_PATH.
Other way to check if your executable is correctly linked is by getting the linking information with ldd command. It will print all linked libraries.
ldd mongotest
If you can see /usr/local/lib/libmongoc.so.[numbers] that means it is linked with mongoc library.

Related

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?

GCC have include but not library

I'm writing my own kernel for fun, and in doing so I've needed to install glibc to use the standard C libraries. However, after installing the library to the desired directory, my kernel.c program includes the stdio.h header and attempts to use fopen, however I come across this error:
kernel.c:(.text+0x238): undefined reference tofopen'`
After looking around I noticed that I don't have any actual code to all of the header files, just the header files themselves. So I went and added the -L flag to GCC to add the lib folder that was created during the compilation of glibc and what I've found out is that the lib folder has nothing of what I need.
I poked around and found that the build directory I used when compiling glibc has the .o files I'm looking for (e.g it has iofopen.o for the fopen method).
So what's going on?
If needed, the commands I am using to compile my kernel are:
#!/bin/bash
nasm -felf32 boot.asm -o boot.o
/home/noah/opt/cross/bin/i686-elf-gcc -I/home/noah/Documents/NoahOS/include/ -L/home/noah/Documents/glibc/build -c *.c -std=gnu99 -ffreestanding -Wall -Wextra
/home/noah/opt/cross/bin/i686-elf-gcc -I/home/noah/Documents/NoahOS/include/ -L/home/noah/Documents/glibc/build -T linker.ld -o noahos.bin -ffreestanding -O2 -nostdlib *.o -lgcc
First line builds the boot file, which is assembly.
Second line runs gcc on all of the C language .c files and creates their object files.
Third line links all of the files together with linker.ld and outputs the final kernel to noahos.bin which is a runnable kernel using
qemu-system-i386 -kernel noahos.bin
If needed more information can be provided. Please ask.
You are correctly compiling your kernel using -nostdlib because the kernel can't use the standard library. Why not? Because it doesn't make sense: the standard library is the interface between user programs and the kernel, so that application developers don't need to know the system call specification for your kernel, all that is required is a port of the C library.
Oh, there's the answer. You need a port of the C library to use your own system calls. Starting with glibc might not be the easiest to port (it comes with the kitchen sink).

scons executable + shared library in project directory

Here's a sample SConscript file:
env = Environment()
hello_lib = env.SharedLibrary('hello', ['libhello.c'])
exe = env.Program('main', ['main.c'], LIBS=hello_lib)
env.Install('/usr/lib', hello_lib)
env.Install('/usr/bin', exe)
env.Alias('install', '/usr/bin')
env.Alias('install', '/usr/lib')
It builds one shared library, and one executable linked to that library:
$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o libhello.os -c -fPIC libhello.c
gcc -o libhello.so -shared libhello.os
gcc -o main.o -c main.c
gcc -o main main.o libhello.so
scons: done building targets.
Now, the issue is the created executable will not find the shared library when running it from the project directory,
which is quite natural, since neither the LD_LIBRARY_PATH env variable is set, or
any RPATH is set in the executable:
[fedora 00:07:10 2 ~] $ ./main
./main: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory
I can always set the LD_LIBRARY_PATH variable while developing, but this becomes cumbersome if the project has a directory hierarchy with several shared libraries in sub_directories.
The GNU autotools/libtool solves this by automagically set the RPATH of the executable to wherever the shared libraries are built in the project directory, which allows for easy running/testing the executable while developing. And it relinks the executable when installing it to leave out those RPATH which arn't needed anymore.
Is there anything similar to what autotools does that can be done with scons to ease testing the executables while developing ?
Is there any recommended way to build applications using shared libraries with scons, that makes it easy to run the executable from the build directory ?
You could modify each of the SConscript files which produce libraries, like so:
hello_lib = env.SharedLibrary('#/lib/hello', ['libhello.c'])
All of your shared libraries are now located in a single directory.
The SConscript which produces an executable becomes:
exe = env.Program('main', ['main.c'], LIBPATH='#/lib', LIBS=hello_lib)
Then you will be able to set LD_LIBRARY_PATH to $PWD/lib.
It looks like you are looking for the RPATH option in scons.
From the wiki page, the RPATH is described as scons as the following.
A list of paths to search for shared libraries when running programs.
Currently only used in the GNU linker (gnulink) and IRIX linker
(sgilink). Ignored on platforms and toolchains that don't support it.
Note that the paths added to RPATH are not transformed by scons in any
way: if you want an absolute path, you must make it absolute yourself.

Error when using libraries in C

I'm trying to play an mp3 file in terminal using C and I followed this link to do so.
I've installed the two libraries libmpg123 and libao. Also, I've compiled the play.c program using the command:
gcc -O2 -o play play.c -lmpg123 -lao
But I get the following error when I run it:
./play: error while loading shared libraries: libao.so.4: cannot open shared object file: No such file or directory
Can you figure it out why it happened.
The executable can be linked, but at run-time, it cannot find the shared libraries. Add the libraries to your LD_LIBRARY_PATH so the program can find them at run time.

linking libraries -rpath LD_LIBRARY_PATH

I have some 3rd party libraries and includes (I have copied them to the this location /usr/ssd/include and /usr/ssd/lib) that I need to link with my application. I have just created a test application to see if I can link ok. However, when I try to run my app I get the following message.
./app: error while loading shared libraries: libssdn.so: cannot open shared object file: No such file or directory
On the command line I am compiling like this:
gcc -g -Wall -I/usr/ssd/include -L/usr/ssd/lib -lssdn test_app.c -o app
Everything compiles ok, as I don't get any warnings or errors. However, I get the error when I try and run the app.
In the usr/ssd/lib the library is called libssdn.so
I am been looking for solution and I have read something about -rpath, -Wl and LD_LIBRARY_PATH, but not sure what they are and how to include them when I compile.
I am using Ubuntu 9.04 Linux,
Thanks for any advice,
Test if adding /usr/ssd/lib to your $LD_LIBRARY_PATH helps:
In a shell:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/ssd/lib
If that solves the problem, make it permanent by adding /usr/ssd/lib to /etc/ld.so.conf or by running
ldconfig -n /usr/ssd/lib
My personal preference is not to bake the location of a shared object into an executable (which is what -rpath would do).
Instead, you should add /usr/ssd/lib to your LD_LIBRARY_PATH at run time. Assuming you are running bash or a bash like shell, do:
export LD_LIBRARY_PATH=/usr/ssd/lib:${LD_LIBRARY_PATH}
and once you do that, you can run your executable.

Resources