Pages: [1]   Go Down
Author Topic: errors in a file that I didnt call or reference (main.cpp) HELP requested  (Read 401 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have been working on a project to control  an SI570 Direct digital Synthesizer.  WHen I compile it I get a strange error message that is (seemingly) unrelated to the program I jusst comiled.  Now the program was not written by me it was written and originally compiled about 2 years ago and maybe with another compiler. 

Here is the error given by the Arduino 0022 compiler:


core.a(main.cpp.o): In function "main":
c:\user\voyager\arduinarduino 0022\hardware\arduino\cores\arduino/main.cpp:7:undefined feference to "setup"

c:\user\voyager\arduinarduino 0022\hardware\arduino\cores\arduino/main.cpp:10:undefined feference to "loop"

The file "main.cpp"   is located in
arduino 0022\hardware\arduino\cores\arduino\main.cpp

Now the problem I have is that I did not call main in any of the files that I have (as far as I cal tell) so what gives with the error message... what did I do wrong  .. etc. 

I am totally at a loss as to where to look and the code for the si579 is sufficiently (for me) convoluded that I am having problems trying to figure it out.


Thanks in advance

Dillon
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...
Here is the error given by the Arduino 0022 compiler:


core.a(main.cpp.o): In function "main":
c:\user\voyager\arduinarduino 0022\hardware\arduino\cores\arduino/main.cpp:7:undefined feference [sic] to "setup"

All C++ programs must have a main() function (really), which is the entry point of the user code.  Period.  Full Stop.

When you compile your sketch from inside the Arduino IDE, it embeds your sketch into a .cpp file and links the Arduino core file main.cpp, which looks like this:
Code:
#include <WProgram.h>

int main(void)
{
init();

setup();
    
for (;;)
loop();
        
return 0;
}

It also links all of the other core libraries and any other libraries for which you have included library headers in your sketch.  Note that the function init() is defined in the core library file wiring.c, which is in the same directory as main.cpp.

It is a little known fact (deliberately concealed, I'm thinking) that you can create your own main() function (in your sketch) that does not call setup() and loop(), and that will subvert the linking of the Arduino core main() function, but why the heck would you do such a thing?  I mean, it's Arduino, for goodness sake!   Look at the supplied examples.  Look at the thousands of examples in the Arduino playground and in this forum.

Follow the rules of the Arduino Way.  Resistance is futile.  You will be assimilated.


Regards,

Dave

Footnote:
When I say stuff like "it links all..." you have to realize that it compiles the source files and recursively looks in the compiled object files for any functions used in your sketch.  Functions that you don't use do not become part of the executable.  Bottom line: The compiler does a little more work but it doesn't cost anything at run time to have all of these functions available if you don't use them.
« Last Edit: May 30, 2011, 07:04:04 pm by davekw7x » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually I think the guys that put the compiler together did a "HELLAVA" job with it.  I mean all of this functionality and its FREE?  Geeze.

Anyhow.. I sorta suspected it called the main.cpp on its own.. now my problem is to fix this..

Here is the thing, I am an old timer C programmer.. notice I didnt say C++,  I also program in assembler but not 32 bit.  AND I didnt write this program I was only hoping to use it as a learning tool because it doesnt do exactly what I wanted to do.

With reference to using the "main" in my program.. I always do this because it is the way I was trained but I previously noticed that the program I got from my friend didnt have one..

Anyhow .. thanks.. I will try to figure it out.

Dillon
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...
Anyhow.. I sorta suspected it called the main.cpp on its own..
In C++ (and in C), the thing that "calls" the main() function is some run-time code that sets up stuff like the stack pointer and performs certain other CPU bookkeeping tasks that are necessary before user code can be run.

Quote from: Dillon
now my problem is to fix this..
Fix what???

In a "normal" C or C++ embedded program not running under an operating system, nothing ever returns from the main program.  In a "normal" Arduino sketch, the sketch's loop() function is called repeatedly in an infinite loop from the core library main() function, and the sketch writer is not given the opportunity to return from main().

If you do make your own main() function in a sketch and you return from that main(), it is the same as executing the standard library exit() function.  (That's in the standard language specification document, and that's what happens with Arduino's avr-gcc and avr-libc.)

For the avr-gcc/avr-libc library that Arduino uses, the exit() function causes interrupts to be disabled and the program enters a tight do-nothing loop.  In Arduino, as in other embedded systems that run "barefoot," there is no operating system to return to.


Run-time initialization code and run-time library functions are part of the the avr-gcc library and are not part of the Arduino sketch or library.  This is always linked into the execution code.  Arduino-specific initialization code having to do with setting up timers for default operation is executed in the core library init() function, which is called first in the Arduino core library main() function.

Bottom line: Thanks to the efforts of the Arduino development team you don't have to worry about a lot of these things.  Look at Arduino examples.  All of them have a setup() function that is executed once and a loop() function that is executed repeatedly inside the Arduino core library main() function.  A "normal" Arduino sketch must have these two functions. (See Footnote.)

After that, the sky's the limit.


Regards,

Dave

Footnote:
It very well may be that one or the other (setup() or loop()) is empty, but they both have to be there as long as you don't make your own main() function in your sketch.  (And if you decide to branch out with your own, non-Arduino, way, you may lose a fair amount of support in the generous and knowledgeable user community on this forum because most of us don't do it that way.)
« Last Edit: May 30, 2011, 11:22:20 pm by davekw7x » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well I cheated a little to resolve the problem for this one time

Obviously main didnt have anything returned for setup and loop so  all I did was

    void setup()
    void loop()

and it compiles great..

I wont leave main like that however
Logged

Pages: [1]   Go Up
Jump to: