can .ino be C code?

Post here first, or if you can't find a relevant section!
Post Reply
dudmuck
Posts: 3
Joined: Tue May 25, 2021 10:31 pm

can .ino be C code?

Post by dudmuck »

I was trying to build some already existing C code with stm32duino.
It ran into issue because the C code uses C99 struct initializers, which arent in the C++ specification.
The situation is described https://stackoverflow.com/questions/187 ... sts-as-c99

I wonder if the correct solution is to move these C struct initializers into a separate C file.
Would stm32duino build a .c file using gcc instead of g++?
AndrewBCN
Posts: 84
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 2
Location: Strasbourg, France

Re: can .ino be C code?

Post by AndrewBCN »

From: https://arduino.stackexchange.com/quest ... rduino-ide
Compiler flags are defined in the platform.txt of the board you have selected in the Tools > Board menu. You can also add compiler flags via a build.extra_flags property in the boards.txt entry for the selected board.
If I understand your question, basically what you want is to specify some compiler flags for gcc, namely the -std=c99 flag (see: https://gcc.gnu.org/onlinedocs/gcc/C-Di ... tions.html ). The way to do that is explained in the quote above.

Note that choosing and configuring the "best" compiler flags rather than the default ones is a rather complex matter, that's why most developers using the Arduino IDE never worry about this.
Last edited by AndrewBCN on Thu Jun 03, 2021 10:47 am, edited 1 time in total.
dannyf
Posts: 74
Joined: Sat Jul 04, 2020 7:46 pm

Re: can .ino be C code?

Post by dannyf »

I can report that you can indeed write C code in .ino - I have done it plenty of times.

obviously the flavor of C code needs to be supported by the corresponding compiler.
dudmuck
Posts: 3
Joined: Tue May 25, 2021 10:31 pm

Re: can .ino be C code?

Post by dudmuck »

AndrewBCN wrote:
Wed Jun 02, 2021 8:53 pm
From: https://arduino.stackexchange.com/quest ... rduino-ide
Compiler flags are defined in the platform.txt of the board you have selected in the Tools > Board menu. You can also add compiler flags via a build.extra_flags property in the boards.txt entry for the selected board.
If I understand your question, basically what you want is to specify some compiler flags for gcc, namely the -std=c99 flag (see: https://gcc.gnu.org/onlinedocs/gcc/C-Di ... tions.html ). The way to do that is explained in the quote above.

Note that choosing and configuring the "best" compiler flags rather than the default ones is a rather complex matter, that's why most developers using the Arduino IDE never worry about this.
thanks!
I look in platform.txt
I can see compiler.cpp.flags and compiler.c.flags
since there are separate C flags, i can just put my C code in a .c file, and it will use compier.c.flags
I think editing platform.txt or boards.txt would be difficult for users; arduino should be easy.

The problem with using g++ for C code is when there are more complex struct initialization with .member = foo notation, it barfs and gcc needs to be used instead of g++.
User avatar
fpiSTM
Posts: 1074
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 50
Location: Le Mans
Contact:

Re: can .ino be C code?

Post by fpiSTM »

Yes simply add a .c file. Ino is concerted to cpp. You van also add .h file or . Cpp or .hpp.
AndrewBCN
Posts: 84
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 2
Location: Strasbourg, France

Re: can .ino be C code?

Post by AndrewBCN »

So, in summary, to answer your original question:
A .ino (Arduino sketch) file is always converted to cpp and it will be compiled with g++ using the c++ flags, BUT
you can always add a .c file (and a .h file, etc) to your sketch folder (in the Arduino IDE, menu Sketch->Add File...), and it will be compiled with gcc using the c flags specified in platform.txt and linked into the final executable.
mrburnette
Posts: 436
Joined: Thu Dec 19, 2019 1:23 am
Answers: 1

Re: can .ino be C code?

Post by mrburnette »

dudmuck wrote:
Thu Jun 03, 2021 5:42 pm
...
I think editing platform.txt or boards.txt would be difficult for users; arduino should be easy.
...
The reason that "difficult" is even surfacing is that you are going outside the Arduino IDE standard paradigm with your specific need. That "it" can be done at all is a testament to Arduino's flexibility. Adding compiler switch configuration options inside the IDE would complicate ArduinoIDE for the vast majority of users. Imagine the support dialog in the forum it every programming failure needed an analysis of compiler switches in addition to code logic & syntax.

IMO, you really should go outside the IDE and use the CLI interface which is well documented.
https://arduino.github.io/arduino-cli/l ... duino-cli/


Ray
dannyf
Posts: 74
Joined: Sat Jul 04, 2020 7:46 pm

Re: can .ino be C code?

Post by dannyf »

arduino should be easy.
the reason arduino is made easy is in part to make it difficult for further customization. you are paying for that ease of use.
ag123
Posts: 810
Joined: Thu Dec 19, 2019 5:30 am
Answers: 4

Re: can .ino be C code?

Post by ag123 »

i think a reason these days more things are developed in c++ rather than C, is that from c++ 11 and above many better features has been developed.
things like c++ references
https://isocpp.org/wiki/faq/references
https://en.wikipedia.org/wiki/Reference_(C%2B%2B)
they avoid straight out use of pointers, they are particularly useful in memory constrained setups and does not need a pointer to pass an object.
it is type checked as well. C don't have all these features.
mrburnette
Posts: 436
Joined: Thu Dec 19, 2019 1:23 am
Answers: 1

Re: can .ino be C code?

Post by mrburnette »

... for another perspective read here

If you want to know C++, IMO one really needs to familiarize themselves with the STL, but using it is completely optional.

Andy Brown has a (somewhat dated) download and condensed overview here.
Post Reply

Return to “General discussion”