0

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 ?

1
  • you include several time the header Commented May 28, 2015 at 7:12

2 Answers 2

1

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 e.g.

extern bool Transfer_Cancelled;
Sign up to request clarification or add additional context in comments.

6 Comments

I have done this I have declared the Transfer_Cancelled in the same header but out of extern "C" {
I have changed the code in the description and also change the build isse
@Seb You still need to define the variable. Define it in one single source file.
I move the extern bool.. from header to cpp file but still the same issue
@Seb Keep the extern bool Transfer_Cancelled; in the header file, and in a source file add bool Transfer_Cancelled = false;. Declaration in header file, definition in source file.
|
0

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

1 Comment

Include guards only prevents multiple inclusion in a single source file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.