Recommended linking options Bsymbolic when creating dynamic libraries

Off topic: I recently learned about a lightweight markup language called Markdown . You can use Markdown 's readable text format and then convert it to HTML . Although I only knew, I found that this syntax is already everywhere, famous stack overflow posts, and some wikis, or some blogs all support this syntax editing. OK, then the blogger, try it this time. This blog is written in the MarkDownPad editor.

Problem Description

Back to the topic, in the project development some time ago, a dynamic library was implemented that encapsulates some methods. Then based on this dynamic library, an application is realized. The application contains global variable A, and the dynamic library also contains global variable A. When I call the function in the dynamic library, I find that the application's A has changed! ! ! O, My God! For someone like me who hasn't done development under Linux, it's a bit confusing. . . . . . So I tried to change the variable name in A to B, so the problem would be gone~~~

reason

When the application is linked, the global variable definition in the dynamic library will be overwritten by the global variable with the same name in the application. In this way, when the A variable is modified in the dynamic library, the A in the application also changes.

Solution

When creating a dynamic link library, add compile options to gcc/g++ options

-Wl,-Bsymbolic.

Where Wl represents the parameter that will be followed by the linker ld . Bsymbolic means that the local global variable definition is forced, so that the global variable definition of the dynamic link library will not be overwritten by the definition of the same name in the application/dynamic link library!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326071931&siteId=291194637