C to C++ Porting Issue

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

I've been porting some AVR C code to C++, which means compiling some of the underlying libraries with avr-g++ for the first time. In one such library, which is based on the "ioport" library from the AVR Software Framework, I'm getting an error only when compiled as C++ code.

This is the line:

ioport_configure_port_pin(ioport_pin_to_port(pin), ioport_pin_to_mask(pin), flags);

This is the error:

undefined reference to `ioport_configure_port_pin(void*, unsigned char, unsigned int)'

...and these are the related declarations:

extern void ioport_configure_port_pin(void *port, pin_mask_t pin_mask, port_pin_flags_t flags);

static inline PORT_t *ioport_pin_to_port(port_pin_t pin)
{
	// Each port has an offset of 0x20
	return (PORT_t *)((uintptr_t)&PORTA + (pin >> 3) * 0x20);
}

static inline pin_mask_t ioport_pin_to_mask(port_pin_t pin)
{
	return 1U << (pin & 0x7);
}

typedef uint8_t pin_mask_t;
typedef uint8_t pin_mask_t;

I have heard that C++ has stricter typing rules than C, which I'm guessing is at the root of this error. However, I don't know enough to figure out what is going on. Could any of you enlighten me?

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

Undefined reference means the thing that is calling it cannot find it at the link phase. There's a strong chance this is a C->C++ boundary problem. Do you have any .c files left in the project and is one of them calling ioport_configure_port_pin() ? If so it's linkage must be declared

extern "C"

otherwise the link target will not be called ioport_configure_port_pin but something like Z25ioport_configure_port_pinPvhj.

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

Thanks for the response - your post caused me to start digging into materials on mixing C and C++, and I realized that even though my project is set as a C++ project it was compiling the .c files in C instead. Although that makes sense it's not what I had intended. I changed the library files to compile in C++ and things are going smoothly again.

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

Why not just rename all .c to .cpp? Assuming they pass C++ stricter type checking they should just compile as C is a subset of C++.

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

> as C is a subset of C++.

Only at a first glance. At a second glance, there are many differences.

But yes, a reasonably well written standards compliant C program is normally
not that much away from a valid C++ program, too.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

The best workaround when using lamely coded C headers is to #include them like this:

extern "C"
{
    #include 
    #include "lame2.h"
}

That way you don't have to modify the actual header files, and you don't have to recompile library code.

It also allows the library code to still be used from C files.

Sid

Life... is a state of mind