简体   繁体   中英

how to solve this c linker error?

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

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?

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, eg main.c like so:

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM