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


i'm new c , wanted use library (mlt multimedia framework)

i've built , produced following directories: include lib share

inside lib there .so .a .la files
inside include there .h files

now, i'm instructed this:

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

questions:

  • why need place header file contains function prototypes? real functions then? linked someway ones included in lib directory?
  • where place own files , how compile it?
  • how learn more topics:
    • dynamic/static libraries
    • building / making / installing
    • how use c library

if don't have function prototypes, how compiler know functions exist in library? short answer is: doesn't. longer answer: compiler doesn't care library files, static (files ending in .a) or shared (files ending in .so), cares current translation unit. it's linker handle resolving undefined references.

when use libraries, include header files contain needed declarations (structures, classes, types, function prototypes) source file. source file plus included header files forms translation unit compiler uses generate code. if there undefined references (for example call function in library) compiler adds special information generated object file. linker looks through object files, , if finds unresolved reference tries find in other object files , provided libraries. if definitions resolved linker generates final executable, otherwise report unresolved definitions errors.


to answer other questions:

where place own files , how compile it?

this 2 questions, answer first 1 (about placement of files) doesn't matter. small project few source , header files, it's common place files in common project directory.

the second question, compiling, there different ways too. if there 1 or 2 source files use compiler frontend (e.g. gcc) compile , link , generate executable in 1 go:

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

the above command takes 2 source files, compiles , links them program your_program_name.

if need use library, there 1 or 2 things need add above command line:

  1. you need tell linker link library, done e.g. -l (lower case l) option:

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

    it's important note the_library base name of library. if library file named libthe_library.so the_library part needed, linker add other parts automatically.

  2. if library not in standard location, need tell compiler , linker library file are. done -i (capital i) option tell preprocessor header files are, , -l (capital l) 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 have more couple of source files, it's common use called makefiles lists source files, dependencies, compiler , linker flags, , contain rules on how build object files , link final program. such makefile like

cflags  = -wall -g ldflags = -g  sources = source1.c source2.c objects = $(sources:.c=.o)  target  = your_program_name  .phony: all: $(target)  $(target): $(objects)     $(ld) $(ldflags) $^ -o $@  %.o: %.c     $(cc) $(cflags) $< -c -o $@ 

the above makefile should same previous command line. big difference it's easier add more source files, add special rules special files, , importantly, make program handle dependencies if 1 source file haven't been modified since last build won't compiled. last bit make big projects many source files build quicker when 1 or few source files has been modified.


how learn more topics [...]

by going favorite search engine, , looking topics there. recommend e.g. wikipedia.


of course, if use integrated development environment (a.k.a. ide) don't have compile command line, or make own makefiles, ide handle you. have dialogs project settings can enter include paths , library paths, , libraries link with.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -