Main function

Hello,

for one of my projects i need to manage my own main loop. i am trying to implement my own main function but i am getting a strange error.

core.a(main.cpp.o):In function `main'
collect2
:error: ld returned 1 exit status
Error creating .elf

i have a feeling its something to do with redefining main.

this code beneath compiles just fine so i know it is possible. Ok using the arduino ide it does not compile and the error i get is indeed "multiple definition of `main'" is it even possible? i hope it is.

#include <arduino.h>

void DoStuff()
{

}

int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif

	while (true)
	{
		DoStuff();
	}
}

Edit 2:
if i comment out the main function from C:\Arduino\hardware\arduino\avr\cores\arduino\main.cpp it compiles just fine its just i don't want to do that. is there any way of excluding that file from the compile run?

No, Arduino sketches with a 'main()' function work well with Atmega328 and Atmega2560 based boards and code like that (at least using 1.0.x versions of the Arduino software):

void DoStuff()
{
}

int main(void)
{
  init();
  while (true)
 {
   DoStuff();
 }
}

Which type of board do you want to use?

And what's wrong with using the main() function of the Arduino core library and just writing your own setup() and loop() functions?

jurs thanks for your reply, I am using the ardiono Uno "ardino IDE 1.6".

your example does compile thanks man it fails when i call initVariant(); i wonder if i need it.

Edit: the problem is i am forced to rewrite part of my state machine if i don't manage my own main loop. just stupid. your example does compile now i need to get mine to do it. just don't know why it does not work yet.

@jurs - Your wrong!! So worng.

The Arduino IDE provides main() - you can't END IT. If you want your own main then you need to ues another IDE. YOu can't have multiple main()'s in any C/C++ program.

Mark

You can write your own main.

The one supplied by the IDE is "lightly" bound so it can be replaced.

Example here: Gammon Forum : Electronics : Microprocessors : How to avoid the quirks of the IDE sketch file pre-preprocessing

Of course you cannot have two mains, so if you write your own you should probably call init() inside it.

The problem is that initVariant is in the same module as main. Calling initVariant causes the linker to include all of main.cpp. The Arduino folks should have put initVariant in its own module.

I don't think there is anything that you can do in "your own" main( ), that you cannot do in loop( ).

I think that the whole premise of your "problem" is misguided.

Hi,

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
int main(void);
}

main(void)
{
 // do stuff
}

I don't know if this makes sense, but put main inside loop, it compiles for UNO.
Tom...... :slight_smile:

Why?

TomGeorge:
Hi,

void setup() {

// put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
int main(void);
}

main(void)
{
// do stuff
}




I don't know if this makes sense, but put main inside loop, it compiles for UNO.
Tom...... :)

All you've done there is provide another function prototype.

is it even possible? i hope it is.

Why is it important to you?

It is important because of a couple of things. First i am porting my state machine and well id does all the main loop stuff, i don't feel like rewriting it because arduino people decide to make their own main function.

as far as i am aware any other development platform lets you manage your main loop, although i am mostly a pc programmer.

and the third and most important is that i just find the way arduino does stuff a little annoying. so no i just commented out the main function. did not want to do that but i see no other way. i was thinking of setting a #ifdef clause but i dont see a way to have it work when its defined in the users project

Edit:
my main.cpp(arduino code) now looks like this.

//define USERMAIN in your project if you want to use your own main.
#if !defined(USERMAIN)
int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}
#endif

problem solved!

You are making something much more complicated than it needs to be.

Just treat "setup" as "main" and leave "loop" empty. eg.

void setup ()
  {
  // your code goes here (the stuff that usually goes into main)
  }

void loop () {  }  // does nothing

as far as i am aware any other development platform lets you manage your main loop, although i am mostly a pc programmer.
...
i was thinking of setting a #ifdef clause but i dont see a way to have it work when its defined in the users project

As I posted before you can make your own main function which takes precedence over the existing one. I still don't know why you can't do either of the above methods.

If you make your own main you basically avoid init() being called. As I said, if that is important to you, you can do that.

You are making something much more complicated than it needs to be.

Just treat "setup" as "main" and leave "loop" empty. eg.

Yes you are right but it does look messy does it not ;}

as for

make your own main function which takes precedence over the existing one

i kept getting the you have 2 main functions defined error.

And this doesn't?

//define USERMAIN in your project if you want to use your own main.
#if !defined(USERMAIN)
int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}
#endif

Please post your code that does this, don't just claim it.

Which IDE are you using? So I can test your assertion.

My development environment is.

Visual studio using the visual micro plugin..

but i tested the flowing code using both.

#include <arduino.h>

void DoStuff()
{

}

int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif

 while (true)
 {
 DoStuff();
 }
}

and it does not work. when you(previous posts) say it should. now i added the #if !defined(USERMAIN) to main.cpp in the arduino code.

and now i can just define it and it will ignore the arduino code

Telling about that in reply #16?

OH BOY!

If somebody sends you example code, run it in the Arduino IDE, if not otherwise specified!

There is absolutely no problem creating your own main() function while using the Arduino IDE.

jurs:
Telling about that in reply #16?

OH BOY!

If somebody sends you example code, run it in the Arduino IDE, if not otherwise specified!

There is absolutely no problem creating your own main() function while using the Arduino IDE.

Using VisualStudio and VisualMicro uses the entire Arduino build system verbatim. I have been using it for years, and have never once seen ANY difference in operation between this configuration and the Arduino IDE. Code compiles and runs exactly the same in both.

Regards,
Ray L.

Please put your code in code tags.

Why are you calling initVariant?

This is what it does:

void initVariant() { }

Nothing. If you omit that call, as suggested on page 1, the code compiles.

Is this for a Leonardo or similar? If not why are you calling USBDevice.attach();?

This compiles:

#include <arduino.h>

void DoStuff()
{

}

int main(void)
{
 init();
 while (true)
   {
      DoStuff();
   }
}

Although you've just renamed "loop" as "DoStuff" so I don't really see what you've achieved, but whatever.

and it does not work. when you(previous posts) say it should.

Well, it used to in earlier versions of the IDE. They must have added the empty function initVariant() for some reason.

You don't seem to have a very clear direction here. We've been on this for two days, where you aren't clear about why you "must" use main. You ignored the suggestion about omitting initVariant, a function you don't seem to realize does nothing. Your end result looks just like the ordinary IDE code except you have renamed loop as DoStuff. I can only hope the rest of your project is moving faster than this part.