[SOLVED] Library compiled in Atmel Studio 7, gives error in Arduino IDE

Hello,

I want to include my own I2C library in Arduino libraries folder.

I compiled the codes in Atmel Studio 7, and worked OK.

So I copied the folder to Arduino IDE.

Then, I only included the library header and compiled the code then I received the error:

“each undeclared identifier is reported only once for each function it appears in”

The library in the attachment and this is the code:

#include "I2C.h"

//Output = [Mset – Mreset] / 2

#define HMC5883L_read   0x3D
#define HMC5883L_write  0x3C
#define Configuration_Register_A    0x00  //Read/Write
#define Configuration_Register_B    0x01  //Read/Write
#define Mode_Register               0x02  //Read/Write
#define Data_Output_X_MSB_Register  0x03  //Read
#define Data_Output_X_LSB_Register  0x04  //Read
#define Data_Output_Z_MSB_Register  0x05  //Read
#define Data_Output_Z_LSB_Register  0x06  //Read
#define Data_Output_Y_MSB_Register  0x07  //Read
#define Data_Output_Y_LSB_Register  0x08  //Read
#define Status_Register             0x09  //Read
#define Identification_Register_A   0x10  //Read
#define Identification_Register_B   0x11  //Read
#define Identification_Register_C   0x12  //Read


void HMC5883L_init(void);



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

}

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

}


void HMC5883L_init(void)
{
  
}

I2C.c (1.62 KB)

I2C.h (510 Bytes)

I'm quite sure that there is more in the output window than just that.

The gcc compiler used by the Arduino IDE is better about warning about possible issues and better about finding language errors than many other compilers.

sterretje: I'm quite sure that there is more in the output window than just that.

Believe me, this is the only error, and of course you can download the files and test it and find the error, then you may know the problem.

vaj4088: The gcc compiler used by the Arduino IDE is better about warning about possible issues and better about finding language errors than many other compilers.

I don't think so in my issue, because the library works ok in Atmel Studio when uploaded to the chip.

And the same code if you past it in one Arduino sketch, it runs ok.

The problem is because I don't know how to develop a library which works in Atmel Studio and Arduino IDEs.

wolfrose: Believe me, this is the only error, and of course you can download the files and test it and find the error, then you may know the problem.

Believe me, there is important information that you are not giving us. We shouldn't have to download the files and compile just to get some output that would take you a matter of seconds to post, not much more time than it took you to tell us you're not going to post it. You're asking us for help. Help us to help you.

Please do this:

  • Open your code
  • Sketch > Compile/Verify
  • When you encounter an error you'll see a button on the right side of the orange bar "Copy error messages". Click that button. Paste the error in a message here using code tags.

wolfrose: And the same code if you past it in one Arduino sketch, it runs ok.

Please explain exactly what you mean by that. Post the sketch that runs ok.

pert: Believe me, there is important information that you are not giving us. We shouldn't have to download the files and compile just to get some output that would take you a matter of seconds to post, not much more time than it took you to tell us you're not going to post it. You're asking us for help. Help us to help you.

Please do this:

  • Open your code
  • Sketch > Compile/Verify
  • When you encounter an error you'll see a button on the right side of the orange bar "Copy error messages". Click that button. Paste the error in a message here using code tags.

Please explain exactly what you mean by that. Post the sketch that runs ok.

First I'm sorry I think I copied the red message which appeared to me, but I think if I clicked on copy error message button, then I guess I would get other error message. Let me try, I just went out with my wife for lunch.

The second part I mean that, if I copied the code in .h and .c files into one Arduino sketch and compiled that, then it compiles and works OK, but when I separated them into .h and .c, it didn't work well but the library works OK in Atmel Studio 7.

Believe me, this is the only error, and of course you can download the files and test it and find the error, then you may know the problem.

Tried it :wink: IDE 1.6.6

C:\Users\sterretje\AppData\Local\Temp\build6baaa555d42b2cf3041801fdcc735234.tmp\sketch\I2C.c: In function 'I2C_TWSR_Check':

