[UNIX]: Do I need to add all the libraries to my makefile for the project, which is used in the library used in my project?

Ha, that sounds harder than it really is. Here is what I mean:

Suppose I am writing an application that uses threads. In this application, I do not use pthreads directly, but I use a shell that uses pthreads . So, -lpthread should be included in this make-up wrapper file. And the question is: do I need to include -lpthread in my project, or is it not needed? Or does it depend? If so, then what?

I ask about it because I saw it a lot and I donโ€™t think it is necessary .. The same for -std=c++0x ?


Also, I had a problem with the Informix C ++ interface, as it relies on a lib called DMI , which is built on top of ESQL/C When I deleted these libraries and used only the real one, I had problems with linkers (problems with finding libs). When I added DMI and ESQL/C , everything was fine.

Does this answer my question (with "YES"), or could I do something wrong, except for this (I'm new to make files (:)


: "Makefiles is something like a mysterious topic - one joke is that there is only one makefile in the world and that all other makefiles are just an extension of it. I assure you, however, that this is not true, I wrote from time to time your own files.

This made me wonder if all of the included libraries in makefiles are needed (from my company projects), or is this for "historical reasons"


EDIT . The pthread shell is linked to static, and Informix lib is dynamically linked, if that matters.
In addition, the OS is RHEL (4 and 5), but I need to know if it depends on the OS and the binding method (dynamic or static)

+2
source share
3 answers

Here you make a mistake:

So, in this make-up wrapper file, -lpthread should be included

The static library created by the ar tool is simply a collection of object files in a special file format that is later recognized by the linker. This code collection is not associated with any library. Specifying -lpthread when compiling each of the source files at this point is pointless because the link is not running.

Only when invoking the linker to create the final executable from all libraries and object files, do you need to pass specific libraries with the -l option. Please note that at this stage the compiler is not called, and -l not a compiler option, but a linker.

For example, this only calls the link because the source files are not provided:

 gcc -o myprog main.o -lmylib -lpthread 

In contrast, compiling the source file into an object file and specifying the library is pointless, since no binding will be done:

 gcc -c wrapper_source.c -lpthread 
+1
source

If the libraries are somehow not included in the shell used, the answer is YES, you will need to include them in the make file of the program that uses the shell.

For example, if the shell is .o, it is not yet associated with anything. However, if the shell is .so or .a, it may include libraries depending on how you built it. I think ar is responsible for such things.

+2
source

All dynamically linked libraries must be present.

Usually, when your application uses a wrapper in a shared library, it does not know about the cover makefile and cannot find that it needs to include pthread. On the other hand, the linker working on your application usually wants to make sure that no undefined characters remain in the project.

Therefore, you will have to add directives for all shared libraries your code depends on.

0
source

All Articles