在 ARM mbed 中使用回调时未定义的引用
Undefined reference when using callbacks with ARM mbed
上下文:我正在为名为 Switch 的 InterruptIn 制作开关去抖动包装器 class。开关将允许用户附加他们自己的功能,当按下和去抖动真实世界的开关时,这些功能应该被回调。我将 Eclipse C++ 与 arm-none-eabi-* 工具链一起使用。
首先,我找到了三种使用 mbed 实现回调的方法:
FunctionPointer class,它已被 ARM 弃用,但 has some documentation(我认为我需要的信息的超链接已损坏)。
Callback,编译器告诉我这是在尝试使用已弃用的 FunctionPointer 时执行回调的正确方法TM,但没有文档?
event_callback_t,没有说明文档,负责调用用户附加函数的函数接受一个int作为参数?
我将使用 Callback 方式来说明我遇到的问题,但这三种方式都会产生相同的编译错误。
Switch.h:
#ifndef MBED_SWITCH_H
#define MBED_SWITCH_H
#include "mbed.h"
class Switch {
private:
Callback<void()> callback_press_;
InterruptIn switch_;
//redacted some debouncing stuff
public:
Switch(PinName pin, bool isActiveHigh);
Switch(PinName pin, bool isActiveHigh, int samplePeriod_us, int sampleCount);
void attachPress(Callback<void()>); //THE PROBLEM FUNCTIONS
template<typename T, typename F>
void attachPress(T* object, F function);
};
#endif
Switch.cpp:
#include "Switch.h"
#include "CustomNames.h"
// redacted constructors here
// redacted debouncing functions here
// for attaching a function not associated with any particular class
void Switch::attachPress(Callback<void()> function) {
callback_press_.attach(function);
}
// for attaching a member function of some other class
template<typename T, typename F>
void Switch::attachPress(T* object, F function) {
callback_press_.attach(object, function);
}
问题
如果我使用第一个候选函数 attachPress(Callback function) 附加一个与任何特定 class 无关的函数,那么在现实世界中一切都编译、链接和工作正常。 =14=]
但是,当我尝试将另一个 class "Test" 的特定成员函数 "testfunc1(void)" 附加到第二个候选函数时,出现以下错误(从链接器,我想想):
Test.o: In function `Test::Test()':
Test.cpp:(.text._ZN4TestC2Ev+0x1e): undefined reference to `void Switch::attachPress<Test, void (Test::*)()>(Test*, void (Test::*)())'
/home/yankee/programming_projects/cpp-eclipse/SO_EXAMPLE/Makefile:97: recipe for target 'HW4_RACE.elf' failed
collect2: error: ld returned 1 exit status
Makefile:21: recipe for target 'all' failed
make[1]: *** [HW4_RACE.elf] Error 1
make: *** [all] Error 2
03:10:35 Build Finished (took 165ms)
为什么这会给我 "undefined reference",有没有办法解决它?
模板化 类 函数在使用前不会被实例化。
也就是说,使用模板类和函数的翻译单元需要该模板的完整代码。
在这种情况下,只需移动:
template<typename T, typename F>
void Switch::attachPress(T* object, F function) {
callback_press_.attach(object, function);
}
从 Switch.cpp
到 Switch.h
都可以。
上下文:我正在为名为 Switch 的 InterruptIn 制作开关去抖动包装器 class。开关将允许用户附加他们自己的功能,当按下和去抖动真实世界的开关时,这些功能应该被回调。我将 Eclipse C++ 与 arm-none-eabi-* 工具链一起使用。
首先,我找到了三种使用 mbed 实现回调的方法:
FunctionPointer class,它已被 ARM 弃用,但 has some documentation(我认为我需要的信息的超链接已损坏)。
Callback
,编译器告诉我这是在尝试使用已弃用的 FunctionPointer 时执行回调的正确方法TM,但没有文档? event_callback_t,没有说明文档,负责调用用户附加函数的函数接受一个int作为参数?
我将使用 Callback
Switch.h:
#ifndef MBED_SWITCH_H
#define MBED_SWITCH_H
#include "mbed.h"
class Switch {
private:
Callback<void()> callback_press_;
InterruptIn switch_;
//redacted some debouncing stuff
public:
Switch(PinName pin, bool isActiveHigh);
Switch(PinName pin, bool isActiveHigh, int samplePeriod_us, int sampleCount);
void attachPress(Callback<void()>); //THE PROBLEM FUNCTIONS
template<typename T, typename F>
void attachPress(T* object, F function);
};
#endif
Switch.cpp:
#include "Switch.h"
#include "CustomNames.h"
// redacted constructors here
// redacted debouncing functions here
// for attaching a function not associated with any particular class
void Switch::attachPress(Callback<void()> function) {
callback_press_.attach(function);
}
// for attaching a member function of some other class
template<typename T, typename F>
void Switch::attachPress(T* object, F function) {
callback_press_.attach(object, function);
}
问题
如果我使用第一个候选函数 attachPress(Callback 但是,当我尝试将另一个 class "Test" 的特定成员函数 "testfunc1(void)" 附加到第二个候选函数时,出现以下错误(从链接器,我想想): 为什么这会给我 "undefined reference",有没有办法解决它?Test.o: In function `Test::Test()':
Test.cpp:(.text._ZN4TestC2Ev+0x1e): undefined reference to `void Switch::attachPress<Test, void (Test::*)()>(Test*, void (Test::*)())'
/home/yankee/programming_projects/cpp-eclipse/SO_EXAMPLE/Makefile:97: recipe for target 'HW4_RACE.elf' failed
collect2: error: ld returned 1 exit status
Makefile:21: recipe for target 'all' failed
make[1]: *** [HW4_RACE.elf] Error 1
make: *** [all] Error 2
03:10:35 Build Finished (took 165ms)
模板化 类 函数在使用前不会被实例化。
也就是说,使用模板类和函数的翻译单元需要该模板的完整代码。
在这种情况下,只需移动:
template<typename T, typename F>
void Switch::attachPress(T* object, F function) {
callback_press_.attach(object, function);
}
从 Switch.cpp
到 Switch.h
都可以。