How to compile ffmpeg.c into shared library? - c

Is it possible to compile ffmpeg.c into the shared library? I need it in shared library form to link with my C program and call main function in ffmpeg.c.

Yes, it is possible. To do so you'd need to:
Find out how you gonna pass the arguments (if via argc/argv no changes would be needed)
Rebuild the object files linked into ffmpeg shared library with -fpic
Pass the -shared option to the linker

Related

If GCC only finds a static library and not a dynamic one, does it resort to static linking the found file?

Please consider the following command:
gcc myfile.c -o myapp.exe -Lmydir -lmylib
mydir inclues the following file:
`libmylib.a`
I know that by default, gcc wants to do dyanmic linking. However, as you can see it does not find a libmylib.so dynamic library, only a libmylib.a static one.
Does that mean that gcc will statically link libmylib.a into the result executable?
This behavior is up to the linker, not GCC. Standard linker behavior is to try a dynamic library then a static one in each path component of the link search path. For example, if the first directory searched has only a static library by the name, the second directory will not be searched at all, even if it has a dynamic one. As such, for normal user expectations to work correctly, corresponding dynamic and static versions of the same library need to be placed in the identical location.

How to write common functions for reusing in C

I was trying to write a common function for other files could reuse it, the example as following, I have three files:
The first file: cat test1.h
void say();
The second file: cat test1.c
void say(){
printf("This is c example!");
}
The third file: cat test2.c
include "test1.h"
void main(){
say();
}
but when I ran: gcc -g -o test2 test2.c
it threw error as:
undefined reference to `say'
Additionally: I knew this would work:gcc -g -o test2 test1.c test2.c
but I don't wanna do this, because the other team would use the server, and I hope them directly use my binary code not source code. I hope that just like we use printf() function, we just need include .
You can build yourself a library from the object files containing your useful functions, and store the header(s) that describe them in a convenient location. You and your colleagues then compile with the headers and link that library with any executables that use any of those functions. That's very much the same general mechanism that the C compiler uses to include the standard headers and automatically link with the standard C library.
The mechanics vary a bit depending on platform (Windows vs Unix being the primary distinction, though there are differences between Unix platforms too), and also on the type of library (static archive vs dynamic linked / loaded libraries — also known as shared objects or shared libraries).
In broad outline, for a Unix system with a static library, you'd:
Compile library object files libfile1.o, libfile2.o, … using (for example) gcc -c libfile1.c libfile2.c.
Create an archive from the object files — using for example ar r libname.a libfile1.o libfile2.o.
Copy the headers to a standard location such as /usr/local/include.
Copy the library to a standard location such as /usr/local/lib.
You'd compile any code that uses the library functions with -I/usr/local/include (if that is not already a standard compilation option).
You'd link the programs with -L/usr/local/lib -lname (you might not need to specify -L… but you would need to specify -lname).
Including a header file does not make a function available. It simply informs the compiler that the function will be provided at a later time.
You should compile the file with the function into a shareable object file (or a library if there is more than one function that you want to share). Mind the switch -c which tells gcc not to build an executable file:
gcc -o test1.o test1.c -c
Similarly, compile the main function into its own object file. Now you or anyone else can link the object file with their main program:
gcc -o test2 test2.o test1.o
The process can be automated using make.
Other programmers can use compiled object files (`*.o') in their programs. They need only to have a header file with function prototypes, extern data declarations and type definitions.
You can also wrap many object files into the library.
On many systems you can also create the dynamic linked libraries which do not have to be linked into the executable.
you also need to compile test1:
gcc -g -o test2 test1.c test2.c.

How can I link with my own pthread library

I want to make some modifications to the pthread library used for my program. That is why I want to link with my own modified pthread library. I can take the source code in glibc for pthread, modify it and use that for my programs.
Normally you use the flag -pthread for linking with the original pthread library. How do I specify in my makefile to link with my own library.
Just use the -L option to specify the directory where your custom lib is present and use -l option to specify the name of your library.
For Ex:
-L/root/x/mylib -lmypthread
In this case your lib name should libmypthread.so
Refer to http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html for more details.
To ensure that the library is found for loading when you execute your program, do
export LD_LIBRARY_PATH = $(LD_LIBRARY_PATH):/root/x/mylib
-pthread is equivalent to -lpthread -D_REENTRANT with gcc/glibc. You can do the same, but with a different link argument (-lname-of-library).
Don't use -pthread. It is an undocumented GCC option, probably for compatibility with some other (Solaris?) compilers.
The -D_REENTRANT definition which it -pthread enables is completely unnecessary in glibc; none of the headers depend on this macro for thread safety. (The last of such mechanisms were removed from the glibc headers in 1998!) Simply linking in -lpthread is enough to switch glibc functions to thread safe mode, and -lpthread can be substituted with your own library, like the other answer says.
Compile the library in a different name, e.g., libmypthread.so and put it in one of the directories contained in your LD_LIBRARY_PATH environment variable (or add a new directory). Now you can use -lmypthread to link to your library.

How do I combine a shared object (.so) and a static library (.a) into a new shared object?

I have a library; call it libdog.so.
I do not have the source to libdog.so.
I do not have the .o files which went into libdog.so.
ldd libdog.so
libdogfood.so.1 => not found
libdog depends on libdogfood.
I have a static dogfood library, libdogfood.a and libdogfood.la.
I want to create a new library, libcompletedog.so, which has no
dependency on libdogfood.
I want libcompletedog to include all symbols from libdogfood.
Most UNIX systems (AIX is the exception) consider .so libraries a "final" product of the link, that can not be relinked into something else.
If your libdogfood.a is a 32-bit library, you might be able to link it into libdogfood.so.1, and thus satisfy the missing dependency:
gcc -shared -o libdogfood.so.1 \
-Wl,--whole-archive libdogfood.a -Wl,--no-whole-archive
If libdogfood.a contains 64-bit objects, above may still work (if the objects were compiled with -fPIC), but that's somewhat unlikely.
Basically you cannot do that, because libdog.so was compiled with -fPIC while libdogfood.a wasn't. Shared libraries need (in practice) to contain only position independent code
(otherwise there is too much relocation information inside them)

creating shared library using other library in linux

I have a shared library say "libeval.so". I am using this in my project to create on more shared library called say "lidpi.so". The library called "libdpi.so" is used by a tool. Now, this tool cannot see any other library other than "libdpi.so". I am using few function calls that are present in "libeval.so", and these are not present in "libdpi.so". Is there any switch in gcc, or something to overcome this.
If libdpi.so is designed so that it can open libeval.so, then your program only needs to know about libdpi.so.
Specifically, libdpi.so should have some function that calls dlopen, probably like this:
dlopen("path/to/libdpi.so", RTLD_LAZY);
Then other functions in libdpi.so can interface with libeval.so.
Edit: To build a shared library, use this command:
gcc -shared -o libdpi.so [list of object files to go in libdpi.so]
Note: When you build your objects, use the -fPIC command argument with gcc, like this:
gcc -fPIC -o foo.o foo.c

Resources