C:\Users\sterretje\AppData\Local\Temp\build6baaa555d42b2cf3041801fdcc735234.tmp\sketch\I2C.c:51:5: error: 'Serial' undeclared (first use in this function)

     Serial.println("Start is OK");

     ^

C:\Users\sterretje\AppData\Local\Temp\build6baaa555d42b2cf3041801fdcc735234.tmp\sketch\I2C.c:51:5: note: each undeclared identifier is reported only once for each function it appears in

exit status 1
Error compiling.

You can place the cursor in the output window and scroll through it; the above was retrieved using that method.

Easiest is to include Arduino.h. You also need to get rid of bitmask in the I2C.h file as it will cause errors due to double include (in the sketch and in the c file). And you need to rename the c-file to cpp (or use extern C or something like that).

Tested by simply placing your library files at the same level as the sketch; I had to close the IDE and open it by double clicking the sketch though (??)

sterretje: Tried it ;) IDE 1.6.6

C:\Users\sterretje\AppData\Local\Temp\build6baaa555d42b2cf3041801fdcc735234.tmp\sketch\I2C.c: In function 'I2C_TWSR_Check':

C:\Users\sterretje\AppData\Local\Temp\build6baaa555d42b2cf3041801fdcc735234.tmp\sketch\I2C.c:51:5: error: 'Serial' undeclared (first use in this function)

    Serial.println("Start is OK");

    ^

C:\Users\sterretje\AppData\Local\Temp\build6baaa555d42b2cf3041801fdcc735234.tmp\sketch\I2C.c:51:5: note: each undeclared identifier is reported only once for each function it appears in

exit status 1 Error compiling.

Sorry :) I just got home.

You're right!

Arduino: 1.8.0 (Windows 10), Board: "Arduino Nano, ATmega328"

E:\Program Files Directory\Arduino\libraries\I2C\I2C.c: In function 'I2C_TWSR_Check':

E:\Program Files Directory\Arduino\libraries\I2C\I2C.c:48:3: error: 'Serial' undeclared (first use in this function)

   Serial.println("Start is OK");

   ^

E:\Program Files Directory\Arduino\libraries\I2C\I2C.c:48:3: note: each undeclared identifier is reported only once for each function it appears in

exit status 1
Error compiling for board Arduino Nano.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

sterretje: You can place the cursor in the output window and scroll through it; the above was retrieved using that method.

Easiest is to include Arduino.h. You also need to get rid of bitmask in the I2C.h file as it will cause errors due to double include (in the sketch and in the c file). And you need to rename the c-file to cpp (or use extern C or something like that).

Tested by simply placing your library files at the same level as the sketch; I had to close the IDE and open it by double clicking the sketch though (??)

You absolutely right :)

Problem is solved.

But why bitmask did a problem? It's only an uninitialized variable. How it caused an error?

Because you include I2C.h twice. Once in I2C.cpp and once in the sketch; so both now have a variable called bitmask. During the linking two variables with the same name are found and the linker objects.

This is a new area for me but this is my understanding; probably the first time that I implement this properly.

If you need bitmask in I2C.cpp, you can declare it there.

#include "I2C.h"
#include <Arduino.h>
//#include <avr/io.h>

uint8_t bitmask = 0xFF;
...
...

If you also need it in the sketch, you need to define it in I2C.h as extern. The ‘extern’ keyword tells the compiler that the variable is declared elsewhere (later in the code or in another file).

#ifndef I2C_H_
#define I2C_H_

#include <inttypes.h>
void I2C_init(void);
void I2C_start(uint8_t address);
void I2C_stop(void);
void I2C_tx(uint8_t data);
uint8_t I2C_rx(void);
void I2C_TWSR_Check(void);

extern uint8_t bitmask;

/////////// I2C modules addresses ////////////
// 8-bit I/O expander 0x20
#define IOEXP_WR  0x40    // I/O 8-bits expander address
#define IOEXP_RD  0x41

// LCD1602 adapter
#define LCD1602_WR  0x4E  // LCD1602 I2C adapter address
#define LCD1602_RD  0x4F

#endif

And next you can use it in the sketch.

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

  bitmask = 0x00;
}

Play with it