AS 7 - cpp and c library modules

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Hi. I have Peter Fluery's UART library modules (uart.c and uart.h).  I can get this to work on a main.c program.

However, changing main.c to main.cpp (it's a simple main), results with compile error for the main.cpp.

------------------------

Error        undefined reference to `uart_init(unsigned int)'    Test_UART    C:\u\AS_70\02_PeterF_Library\Test_UART - Copy\Test_UART\main.cpp    51

------------------------

Likewise this error is repeated for all the other routines used in the library.

The Symbols and Directories are set the same for the C and C++ compilers.

 

 

The library is referenced as a link file (refer above).

 

Is there any basis problem(s) ?? Are there other steps for a C library to be use as a C++ library ??

 

Regards JC......

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jmclifford@aanet.com.au wrote:
Are there other steps for a C library to be use as a C++ library ?

The ways to use 'C' code in a C++ project are well-documented, and not specific to Atmel Studio or even GCC

 

Try some googling something like, "mix C C++" ...

 

eg,  https://isocpp.org/wiki/faq/mixing-c-and-cpp

 

 

EDIT

 

That specific error will be because C++ expects the names to be "mangled" - which 'C' doesn't do.

 

https://en.wikipedia.org/wiki/Name_mangling#:~:text=In%20compiler%20construction%2C%20name%20mangling,in%20many%20modern%20programming%20languages.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Aug 1, 2020 - 10:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jmclifford@aanet.com.au wrote:

The library is referenced as a link file (refer above).

Do you mean you've made a fluery_uart.a and you're linking against that ?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

N.Winterbottom wrote:
Do you mean you've made a fluery_uart.a and you're linking against that ?

presumably not - as there's a uart.c source file in the project ?

 

I assumed he meant the source file was added to the project as a link:

 

 

EDIT

 

But that should make no difference to the C / C++ interfacing issue ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Aug 1, 2020 - 12:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The .cpp calling code must "know" the interface to the library functions so you have something like:

void uart_init(int speed);
void uart_sendchar(char c);

int main(void) {
    uart_init(1123);
    uart_sendchar('A');
}

but the declarations need to be:

extern "C" {
void uart_init(int speed); 
void uart_sendchar(char c); 
}

int main(void) {
 uart_init(1123);
 uart_sendchar('A'); 
}

To say " this C++ is calling across to C"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.  Thank you.

My current situation is that with only doing;

 

extern "C" { uart.h }

 

I got the uart working (no compiler errors).

 

My current issue is that the program hangs with CAN0.begin().

Refer code below;

---------------------------------

   uart_puts("String stored in SRAM\n");
   // Initialize the CAN controller
   // Baud rates define in mcp_can_dfs.h
   if (CAN0.begin(CAN_500KBPS) == CAN_OK)      // 500KBPS instead of 250KBPS
      uart_puts("CAN INIT OK for MonitorAndDiagnostics. \n\r\n\r");
   else
      uart_puts("CAN INIT Failed.\n\r");

------------------------------------

The string "String stored in SRAM" is sent but neither any one of the others.

 

This CAN library is a C++ source that is linked to like the uart.c.

 

Regards JC.......

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which suggests it is "stuck inside" .begin()

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Either use a debugger, or follow the execution path through the library source and liberally sprinkle serial debug output.

 

The .begin() method calls SPI.begin() which is an Arduino class/object. Is SPI setup correctly ? Is the SPI slave-select pin set correctly in the CAN object constructor ?

 

It also calls many internal library methods that twiddle various register bits in the MCP2515.

Last Edited: Sat. Aug 1, 2020 - 06:35 PM