how to solve this c linker error? - c

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, e.g. 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).

Related

How to define a const struct to make a consant

I'm trying to make a const struct so I can use is values as constant
For this purpose I have write this code:
typedef struct __PIN{
GPIO_TypeDef GPIO;
uint16_t pin;
}__PIN;
const struct __PIN PA1 = {.GPIO=GPIOA,.pin=GPIO_PIN_5};
But When I compile it prompt me whit the following error:
"multiple definition of 'PA1' "
but it's only defined in this file!
in fact the its said that is defined first in the same file and line that prompt the error:
.../CUSTOM/GPIO_custom/GPIO_lite_s.h:27: multiple definition of `PA1'; .../CUSTOM/GPIO_custom/GPIO_lite_s.h:27: first defined here
what is going on?
should I do this on a different way?
additional info:
-this is the header of a library that Im making, I want this constant value to be accesible to all files that include this header.
-Im programming on a STM32 board (STM32F411CEU6) using STM32CubeIDE 1.10.1
-I included the main.h so I can use the uint8_t like variables
-this files is currently being included in 2 files, main.c and another .c file, deleting its include in either of those files seems to fix the problem, but I don't know why.
any help would be very much appreciated!

Resolve 'undefined reference' to a global external array variable

I have a header constants.h file, with the following declarations of an array variable:
extern storageCommandData storageCommands[];
The type of the array is defined elsewhere, and is not relevant to the question.
In another source file (.c) I initialized the array like so:
#include "constants.h"
storageCommandData storageCommands[STORAGE_COMMAND_NUM] =
{
/*storageCommandData intilazation follows the
following template: {commandName, storageSize}*/
{".db", 1},
{".dw", 4},
{".dh", 2},
{".asciz", 0},
};
I tried to use these arrays in another source file (a different one than the one I define the arrays in), by including constants.h.
However, when I try to use the variable storageCommands I get the following error message:
undefined reference to `storageCommands'
How do I fix the error?
Using extern means that you are using a variable that is declared in another translation unit (i.e. basically a source file and the headers it includes). It is the linker's task to relate the extern-specified variable name to its actual declaration. If the linker cannot find the latter, it will report an "undefined reference".
As already pointed out in the comments, the most common cause of this error is that the source file containing the actual declaration was not compiled or linked.

Avoid redefinition of alias variable for weak reference in header-file

I have a variable in an header file:
myHeader.h
uint16 dummyVar = 0;
extern const uint16 myVar __attribute__((weak,alias("dummyVar")));
So when I don't link the parts where myVar gets defined, the linker will just give it the value of the symbol dummyVar.
My problem is, that I am working on a Project with a given architecture, where my Header-File myHeader.h is included by several C-Files. Because of that I get multiple definitions of dummyVar . But when I define dummyVar outside of my header it doesnt work anymore for my __attribute__ since dummyVar needs to be defined when it is assigned as an alias.
Is there any way I can work around this without changing the basic architecture of this?
A simple solution would be to to just declare it as follows:
static uint16 dummyVar = 0;

What is wrong with using extern in C like that?

I've already searched something abou this but I've still don't understand it..
file1.h
extern int *game_array[5];
Player.c
#include "file1.h"
void *delete_player(player_struct *player)
{
... //some code
game_array[5] = 5; //undefined reference to `game_array`
... //some code
}
When I don't use extern it "work's fine" = I can build it withou errors, but the program's not finished ..
I suppose the using extern is fine but something is wrong ..
I want to use this game_array ... array of games on server from all my .c source files, there's only one instance of this array in my aplication.
You need to define game_array in one of your .c files, and compile/link that file into your executable.
The definition will look like this:
int *game_array[5];
What your file1.h is saying is basically "There exists a variable called game_array somewhere in my project, and it has such-and-such type". However, the variable doesn't actually exist until you've defined it somewhere (typically, in a .c file).
The extern keyword basically means that the compiler should not complain about the symbol, even if it's not defined, because it will be available at link-time.
So if it's not defined at link-time, you'll obviously have errors.
You need to provide an implementation in one of your C file:
Something like:
#include "file1.h"
int * game_array[ 5 ];
void * delete_player( player_struct * player)
{
...
Writing extern int *game_array[5]; means that somewhere, in some file, there's an actual definition for game-array -- i.e., a declaration without the extern. You're failing to provide that, so the linker complains that the actual variable doesn't exist anywhere.

initializing a static variable in header

I am new in programming in C, so I am trying many different things to try and familiarize myself with the language.
I wrote the following:
File q7a.h:
static int err_code = 3;
void printErrCode(void);
File q7a.c:
#include <stdio.h>
#include "q7a.h"
void printErrCode(void)
{
printf ("%d\n", err_code);
}
File q7main.c:
#include "q7a.h"
int main(void)
{
err_code = 5;
printErrCode();
return 0;
}
I then ran the following in the makefile (I am using a Linux OS)
gcc –Wall –c q7a.c –o q7a.o
gcc –Wall –c q7main.c –o q7main.o
gcc q7main.o q7a.o –o q7
the output is 3.
Why is this happening?
If you initialize a static variable (in fact any variable) in the header file, so if 2 files include the same header file (in this case q7.c and q7main.c) the linker is meant to give an error for defining twice the same var?
And why isn't the value 5 inserted into the static var (after all it is static and global)?
Thanks for the help.
static means that the variable is only used within your compilation unit and will not be exposed to the linker, so if you have a static int in a header file and include it from two separate .c files, you will have two discrete copies of that int, which is most likely not at all what you want.
Instead, you might consider extern int, and choose one .c file that actually defines it (i.e. just int err_code=3).
When you declared a variable as a static it has only scope within a file ie.., it can be accessed only within a file.
When you declare a static variable in a header file and include this header file in two .c file, then you are creating two different
memory for two different ".c" files
When you print err_code directly in the Main function you would see its value as 5 instead 3,
but you are calling a function printErrCode which is defined in a different file "q7a.c" for which err_code has a different memory location
in which err_code memory is still 3 and it not updated and that is why you are getting the value as 3 instead of 5.
Since two memory is created and err_code is considered as two different variables having different memory with different file scope you will not see any linking errors.
The static variables do not have external linkage which means they cannot be accessed outside the translation unit in which they are being defined. So in your case when q7.h is #include'ed in both translations units q7a.c and q7main.c ... two different copies exists in their corresponding .o files. That is why linker does not report error becuase both copies are not seen by linker while doing external symbol linkage.
While doing small research came to know that we can declare variable in Header file but in one of the source file includes that should have definition for that variable.
Instead if we define a variable in header file. in the source files where this header file included, definitions will be created which causes multiple definitions.
A static variable should be declared with in the file where we use it shouldn't be exposed to header file.
Hope I am giving right information. If I am wrong feel free to correct my statements in your comments.

Resources