I'm building a Qt/C++ app. This App must connect to an android device through MTP. during a mtp copy, I had to provide a C callback to the MTP API (C-only)
I have declared this callback is below:
DeviceMngr.cpp
int fileprogress(const uint64_t sent, const uint64_t total, void const * const data) {
int percent = (sent * 100) / total;
if (Transfer_Cancelled == true)
return 1;
return 0;
}
DeviceMngr.h
extern bool Transfer_Cancelled;
extern "C" {
int fileprogress(const uint64_t sent, const uint64_t total, void const * const data);
}
And it's called in the method below:
uint32_t DeviceMngr::CreateFile(QString filename, uint32_t parent_id) {
...
ret = LIBMTP_Send_File_From_File(Device->device, strdup(AbsolutePath), genfile, fileprogress, NULL);
...
The Transfer_Cancelled is used :
void DeviceMngr::CancelTransfer() {
Transfer_Cancelled = true;
}
and
DeviceMngr::DeviceMngr()
{
...
Transfer_Cancelled = false;
}
And also in the method instantiation to make sure it's init to false.
Here is the issue:
Undefined symbols for architecture x86_64:
"_Transfer_Cancelled", referenced from:
DeviceMngr::DeviceMngr() in devicemngr.o
DeviceMngr::CreateFile(QString, unsigned int) in devicemngr.o
_fileprogress in devicemngr.o
DeviceMngr::CancelTransfer() in devicemngr.o
ld: symbol(s) not found for architecture x86_64
TransferCancel is only define DeviceMngr.c and any other place.
Any idea ?
It has nothing to do with the function, it's the variable Transfer_Cancelled
that is the problem. It's a problem because you define it in the header file, and since you define it in the header file it will be defined in all source files ( translation units ) where the header file is included.
You should only declare the variable in the header file, by doing eg
extern bool Transfer_Cancelled;
Add ifndef
to avoid several include
#ifndef FOO_H
#define FOO_H
extern "C" {
bool Transfer_Cancelled;
int fileprogress(const uint64_t sent, const uint64_t total, void const * const data);
}
#endif
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.