Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing C and C++... undefined reference to function [closed]

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))'
like image 298
kjgregory Avatar asked Sep 19 '25 09:09

kjgregory


1 Answers

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.

like image 162
Iharob Al Asimi Avatar answered Sep 20 '25 23:09

Iharob Al Asimi