-->
Page 1 of 1

Compiler / Linker Problem

PostPosted: Tue Feb 06, 2018 1:19 pm
by Arnold
Hello Together,

i think the compiler / linker is still buggy.

I just moved one variable from the main file to one header file an i get various errors which are strange.

Moved: I2cbuffer from main app to global.h

then compiled again and got this error´s

BasicObjClass.cpp.o*: In function BasicObjCLass::run(int)
BasicObjClass.h:36: multiple definition of i2c_buffer
sonoff.cpp.o*: ..arduino15\packages\esp8266\hardware\esp8266\2.4.0\cores\esp8266\pgmspace.h:103: first defined here
global.cpp.o*: (.bss.i2c_buffer+0x0): multiple definition of i2c_buffer
sonoff.cpp.o*: ..l\arduino15\packages\esp8266\hardware\esp8266\2.4.0\cores\esp8266\pgmspace.h:103: first defined here
PCF8574.cpp.o*: (.bss.i2c_buffer+0x0): multiple definition of i2c_buffer

Error linking for board Generic ESP8266 Module
sonoff.cpp.o*: C:\Users\Braun\AppData\Local\arduino15\packages\esp8266\hardware\esp8266\2.4.0\cores\esp8266\pgmspace.h:103: first defined here

PCF8591.cpp.o*: In function PCF8591::getid()
PCF8591.h:29: multiple definition of i2c_buffer
sonoff.cpp.o*: ..l\arduino15\packages\esp8266\hardware\esp8266\2.4.0\cores\esp8266\pgmspace.h:103: first defined here
TFTSD1303.cpp.o*: (.bss.i2c_buffer+0x0): multiple definition of i2c_buffer
sonoff.cpp.o*: ..l\arduino15\packages\esp8266\hardware\esp8266\2.4.0\cores\esp8266\pgmspace.h:103: first defined here
Build failed for project 'sonoff'


The global.h ist not big and looks like this:


// global.h

#ifndef _GLOBAL_h
#define _GLOBAL_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif

#define I2C_RETRY_COUNTER 3

uint32_t i2c_buffer = 0;

String GetFirstParam(String * param, const char seperator);

int8 GetIndex(String param, String Search, const char seperator);
String GetEntry(uint8_t idx, String param);

uint8_t I2cRead8(uint8_t addr, uint8_t reg);
bool I2cValidRead(uint8_t addr, uint8_t reg, uint8_t size);


#endif

global.cpp

#include "global.h"

#include "Wire.h"




String GetFirstParam(String *param, const char seperator) // Extract the first param
{
and so on

Re: Compiler / Linker Problem

PostPosted: Tue Feb 06, 2018 11:24 pm
by jcmvbkbc
Arnold wrote:I just moved one variable from the main file to one header file an i get various errors which are strange.

Moved: I2c_buffer from main app to global.h

then compiled again and got this error´s

BasicObjClass.cpp.o*: In function BasicObjCLass::run(int)
BasicObjClass.h:36: multiple definition of i2c_buffer

...

The global.h ist not big and looks like this:


// global.h

#ifndef _GLOBAL_h
#define _GLOBAL_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif

#define I2C_RETRY_COUNTER 3

uint32_t i2c_buffer = 0;

I agree with the compiler: build error looks correct to me, since you have definition of i2c_buffer in each compilation unit that includes global.h. If you meant to declare i2c_buffer in the global.h then you need to remove the ' = 0' part.

Re: Compiler / Linker Problem

PostPosted: Tue Feb 06, 2018 11:47 pm
by McChubby007
The 'bug' is in the programmer, not the compiler.

Re: Compiler / Linker Problem

PostPosted: Thu Mar 22, 2018 11:42 am
by Solomon Candy
McChubby007 wrote:The 'bug' is in the programmer, not the compiler.

Take it easy on him.. we were all newbies once.

He is right though. The bug is in your code. The header file is supposed to only refer to contents within a library or an object file (i.e. a .o file obtained after compiling a .c file) - you shouldn't declare variables within a header file.

If you want to expose your variable globally, write `extern uint32_t i2c buffer;' withing the header file and just declare the variable as `uint32_t i2c_buffer = 0' within a .c file - and declare the variable in only one .c file or you'll get the error again (just declare it in main.c as you did before).