Organgutan and cpp classes

Hi All,

This is my first post.

I have been working with the Organgutan SV-328 and I’m trying to advance my project to use c++ and create some reusable classes. So here is what I have done and where I’m stuck.

I upgraded by development environment with the latest WINAVR 2010 01-10. I also updated the AVR Studio. With some minor manipulation, I got all my current code to compile, link and download to the AVR (all standard C).

This has enabled me to now create .cpp files within the Studio. I then created a cpp file with a couple of simple classes and attempt a build. Compile no problem of course. The link fails with undefined reference to any of the functions in the atmega328p.a lib. I’m guessing names are mangled. Sorry for the long intro. is there a c++ version of the atmega328p.a Lib that I could link in? Or is the problem more fundamental than this?

Sorry in advance if I somehow have missed the big picture or missed something in the docs.

Any assistance greatly appreciated.

Thanks,

Hello.

I’m not familiar with using C++ in AVR studio, but the error you describe also happens when people do not include the library in the AVR studio library options dialog. This section of the user’s guide explains how to do it. Are you already doing that?

-Ryan

Ryan is right - it sounds like you are just not linking in the library. The library files work for both C and C++, and in fact the library code itself is all done in C++ with a simple wrapper to make functions accessible from C.

If you continue to have trouble, please cut and paste the entire output from AVR Studio compilation window into a post so we can see what is wrong. Without that, we can only guess.

-Paul

Thanks for the quick responses, looks like the problem is more fundamental. Looks like installing the new version of the AVR wasn’t a good idea.

Ok, I have corrected the installation problems caused by the new winavr install. I can now successfully build any/all of the example programs again, along with my original code.

I create a small c++ version of the LCD example.

I’m back to the c++ issue. Here is the result from compile link attempt. Notice the warning message regarding -std=gnu99 is valid for C but not c++. I tried removing that option and I get the same additional linker errors. Not sure what that means exactly, there are some vague references to this problem from the general internet (google) but nothing that seems to indicate what to do to fix it.

any continued help would be appreciated, here is the log

Build started 21.2.2010 at 19:09:58
avr-g++  -mmcu=atmega168 -Wall -gdwarf-2 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums  -std=gnu99  -MD -MP -MT main.o -MF dep/main.o.d  -c  ../main.cpp
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
avr-g++ -mmcu=atmega168 -Wl,-gc-sections -Wl,-Map=test.map main.o    -lpololu  -o test.elf
main.o: In function `main':
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:24: undefined reference to `print(char const*)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:25: undefined reference to `delay_ms(unsigned int)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:26: undefined reference to `lcd_scroll(unsigned char, unsigned char, unsigned int)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:27: undefined reference to `clear()'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:28: undefined reference to `lcd_goto_xy(int, int)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:29: undefined reference to `print(char const*)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:30: undefined reference to `delay_ms(unsigned int)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:31: undefined reference to `lcd_scroll(unsigned char, unsigned char, unsigned int)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:32: undefined reference to `clear()'
make: *** [test.elf] Error 1
Build failed with 9 errors and 1 warnings...

Here is the result with the -std=gnu99 option removed

Build started 21.2.2010 at 19:19:05
avr-g++  -mmcu=atmega168 -Wall -gdwarf-2 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums  -MD -MP -MT main.o -MF dep/main.o.d  -c  ../main.cpp
avr-g++ -mmcu=atmega168 -Wl,-gc-sections -Wl,-Map=test.map main.o    -lpololu  -o test.elf
main.o: In function `main':
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:24: undefined reference to `print(char const*)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:25: undefined reference to `delay_ms(unsigned int)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:26: undefined reference to `lcd_scroll(unsigned char, unsigned char, unsigned int)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:27: undefined reference to `clear()'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:28: undefined reference to `lcd_goto_xy(int, int)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:29: undefined reference to `print(char const*)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:30: undefined reference to `delay_ms(unsigned int)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:31: undefined reference to `lcd_scroll(unsigned char, unsigned char, unsigned int)'
C:\Users\0\Desktop\libpololu-avr\examples\Chet-Test-1\default/../main.cpp:32: undefined reference to `clear()'
make: *** [test.elf] Error 1
Build failed with 9 errors and 0 warnings...

This library option must be enabled for each project you make. Have you added the Pololu AVR library to your list of linked-in libraries like section 7 of the document I linked to before suggests?

- Ryan

Hi Ryan,

thanks for the response.

Yes on the libraries, I’m sorry I guess I’m not being clear. Everything builds until you make the source files .cpp, which forces the g++ to run instead of gcc within WINAVR. Thus the standard pololu libraries do not seem to have been built to link within c++ applications. The linker will not resolve the references. If I take the exact same program and compile it as a .c app then gcc runs and everything works. Thus the only difference is the c++ vs c.

I should have noticed this earlier, but it looks like you are using the C names for the functions instead of the C++ names. For example lcd_scroll is OrangutanLCD::scroll in C++.

Does that fix your problem?

- Ryan

Hello,

It looks like you are trying to call the C versions of the functions from C++. Our intention was that people who use C++ would use the C++ include files (mentioned in Ryan’s link) and C++ classes for everything, so that they could avoid having a polluted global namespace, use overloaded functions, instantiate objects, and so on. If you rewrite your code to use the C++ functions, I think it will work.

On the other hand, if you really want to use the C versions, you have to specify C linkage with a slight modification to your include statement:

extern "C"
{
#include <pololu/orangutan.h>
}

I just tested it - it seems to allow C++ code to compile using C functions. I did not test whether it actually works, but I expect that it will.

-Paul

In the link step, the command line option should be -lpololu_atmega328 because the name of the compiled library file is libpololu_atmega328.a. I’m surprised that avr-g++ didn’t complain about that, and I’m surprised that AVR Studio generated that command in the first place if the file doesn’t exist.

–David

Thanks everyone, I got it. I didn’t realize the c++ standard function calls were class referenced.

Thanks again !!!