Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Linker error when function is implemented in extern file .c (Read 3562 times)
zimka
Newbies
*
Offline


Posts: 1
Joined: May 19th, 2015
Linker error when function is implemented in extern file .c
May 19th, 2015 at 7:15am
Print Post  
Hello.
I've got some trouble with linker in Atmel Studio 6.2, Arduino ProMicro Leonardo, Arduino 1.6 IDE.
I tried to write simple project for blinking with one header and one .c file.

blink2.ino:
Code (C++)
Select All
#include "testc.h"

void setup()
{
	pinMode(17, OUTPUT);
	//Time = 500;
	//Step = 100;
	initTime();
}
void loop()
{
        digitalWrite(17, HIGH);
	delay(1000);
	Time += Step;
	digitalWrite(17, LOW);
	delay(Time);
} 

 
testc.h:
Code (C++)
Select All
#ifndef _TESTC_h
#define _TESTC_h

#if defined(ARDUINO) && ARDUINO >= 100
	#include "arduino.h"
#else
	#include "WProgram.h"
#endif
int Time;
int Step;
void initTime(void);
int giveTime(void);
#endif 


Code (C++)
Select All
#include "testc.h"

void initTime()
{
	Time = 500;
	Step = 100;
	return;
}

int giveTime()
{
	Time += Step;
	return Time;
} 


I get an error when I try to use functions from testc.c, but it is working without problems when I only use variables from testc.h and don't use functions;
Code
Select All
Compiling 'blink2' for 'Arduino Leonardo'
Build folder: file:///C:/Users/zimka/AppData/Local/V.Micro/Arduino/Builds/blink2/leonardo
Summary: Header=1 Prototypes=3 Imports=0
Additional Defines:
Architecture Tools: C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/
GCC: 4.8.1
Sketchbook: file:///C:/Program%20Files%20(x86)/Arduino/examples
Core Include Paths
Include Path 'C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino'
Include Path 'C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\leonardo'
"C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avr-gcc" -c -g -Os -w -ffunction-sections -fdata-sections -MMD -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=160 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 -DUSB_MANUFACTURER= -DUSB_PRODUCT="\"Arduino Leonardo\"" -I"C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" -I"C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\leonardo" "C:\Users\zimka\AppData\Local\V.Micro\Arduino\Builds\blink2\leonardo\testc.c" -o "C:\Users\zimka\AppData\Local\V.Micro\Arduino\Builds\blink2\leonardo\testc.c.o"
"C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avr-g++" -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=160 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 -DUSB_MANUFACTURER= -DUSB_PRODUCT="\"Arduino Leonardo\"" -I"C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" -I"C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\leonardo"  -o o"
Build Core Paths: C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino
"C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avr-gcc" -w -Os -Wl,--gc-sections -mmcu=atmega32u4 -o  "C:\Users\zimka\AppData\Local\V.Micro\Arduino\Builds\blink2\leonardo\testc.c.o" o" "C:\Users\zimka\AppData\Local\V.Micro\Arduino\Builds\blink2\leonardo/core.a" "-LC:\Users\zimka\AppData\Local\V.Micro\Arduino\Builds\blink2\leonardo" -lm
blink2.cpp.o:In function `setup'
collect2*:error: ld returned 1 exit status
Error creating .elf 


Thanks.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Linker error when function is implemented in extern file .c
Reply #1 - May 19th, 2015 at 11:30am
Print Post  
Hi Zimka,

Thanks for the clear report.

Your code is invalid so the error is correct although I will look into a better error report.

Are you new to Arduino? I think the initTime() needs to be public or external, why not use .cpp with real class or another .ino? Otherwise make sure you use external or some other means to declare your c methods

Thanks
« Last Edit: May 19th, 2015 at 11:30am by Tim@Visual Micro »  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint