Power & Source of Big Ideas

[SOLVED] Using the Matrix C libraries in C++ code

Moderators: chensy, FATechsupport

    NanoPi Neo Air

I can run the demos and compile them in C but would like to utelise SPI and GPI from the main libarry in c++
When i compile my code using c++ linking to the "fahw" libarry I get this error:

code is here https://github.com/leonyuhanov/NanoPi_APA102/blob/master/spitest.c

administrator@FriendlyARM:~/spitest$ g++ -L/usr/local/lib spitest.c -o spitest -lfahw -I ../lib/includes/
spitest.c:(.text+0x1bc): undefined reference to `boardInit()'
spitest.c:(.text+0x1ca): undefined reference to `openHW(char const*, int)'
spitest.c:(.text+0x1f2): undefined reference to `setSPIDataMode(int, int)'
spitest.c:(.text+0x204): undefined reference to `setSPIWriteBitsPerWord(int, int)'
spitest.c:(.text+0x220): undefined reference to `setSPIMaxSpeed(int, unsigned int)'
collect2: error: ld returned 1 exit status


Above code compiles and runs perfectly with gcc
For example, taking boardInit(), which Matrix (included) file is that in? Then where is setSPIDataMode(int, int)?
boardInit() is located in https://github.com/friendlyarm/matrix/b ... b/common.h
All the SPI functions are in https://github.com/friendlyarm/matrix/b ... /lib/spi.c

The entire lib folder is located in the same folder as my test code above. This compiles perfectly using gcc but fails in g++
OK, I have an older Matrix C library before SPI was added.

Back to your problem .. does the linker behave differently between gcc and g++ ?

Maybe try leaving off the

Code: Select all

-I ../lib/includes/ 
and see if you get more errors.
Yeah if i get rid of the -I i get tins more errors, so the includes are located properly
I have got no C++ experience, only embedded C.

Maybe try including a library (say 1wire) you don't use and call one its functions in main and see if you get any errors.

Are there any .c files that are included and result in no errors?
I had a read through this one ... just maybe

https://isocpp.org/wiki/faq/mixing-c-and-cpp
Thanks dave ill have a lool tjrough that link, i think its def somethinf to do with the way the linker is working
davef wrote:
I had a read through this one ... just maybe
https://isocpp.org/wiki/faq/mixing-c-and-cpp

Dave thanks a million mate, this did the trick
I modified my inclusion of

#include "libfahw.h"


to

extern "C" {
// Get declaration for f(int i, char c, float x)
#include "libfahw.h"
}


And compile using

g++ -L/usr/local/lib spitest.c -o spitest -lfahw -I ../lib/includes/


no errors!! Perfect! Thankyou so much!
You are welcome, now we both have learned something new.

I noticed the comment about versions of g++ and gcc. If you do get more errors and/or things don't run properly I was told once that "the version of gcc/g++ used to compile the toolchain, libraries, kernel and your filesystem should be the same". This would seem difficult to achieve using two different cross-compilers. Hopefully, the version of gcc used to cross-compile the toolchain, (C) libraries and kernel will work when the filesystem and your application have been cross-compiled with g++.

Good luck!
Thanks mate. If your interested i have fixed up the code and uploaded full instructions to my github
https://github.com/leonyuhanov/NanoPi_APA102
This will drive any APA102/SK9822 pixels, its basically a very advanced "blink" for driving these kid of pixels!

Who is online

In total there are 29 users online :: 0 registered, 0 hidden and 29 guests (based on users active over the past 5 minutes)
Most users ever online was 2865 on Sun Nov 10, 2019 5:27 am

Users browsing this forum: No registered users and 29 guests