Ahmed Hal
Ahmed Hal

Reputation: 11

LNK2005 " " already defined in main.obj

i don't know why do i get this error

//sarver.h
typedef char uint8_t;

typedef enum EN_accountState_t
{
    RUNNING,
    BLOCKED
}EN_accountState_t;

typedef struct ST_accountsDB_t
{
    float balance;
    EN_accountState_t state;
    uint8_t primaryAccountNumber[20];
}ST_accountsDB_t;

//------------------------------------------

//server.c
#include "server.h"

ST_accountsDB_t validAccounts[250] = { { 2000,RUNNING,"5807007076043875" }
,{ 3000,RUNNING,"5807007076043876" },{ 40000,BLOCKED,"5807007076043877" }
,{ 5000,RUNNING,"5807007076043878" } ,{ 6000,RUNNING,"5807007076043879" } };

//------------------------------------------

//main.c


#include <stdio.h>

#include "server.c"

int main() {

printf("balance =  %f", validAccounts[0].balance);

}

errors>

Error   LNK2005 validAccounts already defined in main.obj   
Error   LNK1169 one or more multiply defined symbols found  

when i place all this code in one file it runs fine but i don't know why it doesn't when it's like this. if anyone knows what the proplem pleas help

Upvotes: 0

Views: 113

Answers (1)

0___________
0___________

Reputation: 67835

Delete this line as it defined new instance of this object.

#include "server.c"

Basically never include .c files only header files. Do not put any data or code in the header files, only function prototypes, extern object definitions and data types declaration.

Modify sarver.h:

#ifndef SARVER_H    //guard
#define SARVER_H

#include <stdint.h>
//typedef char uint8_t;   - it is standard type, do not declare it yourself
typedef enum EN_accountState_t
{
    RUNNING,
    BLOCKED
}EN_accountState_t;

typedef struct ST_accountsDB_t
{
    float balance;
    EN_accountState_t state;
    uint8_t primaryAccountNumber[20];
}ST_accountsDB_t;

extern ST_accountsDB_t validAccounts[250];

#endif

And include it into main.c file.

#include <stdio.h>

#include "sarver.h"

int main() {

printf("balance =  %f", validAccounts[0].balance);

}

(`sarver.h' - because this name was given by the question author)

Header files content should be protected by the guards.

Upvotes: 1

Related Questions