Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arduino compiling as C++, not C

I'm trying to use a Supermodified Servo on an Arduino.

I don't have any actual code in my sketch yet, I'm just trying to compile the library. I get a ton of errors .

After doing some research, I figured out that if I were to compile my code as C as opposed to C++, I wouldn't be getting these errors. But I have no idea how to do that!

I'm using Arduino 1.0 for Linux x64.

@Omnifarious I only have the #include directive in my arduino sketch, which at this time is sketch_jan20.cpp

I added

#ifd __cplusplus{
extern "C"
#endif

That helped get rid of the errors below, except for one of them: the redefinition of the boolean. When I commented that line out, I got a whole new mess of errors. All of them were the same, consisting of some variation of:

C:\Users\Ventrius\Programming\Arduino1.01\libraries\ArduinoApiC\/zoCommands.h:131: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'zoCommandDoMove'

Errors:

avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=100 -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/hardware/arduino/variants/standard -I/usr/share/arduino/libraries/ArduinoApiC /tmp/build3069866408398264713.tmp/sketch_jan20a.cpp -o/tmp/build3069866408398264713.tmp/sketch_jan20a.cpp.o 
In file included from /usr/share/arduino/libraries/ArduinoApiC/zoSmsMaster.h:4:0,
                 from sketch_jan20a.cpp:1:
/usr/share/arduino/libraries/ArduinoApiC/zoTypes.h:39:18: error: redeclaration of C++ built-in type ‘bool’
In file included from /usr/share/arduino/libraries/ArduinoApiC/zoSmsMaster.h:5:0,
                 from sketch_jan20a.cpp:1:
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:19:29: error: non-local function ‘void zoErrorInit(volatile ZO_ERROR*)’ uses anonymous type
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:15:2: error: ‘typedef volatile struct<anonymous> ZO_ERROR’ does not refer to the unqualified type, so it is not used for linkage
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:20:27: error: non-local function ‘u08 zoErrorGet(volatile ZO_ERROR*)’ uses anonymous type
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:15:2: error: ‘typedef volatile struct<anonymous> ZO_ERROR’ does not refer to the unqualified type, so it is not used for linkage
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:21:44: error: non-local function ‘void zoErrorPut(volatile ZO_ERROR*, u08)’ uses anonymous type
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:15:2: error: ‘typedef volatile struct<anonymous> ZO_ERROR’ does not refer to the unqualified type, so it is not used for linkage
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:22:32: error: non-local function ‘bool zoErrorIsEmpty(volatile ZO_ERROR*)’ uses anonymous type
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:15:2: error: ‘typedef volatile struct<anonymous> ZO_ERROR’ does not refer to the unqualified type, so it is not used for linkage
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:24:30: error: non-local function ‘u08 zoErrorGetIsr(volatile ZO_ERROR*)’ uses anonymous type
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:15:2: error: ‘typedef volatile struct<anonymous> ZO_ERROR’ does not refer to the unqualified type, so it is not used for linkage
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:25:47: error: non-local function ‘void zoErrorPutIsr(volatile ZO_ERROR*, u08)’ uses anonymous type
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:15:2: error: ‘typedef volatile struct<anonymous> ZO_ERROR’ does not refer to the unqualified type, so it is not used for linkage
In file included from /usr/share/arduino/libraries/ArduinoApiC/zoSmsMaster.h:6:0,
                 from sketch_jan20a.cpp:1:
/usr/share/arduino/libraries/ArduinoApiC/zoProtocol.h:93:26: error: non-local function ‘bool zoProtocolCommandResponse(ZO_PROTOCOL_HAL*, ZO_PROTOCOL_PACKET*, volatile ZO_ERROR*)’ uses anonymous type
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:15:2: error: ‘typedef volatile struct<anonymous> ZO_ERROR’ does not refer to the unqualified type, so it is not used for linkage
In file included from sketch_jan20a.cpp:1:0:
/usr/share/arduino/libraries/ArduinoApiC/zoSmsMaster.h:55:18: warning: non-local variable ‘volatile ZO_ERROR* zoSmsMasterError’ uses anonymous type
/usr/share/arduino/libraries/ArduinoApiC/zoError.h:15:2: warning: ‘typedef volatile struct<anonymous> ZO_ERROR’ does not refer to the unqualified type, so it is not used for linkage
like image 838
Windsplunts Avatar asked Dec 15 '25 19:12

Windsplunts


1 Answers

This answer is not very useful, and is just my attempt to deal with the confusing nature of the question.

Rename your files to .c and use avr-gcc to compile them, not avr-g++.

If you get errors, that means you need to adjust your program, and possibly even re-write it to not use any C++ features.

There is no general formula for translating a C++ program to C.

Another option is to create a C layer of your own for your C++ code to call. This layer of C code will have C++-friendly function definitions (i.e. no bool and the like) and it will then call the non-C++-friendly code out of the Arduino libraries.

You can then create header files containing the function definitions for your C layer that are wrapped in extern "C" { ... }. Make sure those header files do not include any of the header files for the Arduino libraries you're using.

like image 100
Omnifarious Avatar answered Dec 17 '25 11:12

Omnifarious



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!