0

Consider:

Enter image description here

I having a problem in compiling in extern "C" code in the Arduino IDE 1.6.7 which I don't have on version 1.6.5.

The error is:

error: conflicts with new declaration with 'C' linkage

It is complaining about the lines containing loop() and start_timer(void);.

This is my code:

#ifdef __cplusplus
extern "C" {
#endif
void start_timer(void);
#ifdef __cplusplus
}
#endif
void start_timer(void)
{
 Serial.println("2");
 Serial.begin(9600);
 delay(2000);
}
void setup()
{
 start_timer();
 Serial.println("4");
}
void loop()
{
 Serial.println("1");
 delay(2000);
}
asked Jan 8, 2016 at 6:40
4
  • Please edit the code and provide the error/warning from the compiler. Commented Jan 8, 2016 at 9:30
  • I can't reproduce that. Please post your full code as Mikael Patel suggested, and the error message. That is: a Minimal, Complete, and Verifiable example Commented Jan 8, 2016 at 11:11
  • I already updated my previous post Commented Jan 8, 2016 at 11:55
  • Why do you need to declare C linkage for a symbol directly in your script? Commented Jan 8, 2016 at 20:53

2 Answers 2

4

I can't reproduce your problem still. See this:

Compiler output


However if you are having problems (and frankly I expected you to) then you should read this:

How to avoid the quirks of the IDE sketch file pre-preprocessing

How the IDE organizes things


I can reproduce your problem in IDE 1.6.5 (not 1.6.7). The code generated by the IDE preprocessor is this:

#line 1 "sketch_jan11a.ino"
#ifdef __cplusplus
#include "Arduino.h"
void start_timer(void);
void setup();
void loop();
#line 2
extern "C" {
#endif
void start_timer(void);
#ifdef __cplusplus
}
#endif
void start_timer(void)
{
 Serial.println("2");
 Serial.begin(9600);
 delay(2000);
}
void setup()
{
 start_timer();
 Serial.println("4");
}
void loop()
{
 Serial.println("1");
 delay(2000);
}

You can see that in the automatic function prototype generation that the IDE "helpfully" generates for you, it puts the prototype for start_timer outside the extern "C" declaration. Thus one function prototype has a declaration in C++ format, and the other in C format.

Why are you even doing this? I presume this is a small example of a larger problem.

If you follow the suggestions in my link How to avoid the quirks of the IDE sketch file pre-preprocessing - did you read that? - the problem goes away, because .cpp files in IDE tabs are not subjected to this extra processing.

answered Jan 9, 2016 at 8:12
3
  • Please refer to the about image that i just attached for the issue that i'm facing. If i put the whole "start_timer()" function into the extern "C" then i will be working fine. Commented Jan 11, 2016 at 4:03
  • Which operating system are you using? Commented Jan 11, 2016 at 8:28
  • See amended reply for more info. Commented Jan 11, 2016 at 8:36
1

The compiler is telling you that there is a conflict between the two definitions of start_timer().

The normal usage of extern "C" is in a header file for functions that are accessed from both C and C++. This actually has to do with how C++ handles names and overloading. Using extern "C" will use normal C name handling and C++ name mangling is turned off (together with overloading, etc).

Please see https://stackoverflow.com/questions/31349865/compiler-error-for-conflicting-variable-declarations-conflicts-with-new-declar.

answered Jan 8, 2016 at 23:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.