3

In a C++ project, I'm trying to call this function, which is defined in C:

int CyBtldr_RunAction(CyBtldr_Action action, const char* file, const uint8_t* securityKey, 
    uint8_t appId, CyBtldr_CommunicationsData* comm, CyBtldr_ProgressUpdate* update);

CyBtldr_ProgressUpdate is also defined in C as:

typedef void CyBtldr_ProgressUpdate(uint8_t arrayId, uint16_t rowNum);

And I'm getting the following undefined reference error. Am I missing something?

.\bootloader.cpp:88: error: undefined reference to 'CyBtldr_RunAction(CyBtldr_Action, char const*, unsigned char const*, unsigned char, CyBtldr_CommunicationsData*, void (*)(unsigned char, unsigned short))'
kjgregory
  • 656
  • 2
  • 12
  • 23
  • 2
    I have no idea how we can help you with so little information. – R Sahu Aug 05 '17 at 05:04
  • At minimum, you'd need to show where you think this function is defined, where it is invoked, and the command line you're using to link the two sources together. You should also define whether you're working in C or C++. Probably the best we can do to help you is point you at [Undefined references](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). (I'd have closed it as a duplicate if I'd not voted so quickly that it is unclear what you're asking about). – Jonathan Leffler Aug 05 '17 at 05:07
  • Well I didn't want to post my entire project. It would be helpful to tell me what might cause this error? The two I know of is if my call doesn't match the prototype or if it can't find the function, but I don't think either are the culprit (could be wrong). – kjgregory Aug 05 '17 at 05:08
  • It's written in c and I'm calling it from c++. Am I allowed to do that? – kjgregory Aug 05 '17 at 05:10
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – sashoalm Aug 05 '17 at 05:50

2 Answers2

14

You need

extern "C"

because c++ function names are changed at compile time. This is what allows function and method overloading in c++.

You can use it to wrap the function declaration like this

extern "C" {
    int CyBtldr_RunAction(CyBtldr_Action action, const char* file, const uint8_t* securityKey, uint8_t appId, CyBtldr_CommunicationsData* comm, CyBtldr_ProgressUpdate* update);
}

Or to include the file where it's declared, like

extern "C" {
#include "cybtldr_runaction_header.h";
}

for an extended explanation and more, read this documentation.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • This sounds lije a likely winner. I'll give that a shot tomorrow and rewrite the question so that it's more useful to others. – kjgregory Aug 05 '17 at 05:20
0
int CyBtldr_RunAction(CyBtldr_Action action, const char* file, const uint8_t* securityKey,
                      uint8_t appId, CyBtldr_CommunicationsData* comm, CyBtldr_ProgressUpdate* update);

This is a declaration, not a definition. The declaration means you can call the function, but it still needs to be defined somewhere (in some compilation unit or library), and filaing to do so will give give you a link-time 'undefined reference' error, as you are seeing. So you need a definition for this function somewhere and you need to link it.

This

typedef void CyBtldr_ProgressUpdate(uint8_t arrayId, uint16_t rowNum);

is a type declaration, and has nothing to do with with CyBtldr_RunAction, other than being used as a type for one of its arguments.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226