1

I got this error when compiling my code in visual studio and i need help please : Error LNK2005: _cmd already defined in complex.obj

I have the following C Files :

### File name: Main.c ###

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "complex.h"


int main()
{
    char command[30];
    int i;
    FOREVER 
        if (scanf("%s", command) == 1)
        {
            for (i = 0; cmd[i].func != NULL; i++)/*searcing the relevant function*/
            {
                if (strcmp(command, cmd[i].name) == 0)
                    break;
            }
            if (cmd[i].func == NULL)
                printf("Command does not exist: %s\n", command);
            else
                (*(cmd[i]).func)();
        }
}

and this:

### File name: Complex.h ###
#define FOREVER for(;;)

typedef struct complex
{
    double real;
    double imag;
}complex;


void read_comp(void);
void print_comp(void);
void add_comp(void);
void sub_comp(void);
void mult_comp_real(void);
void mult_comp_img(void);
void mult_comp_comp(void);
void abs_comp(void);
void halt(void);
void empty_string(void);
void stop(void);

struct STR{
    char* name;
    void(*func)(void);/*pointer to function*/
}cmd[] = {
    { "read_comp", read_comp },
    { "print_comp", print_comp },
    { "add_comp", add_comp },
    { "sub_comp", sub_comp },
    { "mult_comp_real", mult_comp_real },
    { "mult_comp_img", mult_comp_img },
    { "mult_comp_comp", mult_comp_comp },
    { "abs_comp", abs_comp },
    { "halt", halt },
    { "stop", stop }
};

and this:

### File name: Complex.c ###    
#include "complex.h"

    void stop(void)
    {
        exit(1);
    }

    void read_comp(void)
    {
        printf("read_comp\n");
    }
    void print_comp(void)
    {
        printf("print_comp\n");
    }
    void add_comp(void)
    {
        printf("add_comp\n");
    }
    void sub_comp(void)
    {
        printf("sub_comp\n");
    }
    void mult_comp_real(void)
    {
        printf("mult_comp_real\n");
    }
    void mult_comp_img(void)
    {
        printf("mult_comp_img\n");
    }
    void mult_comp_comp(void)
    {
        printf("mult_comp_comp\n");
    }
    void abs_comp(void)
    {
        printf("abs_comp\n");
    }
    void halt(void)
    {
        printf("halt\n");
    }
    void empty_string(void)
    {
        printf("Empty sting, Please try again\n");
    }

We have here some functions that get some parameters as inputs through the command line. The functions implementation is not finished yet. Please i need help with solving the error.

byako
  • 3,372
  • 2
  • 21
  • 36
Gedaan
  • 31
  • 1
  • 7
  • Move cmd[] define out of header file, place it in your local c file, namely complex.c and access it by API – How Chen May 24 '17 at 12:39
  • complex.h is a standard library header. Do not name your own headers the same thing as existing libraries, or you might get all manner of weird errors. – Lundin May 24 '17 at 13:01
  • Possible duplicate of [LNK2005 Error in CLR Windows Form](https://stackoverflow.com/questions/34748913/lnk2005-error-in-clr-windows-form) – CristiFati May 24 '17 at 15:02

1 Answers1

2

Since you define the struct cmd in complex.h it will be instantiated in both object files, which then leads to the linking error.

You should define the struct only in one of the two files and declare it extern it in your header file.

This goes into your header file:

struct STR{
    char* name;
    void(*func)(void);/*pointer to function*/
};
extern struct STR *cmd;

And this for example in complex.c:

struct STR cmd[] = {
  { "read_comp", read_comp },
  { "print_comp", print_comp },
  { "add_comp", add_comp },
  { "sub_comp", sub_comp },
  { "mult_comp_real", mult_comp_real },
  { "mult_comp_img", mult_comp_img },
  { "mult_comp_comp", mult_comp_comp },
  { "abs_comp", abs_comp },
  { "halt", halt },
  { "stop", stop }
};
Ctx
  • 18,090
  • 24
  • 36
  • 51
  • Hi Ctx and thank you for your answer, i got this error when correcting the the code as you wrote : Error C2372 'cmd': redefinition; different types of indirection mman22 c:\users\sofi lt3\desktop\documents\visual studio 2017\projects\mman13\mman13\complex.c 3 – Gedaan May 24 '17 at 13:05