10 月 2024

记一个linux系统c++动态库全局符号重名问题

首先,简单说明下问题情境,一共三个代码文件,一个主执行程序,两个动态库程序,结构与之前写的弱符号,weak symbol一致:

#include <string>

std::string g_name;

void init_name_lib1()
{
    g_name = "lib1";
}

const char* get_name_lib1()
{
    return g_name.c_str();
}
#include <string>

std::string g_name;

void init_name_lib2()
{
    g_name = "lib2";
}

const char* get_name_lib2()
{
    return g_name.c_str();
}
#include <iostream>

void init_name_lib1();
void init_name_lib2();
const char* get_name_lib1();
const char* get_name_lib2();

int main()
{
    init_name_lib1();
    init_name_lib2();

    std::cout << "name from lib1: " << get_name_lib1() << std::endl;
    std::cout << "name from lib2: " << get_name_lib2() << std::endl;

    return EXIT_SUCCESS;
}
Continue reading…