Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » AVR-GCC Assembly help(Trying to compile test.S get /usr/lib/gcc/avr/4.9.2/../../../avr/lib/avr5/crtatmega644a.o:(.init9+0x0): undefined reference to `main')
AVR-GCC Assembly help [message #1746741] Fri, 04 November 2016 00:24 Go to next message
Rod Naugler is currently offline Rod NauglerFriend
Messages: 65
Registered: September 2016
Member
Hi all,
As part of a university course we are required to program an Atmel Microcontroller in ASM. I have gotten gavrasm running, but not with eclipse and I've been encouraged to use avr-gcc instead. I've gotten a basic Test.S file compiling but it fails in a later stage with the following error:
/usr/lib/gcc/avr/4.9.2/../../../avr/lib/avr5/crtatmega644a.o:(.init9+0x0): undefined reference to `main'

Can anyone help me understand what's going on?

Thanks!
Re: AVR-GCC Assembly help [message #1746758 is a reply to message #1746741] Fri, 04 November 2016 10:01 Go to previous messageGo to next message
Tauno Voipio is currently offline Tauno VoipioFriend
Messages: 742
Registered: August 2014
Senior Member
The avr-gcc compiler driver is attempting to link your object module with the standard AVR start-up code and C library. The complaint comes from the linker, as it does not find the standard C starting entry main().

You can invoke the assembler using avr-as instead of avr-gcc, but it does not expand pre-processor macros (which your .S file implies).

You can prevent the linking phase by adding the -c switch to the avr-gcc command line.

The linker can be invoked with avr-ld. Depending on your assembly code, you may skip the linking phase, if the code is absolute already.


--

Tauno Voipio
Re: AVR-GCC Assembly help [message #1746765 is a reply to message #1746758] Fri, 04 November 2016 11:08 Go to previous messageGo to next message
Rod Naugler is currently offline Rod NauglerFriend
Messages: 65
Registered: September 2016
Member
I'm not 100% sure of what you've told me as I'm not 100% sure how Eclipse works with ASM files. I have edited the c/c++ build settings, under AVR Assembler settings and added the -c to the avr-gcc command (avr-gcc -c) and I can see it in the command window:

 **** Build of configuration Debug for project avr-asm-test ****

make all 
Building file: ../test.S
Invoking: AVR Assembler
avr-gcc -c -x assembler-with-cpp -g2 -gstabs -mmcu=atmega644a -DF_CPU=14745600UL -MMD -MP -MF"test.d" -MT"test.d" -c -o "test.o" "../test.S"
Finished building: ../test.S
 
Building target: avr-asm-test.elf
Invoking: AVR C++ Linker
avr-g++ -Wl,-Map,avr-asm-test.map,--cref -mrelax -Wl,--gc-sections -mmcu=atmega644a -o "avr-asm-test.elf"  ./test.o   
/usr/lib/gcc/avr/4.9.2/../../../avr/lib/avr5/crtatmega644a.o:(.init9+0x0): undefined reference to `main'
makefile:59: recipe for target 'avr-asm-test.elf' failed
collect2: error: ld returned 1 exit status
make: *** [avr-asm-test.elf] Error 1

**** Build Finished ****

Re: AVR-GCC Assembly help [message #1746816 is a reply to message #1746765] Fri, 04 November 2016 23:42 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
You need to understand how a C program is run. Eclipse is expecting objects will conform to the C layout model.

Normally, the layout for a C program is
- startup code
- main + other functions
- exit code

In C, the startup and exit code is in the standard C libraries.
When the program is executed the entry point is in the startup code.
During startup execution it will call main.
When the program is done, it returns to the exit code which ends up returning to the OS.

Modules are normally created in object format by compiling and assembling. These are then linked together along with other objects from libraries to form an executable.

The error you are seeing is caused by GCC adding the startup code which wants a main function.


Your program can take one of three forms:
1) it can startup the way an OS would start (that is, it is the OS)
2) It can be started as a program by the OS which eventually tells the OS when it is done
3) if can use the C layout model

I don't know which you are trying to make plus I'm not familiar with the device you are building for.

If your program is type (2) above, you may be able to avoid dragging in the C library by using the GCC loader options -nostartfiles, -nodefaultlibs, and -nostdlib during the linking phase.

GCC ld options: https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

When using GCC to execute ld you need to use -Wl,<option> for some options.
GCC options summary: https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html

You may also need to specify the entry point. The following link shows how but I can't readily find the option so I'm not sure it's still available.
http://stackoverflow.com/a/7494273/3312726

If your program is type (1) you may be able to bypass the linking phase but to do this you will need to change the tool chain to not call the linker. Another way is execute the assembler in a command console and only use Eclipse as an editor.

If you want to make a type (3), then add a main function and use it as your entry point.






Re: AVR-GCC Assembly help [message #1746832 is a reply to message #1746765] Sat, 05 November 2016 10:25 Go to previous message
Tauno Voipio is currently offline Tauno VoipioFriend
Messages: 742
Registered: August 2014
Senior Member
Rod Naugler wrote on Fri, 04 November 2016 13:08
I'm not 100% sure
 **** Build of configuration Debug for project avr-asm-test ****

make all 
Building file: ../test.S
Invoking: AVR Assembler
avr-gcc -c -x assembler-with-cpp -g2 -gstabs -mmcu=atmega644a -DF_CPU=14745600UL -MMD -MP -MF"test.d" -MT"test.d" -c -o "test.o" "../test.S"
Finished building: ../test.S
 
Building target: avr-asm-test.elf
Invoking: AVR C++ Linker
avr-g++ -Wl,-Map,avr-asm-test.map,--cref -mrelax -Wl,--gc-sections -mmcu=atmega644a -o "avr-asm-test.elf"  ./test.o   
/usr/lib/gcc/avr/4.9.2/../../../avr/lib/avr5/crtatmega644a.o:(.init9+0x0): undefined reference to `main'
makefile:59: recipe for target 'avr-asm-test.elf' failed
collect2: error: ld returned 1 exit status
make: *** [avr-asm-test.elf] Error 1

**** Build Finished ****



It seems to me that you're using the internal Makefile builder, which has contradictory instructions for configuration: The compilation is made using simple C compiler (avr-gcc), but the linking is done with C++ linkage setup.

Try from console, in the object file directory:
avr-ld -Map=avr-asm-test.map -mcu=atmega644a -o avr-asm-test.elf test.o

Your assembly-only project is ill-suited for the automatic Makefile generation. IMHO, you should create a Makefile project for it and create the make file by hand.
There are good instructions in the GNU Make manual, and the Net contains plenty of instructions for using the linker.

Also, this project may need a linker script tailored to the processor at hand.



--

Tauno Voipio

[Updated on: Sat, 05 November 2016 10:26]

Report message to a moderator

Previous Topic:build is very slow
Next Topic:Neon.1: Remote Debug problem
Goto Forum:
  


Current Time: Thu Apr 25 06:07:36 GMT 2024

Powered by FUDForum. Page generated in 0.03400 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top