C + + ODR rule and dlopen problem

Source: Internet
Author: User
Tags class definition

Questions :

Development platform *.so plug-in when encountering the same function name appears in different. So files, assuming that a.so and b.so,b.so to use the definition function a () in a.so, and Dlopen will load a.so and then load b.so, when b.so is opened, A null pointer error is reported.

Basic Concepts :

The ODR is interpreted in the C + + standard as:

1. No compilation unit can contain variables, functions, enumerations, classes, or templates defined more than once.

2. All programs must and can contain only one time all non-inline functions and objects that are used in it.

3. The definition of a class must and can only occur once in a compilation unit that requires a complete definition of the class.

4. Include classes, enumerations, class templates ... And so on. Some definitions can occur multiple times in a program, but the following conditions must be met:

(1) All defined token sequences must be the same (tokens you can think of as valid language elements, blank, line-wrapping comments, etc.)

(2) All named lookups must point to the same entity, that is, you cannot make some namespace typedef, and let these same tokens represent different meanings.

(3) All operators must represent the same overload

(4) For all functions with default parameters in the entity you want to define, the default parameters must meet the above three

(5) For a class definition, the base class constructor that is called in the constructor must be the same

All in all, the fourth article means that there can be no ambiguity between the different definitions.

Solution:

Through analysis, for this case, the cross-. So call function, the function name needs to be exposed at compile time, otherwise external. So will not find the definition of the function.

Specifically, add the A.map file to the a.so compilation, where you need to add a function to expose to the outside. Colleagues need to modify the makefile file. The specific contents are as follows:

The method of exposure is to compile the function name of global and Local in the A.map file and specify the content by compiling the parameters:

Compile command:

1 gcc-wall-g-fpic-shared./a.c-o a.so-wl,--Version-script=a.map

Map File A.map

1 {2global:  3    global_fun_name;   4 local:*;   5

C + + ODR rule and dlopen problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.