0
uint8 *measurements[30] = {(uint8*)0x0041c620}; 

I have declared a global variable in my program like above but I am getting a linker error as

LNK2005: _measurements already defined in MAIN.obj

I am modifying the code as

typedef unsigned char uint8;
    uint8 *measurements[30];
    measurements[30]= {(uint8*)0x0041c620}

;

then also I am getting the error

user3189297
  • 65
  • 1
  • 10
  • Where have you declared your measurements variable? – Vladimir Jan 13 '14 at 09:15
  • If you are using global in another file.. use extern where you are trying to use it. – Digital_Reality Jan 13 '14 at 09:15
  • @vladmir: I edited the code. Is that the right way ?? – user3189297 Jan 13 '14 at 09:18
  • Check if `measurements` is defined in any of the other object files you link with. If so, perhaps you want to declare it `static` to keep the symbol invisible to the outside world. Or, as Digital_Reality points out, say you want to use the "other" declaration by using the keyword `extern`. Please take a look at [this](http://msdn.microsoft.com/en-us/library/72zdcz6f.aspx) document or [this](http://stackoverflow.com/questions/10046485/error-lnk2005-already-defined) related discussion. – Jens Jan 13 '14 at 09:22
  • Jens: thank you very much for the reply. it is showing the size of the measurement has of 4bytes each but I have specified as uint8. it should be of 1byte each. – user3189297 Jan 13 '14 at 09:36
  • Did you mean to have an array of 30 `uint8_t`s instead? – user694733 Jan 13 '14 at 09:57
  • uint8: unsigned integer of type 8bits (1byte). but if i declare as uint8 *measurements[30]; then the size(measurements) = 120. It should show 30 right ?? – user3189297 Jan 13 '14 at 10:01
  • Just as a side note: it seems that the `0x0041c620` is an address? Unless you nail code to specific addresses, this is quite likely going to fail at runtime. You are getting this error because the name "measurements" is used to declare a variable in multiple files, it's not because of different types. And it can be accidentally be declared in multiple files, if the declaration is in an include file which you include in different files. – Jens Jan 13 '14 at 10:13

3 Answers3

0

The error tells you that you have defined this global variable in more than one translation unit - in MAIN.obj as well as in some other. Which one I cannot tell, because you didn't post the whole error message, only one line of it.

Your question does not contain enough information to reproduce the problem, or tell where exactly you made the error. Perhaps you defined the variable in a header file?

DevSolar
  • 67,862
  • 21
  • 134
  • 209
0

Is the definition in a header file? If so, you'll get one definition of the variable for each source file that includes the header. Try marking changing the header to only declare the variable, not define it, like so:

  extern uint8_t *measurements[30];

and then define it in one of the files, e.g. main.c like so:

  uint8 *measurements[30] = {(uint8*)0x0041c620}; 
Magnus Reftel
  • 967
  • 6
  • 19
  • Thank you for the reply. The size is of 4 bytes. How could I make it as 1byte ?? – user3189297 Jan 13 '14 at 09:48
  • uint8 is 8 bit integer but If i do like above then the size of each variable is 4byte. – user3189297 Jan 13 '14 at 09:51
  • That's likely because you're declaring an array of pointers to 8-bitvalues, instead of an array of 8-bit values. Try uint8_t measurements[30] = {0x11, 0x22, 0x33}; or something like that. Guessing from your code, it looks like you may want to load an 8-bit value from the address 0x0041c620. That, you can do like so: uint8_t measurements[30] = { * (uint8_t *)0x0041c620}; – Magnus Reftel Jan 13 '14 at 15:25
0

It seems you want array of 30 8-bit values, and that array is already existing at some specific address.

In one source file (measurements.c):

uint8 * const measurements = (uint8*)0x0041c620;

In header file (measurements.h):

uint8 * const measurements;

Usage:

#include "measurements.h"

// In some function
measurements[29] = ... // Set last element to something

Note that I added the const because I think you do not want to change array address (0x0041c620).

user694733
  • 15,208
  • 2
  • 42
  • 68
  • 0x0041c620 : This will be the starting address of the measurement ?? – user3189297 Jan 13 '14 at 10:11
  • It **should** be. I hope you know what you are doing when hardcoding addresses. – user694733 Jan 13 '14 at 10:12
  • could you tell me what is that ?? – user3189297 Jan 13 '14 at 10:18
  • What is *what*? You should know where 0x0041c620 comes from, it was in your question. – user694733 Jan 13 '14 at 10:18
  • oh sorry. The size of the each variable is 4 bytes. How to make it as 1byte ?? – user3189297 Jan 13 '14 at 10:23
  • Sorry, but I am not a mind reader. Maybe it is better if you post a new question where you post your code, explain what you are trying to do, and what problem you have with your current code. I think you are trying to solve wrong problem, but your current question doesn't give enough to determine what the real problem is. – user694733 Jan 13 '14 at 10:30