Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Using the NeoPixel library with a C project
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Using the NeoPixel library with a C project

by bob_mosh on Wed Mar 18, 2020 8:09 am

Hey lovely people,

I've been experimenting with the Adafruit NeoPixel library https://github.com/adafruit/Adafruit_NeoPixel for the ESP8266 and it seems to work like a charm in an example sketch.

Whenever I try to use it in my project however (containing .c files) I can't compile anymore because it can't handle the "class" implementation.
Is there any way to make it usable for my project? I am using a library that uses .c files so there unfortunately is no way for me to move to cpp easily.

This is one of the many errors I'm getting:
Code: Select all | TOGGLE FULL SIZE
/Arduino/libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.h:199:1: error: unknown type name 'class'
 class Adafruit_NeoPixel {


Can someone help me with this? I'm going bananas here.

Cheers,
bob_mosh

bob_mosh
 
Posts: 3
Joined: Wed Mar 18, 2020 7:59 am

Re: Using the NeoPixel library with a C project

by dastels on Wed Mar 18, 2020 10:30 am

The NeoPixel library is written (as you know) in C++. A straight C compiler can't parse the class definitions. Can you change what C compiler is being used? Force it to use a C++ compiler? That will usually work. And what toolchain are you using?

Dave

dastels
 
Posts: 3753
Joined: Tue Oct 20, 2015 3:22 pm

Re: Using the NeoPixel library with a C project

by bob_mosh on Fri Mar 20, 2020 1:53 am

Hey Dave,

thanks for the reply!
I believe the IDE decides wich compiler to use on its own. :/
It should be possible to change it but it seems like I'm going to get a whole new bunch of errors this way.

I have however just come up with a different solution. I wanted to use the Library in my "accessory.c" file (the C file) but since I was not able to do just that, I found a way to create a function inside my .ino file and create a callback from the c file, exposing the specific function using the extern "C" feature. I don't fully understand yet what it does, but it seems to force the cpp compiler to use c compiling for the specified parts and therefore the C code can access it and use it because the CPP compiler switches the function mangling back to the old c version.

It's a bit of a hack but since I'm not working with production code I'm willing to take that as a successful solution. :)

It's especially useful since I was not able to turn my c file into a cpp file because the compiler wouldn't take it.
No idea why since the entire library I'm using is also written in cpp but anyways. Still a bit confused about mixing c and cpp.

To answer your question, I have no idea which toolchain I am using.
How can I find out? Googling didn't help much unfortunately. :(

Sorry, I'm usually more used to the Swift, C# and Java environments.
Going down to C and CPP is fairly new to me.

Cheers,
bob_mosh

bob_mosh
 
Posts: 3
Joined: Wed Mar 18, 2020 7:59 am

Re: Using the NeoPixel library with a C project

by dastels on Fri Mar 20, 2020 9:12 am

Can you rename the .c file to be a .cpp file? That's often what tools use to decide how to process the file.

Dave

dastels
 
Posts: 3753
Joined: Tue Oct 20, 2015 3:22 pm

Re: Using the NeoPixel library with a C project

by bob_mosh on Fri Mar 20, 2020 10:23 am

Hey Dave,

I had that same idea. :)
Unfortunately it won't compile as a .cpp.

But with my lack of c and cpp knowledge I couldn't figure out how to solve all the issues.

Cheers,
bob_mosh

bob_mosh
 
Posts: 3
Joined: Wed Mar 18, 2020 7:59 am

Please be positive and constructive with your questions and comments.