内联方法作为共享库的符号不可用
inline methods not available as symbols of shared library
如果我有一个类,它将被编译为共享对象。该类在.h文件中声明,并在.cpp文件中实现。
此类包含两个内联方法。
如果我写了一些程序,链接到这个共享库并包含.h文件,链接时我会得到未定义的引用错误。
这是因为没有为内联方法导出符号吗?
我的理解正确吗?
更新:下面的一些示例代码
somelib.h
#ifndef __ABC_LIB_H
#define __ABC_LIB_H
#include <iostream>
class ABC {
ABC();
~ABC();
inline void not_callable_outside_library();
void callable_outside_library();
};
#endif
somelib.cpp
#include "somelib.h"
ABC::ABC() {}
ABC::~ABC() {}
void ABC::not_callable_outside_library(){ std::cout<<"not_callable_outside_library"<<std::endl; }
void ABC::callable_outside_library(){ std::cout<<"callable_outside_library"<<std::endl; }
程序.cpp
#include "somelib.h"
int main() {
ABC x;
x.not_callable_outside_library();
return 0;
};
将somelib.cpp编译为共享库(.so对象)并将其链接到program.cpp,然后获得名为program的二进制文件。
现在,链接时应该会得到一个未定义的引用。
内联函数必须在头文件中定义。将您所拥有的与以下内容进行对比:
somelib.h:
#ifndef ABC_LIB_H
#define ABC_LIB_H
#include <iostream>
class ABC {
ABC();
~ABC();
void not_callable_outside_library();
void callable_outside_library();
};
inline void ABC::not_callable_outside_library() {
std::cout << "not_callable_outside_libraryn";
}
#endif
somelib.cpp:
#include "somelib.h"
ABC::ABC() { }
ABC::~ABC() { }
void ABC::callable_outside_library() {
std::cout << "callable_outside_libraryn";
}
program.cpp:
#include "somelib.h"
int main() {
ABC x;
x.not_callable_outside_library();
}
相关文章:
- 为什么我的共享库中存在展开符号
- 仅在少数方法(静态或共享库)中解析的外部符号
- 共享对象、符号、C/C++ 库链接和加载
- 共享对象中的符号
- 如何防止 CMake 在构建时(而不是在安装时)为共享库创建符号链接?
- 使用共享库编译可执行文件时仅链接所需的符号
- 为什么 std:: 符号在共享库中被强制导出?
- 使用 C++ 标准库避免共享库中的符号冲突
- 隐藏共享库中的"_init"和"_fini"符号
- 共享对象中的符号名称与文件中.cpp函数不同
- C++:在共享对象中调用抽象基类构造函数/未定义的符号
- 共享库中使用"使用命名空间"定义的符号错误
- GCC 中使用 -O3 的共享库编译不会导出与 -O0 中那样多的符号
- Cython:共享对象中未定义的符号
- 不同的数学符号绑定与共享库与 dlopen 并直接链接到可执行文件 (Linux)
- 尝试与从 CUDA 对象构建的共享库链接时未定义的符号
- 在链接链接静态(GSOAP)库的共享库时,为什么会得到一个未定义的符号
- 共享库中非模板基的模板子类导致未定义的符号类型信息'class'链接错误
- 静态库将转换为共享的lib符号隐藏
- 在共享C++库中加载共享库时C++未定义的符号,该库本身由 Python 加载