Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a library with headers and .so files?

Tags:

c++

c

I'm new to C and wanted to use a library (MLT Multimedia Framework)

I've built it and it produced the following directories: include lib share

Inside lib there are .so .a .la files
Inside include there are .h files

Now, I'm instructed to do this:

#include <framework/mlt.h> which is inside include/mlt/framework/

Questions:

  • Why I do I need to place the header file that contains only function prototypes? Where are the real functions then? are they linked someway to the ones included in lib directory?
  • Where to place my own files and how to compile it?
  • How to learn more about the topics:
    • Dynamic/Static libraries
    • Building / making / installing
    • How to use any C library
like image 924
Naughty.Coder Avatar asked Jul 02 '15 06:07

Naughty.Coder


People also ask

What is the use of header or library files?

Header Files: The files that tell the compiler how to call some functionality (without knowing how the functionality actually works) are called header files. They contain the function prototypes. They also contain Data types and constants used with the libraries. We use #include to use these header files in programs.

How do I read a .so file?

However, you might be able to read the SO file as a text file by opening it in a text editor like Leafpad, gedit, KWrite, or Geany if you're on Linux, or Notepad++ on Windows.

Is a header file the same as a library?

No. Header File is the file where all the headers name are mentioned that going to be used or consumed in the main code file. On other hand Library is the file where the implementation code of each header is written down which is mentioned in the Header file.


2 Answers

If you don't have the function prototypes, how would the compiler know what functions exist in the library? Short answer is: It doesn't. Longer answer: The compiler doesn't care about library files, static (files ending in .a) or shared (files ending in .so), all it cares about is the current translation unit. It's up to the linker to handle resolving undefined references.

When you use libraries, you include the header files that contain the needed declarations (structures, classes, types, function prototypes) into the source file. The source file plus all included header files forms the translation unit that the compiler uses to generate code. If there are undefined references (for example a call to a function in the library) the compiler adds special information about that to the generated object file. The linker then looks through all object files, and if it finds an unresolved reference it tries to find it in the other object files and the provided libraries. If all definitions are resolved the linker generates the final executable, otherwise it will report the unresolved definitions as errors.


To answer your other questions:

Where to place my own files and how to compile it?

This is two questions, the answer to the first one (about placement of your files) is that it doesn't really matter. For small project with only a few source and header files, it's common to place all files in a common project directory.

The second question, about compiling, there are different ways to do it too. If there are only one or two source files you could use the compiler frontend (e.g. gcc) to compile and link and generate your executable all in one go:

$ gcc -Wall -g source1.c source2.c -o your_program_name

The above command takes two source files, compiles and links them into the program your_program_name.

If you need to use a library, there are one or two things that you need to add to the above command line:

  1. You need to tell the linker to link with the library, this is done with e.g. the -l (lower case L) option:

    $ gcc -Wall -g source1.c source2.c -o your_program_name -lthe_library
    

    It's important to note that the_library is the base name of the library. If the library file is named libthe_library.so then only the_library part is needed, the linker will add the other parts automatically.

  2. If the library is not in a standard location, then you need to tell the compiler and linker where the library file are. This is done with the -I (capital i) option to tell the preprocessor where the header files are, and the -L (capital l) where the linker files are.

    Something like

    $ gcc -Wall -g -Ilocation/of/headers source1.c source2.c -o your_program_name -Llocation/of/libraries -lthe_library
    

If you have more than a couple of source files, it's common to use so called makefiles that lists all source files, their dependencies, compiler and linker flags, and contain rules on how to build object files and link the final program. Such a makefile could look like

CFLAGS  = -Wall -g
LDFLAGS = -g

SOURCES = source1.c source2.c
OBJECTS = $(SOURCES:.c=.o)

TARGET  = your_program_name

.PHONY: all
all: $(TARGET)

$(TARGET): $(OBJECTS)
    $(LD) $(LDFLAGS) $^ -o $@

%.o: %.c
    $(CC) $(CFLAGS) $< -c -o $@

The above makefile should do just about the same as the previous command line. The big difference is that it's much easier to add more source files, add special rules for special files, and most importantly, the make program will handle dependencies so that if one source file haven't been modified since last build then it won't be compiled. The last bit will make big projects with many source files build much quicker when only one or a few source files has been modified.


How to learn more about the topics [...]

By going to your favorite search engine, and looking for those topics there. I also recommend e.g. Wikipedia.


Of course, if you use an Integrated Development Environment (a.k.a. an IDE) then you don't have to compile from the command line, or to make your own makefiles, the IDE will handle all that for you. It will also have dialogs for the project settings where you can enter include paths and library paths, and what libraries to link with.

like image 51
Some programmer dude Avatar answered Sep 28 '22 18:09

Some programmer dude


Why I do I need to place the header file that contains only function prototypes?

So as to satisfy your compiler for declaration of those functions or declaration of classes. As C++ is static type checking language, they must know the type of objects which they will be using.

Where to place my own files and how to compile it?

You can place you code anywhere in you filesystem; only make sure to include .h files in includes path and lib while compiling. Usually you need to modify your path.

You can check about building on this link:

https://en.wikipedia.org/wiki/GNU_build_system

like image 44
mystic_coder Avatar answered Sep 28 '22 16:09

mystic_coder