Page 1 of 1
[SOLVED] Using the Matrix C libraries in C++ code
Posted: Mon Apr 24, 2017 12:53 pm
by elecdashtron
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.cadministrator@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
Re: Using the Matrix C libraries in C++ code
Posted: Mon Apr 24, 2017 7:19 pm
by davef
For example, taking boardInit(), which Matrix (included) file is that in? Then where is setSPIDataMode(int, int)?
Re: Using the Matrix C libraries in C++ code
Posted: Mon Apr 24, 2017 11:45 pm
by elecdashtron
boardInit() is located in
https://github.com/friendlyarm/matrix/b ... b/common.hAll the SPI functions are in
https://github.com/friendlyarm/matrix/b ... /lib/spi.cThe entire lib folder is located in the same folder as my test code above. This compiles perfectly using gcc but fails in g++
Re: Using the Matrix C libraries in C++ code
Posted: Tue Apr 25, 2017 12:10 am
by davef
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
and see if you get more errors.
Re: Using the Matrix C libraries in C++ code
Posted: Tue Apr 25, 2017 12:28 am
by elecdashtron
Yeah if i get rid of the -I i get tins more errors, so the includes are located properly
Re: Using the Matrix C libraries in C++ code
Posted: Tue Apr 25, 2017 12:43 am
by davef
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?
Re: Using the Matrix C libraries in C++ code
Posted: Tue Apr 25, 2017 1:01 am
by davef
I had a read through this one ... just maybe
https://isocpp.org/wiki/faq/mixing-c-and-cpp
Re: Using the Matrix C libraries in C++ code
Posted: Tue Apr 25, 2017 1:09 am
by elecdashtron
Thanks dave ill have a lool tjrough that link, i think its def somethinf to do with the way the linker is working
Re: Using the Matrix C libraries in C++ code
Posted: Tue Apr 25, 2017 2:27 am
by elecdashtron
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!
Re: Using the Matrix C libraries in C++ code
Posted: Tue Apr 25, 2017 2:48 am
by davef
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!
Re: Using the Matrix C libraries in C++ code
Posted: Tue Apr 25, 2017 2:55 am
by elecdashtron
Thanks mate. If your interested i have fixed up the code and uploaded full instructions to my github
https://github.com/leonyuhanov/NanoPi_APA102This will drive any APA102/SK9822 pixels, its basically a very advanced "blink" for driving these kid of pixels!