对静态库中类变量的未定义引用
undefined reference to a class variable inside a static library
我有liblib。lib.h和lib.cpp:
#ifndef LIB_H
#define LIB_H
namespace N1 {
namespace N2 {
class C1 {
C1();
public:
enum DAY { MONDAY, TUESDAY, END };
struct DAY_PAIR {
const int index;
const int garbage;
DAY_PAIR(int i, int g) : index(i), garbage(g) {};
};
static const DAY_PAIR MONDAY_PAIR;
static const DAY_PAIR* PAIRS[END];
static void init();
};
}
}
#endif
#include <iostream>
#include "lib.h"
namespace N1 {
namespace N2 {
const C1::DAY_PAIR C1::MONDAY_PAIR(MONDAY, 1234);
const C1::DAY_PAIR* PAIRS[] = {&C1::MONDAY_PAIR};
void C1::init() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
}
}
我试着把我的虚拟程序链接到lib.a:
#include <iostream>
#include "lib.h"
int main() {
N1::N2::C1::init();
std::cout << N1::N2::C1::PAIRS[N1::N2::C1::MONDAY]->index << std::endl;
return 0;
}
和g++给了我:
/tmp/ccKKqDsT.o: In function `main':
/home/h/test/cpp/nested.cpp:7: undefined reference to `N1::N2::C1::PAIRS'
collect2: ld returned 1 exit status
如果我不创建liblib。然后尝试将所有.cpp文件编译成可执行文件。
我错过了什么吗?
您的.cpp文件const C1::DAY_PAIR* PAIRS[] = {&C1::MONDAY_PAIR};
行有问题
应该是const C1::DAY_PAIR* C1::PAIRS[] = {&C1::MONDAY_PAIR};
吗
lib.cpp中pair前缺少类名,所以:
const C1::DAY_PAIR* C1::PAIRS[] = {&C1::MONDAY_PAIR};
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用