我正在尝试创建一个库,其中包含两个版本的同一函数,使用的是
这里是版本脚本文件。
__asm__(".symver ......
approach
library.h
#ifndef CTEST_H
#define CTEST_H
int first(int x);
int second(int x);
#endif
library.cpp
#include "simple.h"
#include <stdio.h>
__asm__(".symver first_1_0,first@LIBSIMPLE_1.0");
int first_1_0(int x)
{
printf("lib: %s\n", __FUNCTION__);
return x + 1;
}
__asm__(".symver first_2_0,first@@LIBSIMPLE_2.0");
int first_2_0(int x)
{
int y;
printf("lib: %d\n", y);
printf("lib: %s\n", __FUNCTION__);
return (x + 1) * 1000;
}
int second(int x)
{
printf("lib: %s\n", __FUNCTION__);
return x + 2;
}
这里是版本脚本文件。
LIBSIMPLE_1.0{
global:
first; second;
local:
*;
};
LIBSIMPLE_2.0{
global:
first;
local:
*;
};
当使用gcc构建库时,一切正常,我能够链接到一个库二进制文件。使用nm工具,我看到first()和second() 函数符号都被导出。现在,当我尝试使用g++时,没有任何符号被导出。因此,我尝试使用extern "C"指令来包装两个声明。
extern "C" {
int first(int x);
int second(int x);
}
nm显示second()函数符号被导出,但first()仍然未被导出且被混淆。
我错过了什么使它能够工作吗?或者用C++编译器实现这一点是不可能的吗?