1

私は、いくつかのファイルで構成された小さな C プログラムを作成しました。コンパイルすると、「複数の定義」のエラーが発生します。

私のmain.c:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "general_structs.h"

#define FOREVER for(;;)
#define INPUT_LEN 30

int main()
{
    char command[INPUT_LEN];
    char *func;
    int i;
    int t;

    FOREVER
    {
        if(scanf("%s", command) == 1)
        {
            func = strtok(command, " ");
            for(i=0;cmd[i].func != NULL;i++)
            {
                if(strcmp(func, cmd[i].name) == 0)
                {
                    (*((cmd[i].func)));
                    t = 1;
                }
            }
            if(t == 1)
            {
                printf("No such command");
            }
        }
    }   
    return 0;   
}

私のmat.cファイル:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "general_structs.h"

#define LENGTH 100
#define SIXTEEN 16
#define SIZE 4


void read_mat()
{
    int i = 0;
    int j = 0;
    int k = 0;
    char tmp_name[LENGTH];
    char num_buffer[LENGTH];
    char *token;
    double num_list[16];
    double tmp_num = 0;

    scanf("%[^,], %s", tmp_name, num_buffer);

    token = strtok(num_buffer, ",");

    while(token != NULL)
    {
        if(strcmp(token, "0") == 0)
        {
            num_list[i] = 0;
        }
        else
        {
            tmp_num = atof(token);
            if(tmp_num == 0)        
            {
                printf("Error in parameter: %d\n", (i-1));
                break;
            }
            else
            {
                num_list[i] = tmp_num;
            }
        }   
    i++;
    token = strtok(NULL, ",");
    }

    if(!strcmp(tmp_name, "MAT_A"))
    {
        for(i=0;i<SIZE;i++)
            for(j=0;j<SIZE;j++)
            {
                mats[0].mat[0][i][j] = num_list[k];
                k++;
            }

    }
    else if(!strcmp(tmp_name, "MAT_B"))
    {
        for(i=0;i<SIZE;i++)
            for(j=0;j<SIZE;j++)
            {
                mats[1].mat[0][i][j] = num_list[k];
                k++;
            }
    }
    else if(!strcmp(tmp_name, "MAT_C"))
    {
        for(i=0;i<SIZE;i++)
            for(j=0;j<SIZE;j++)
            {
                mats[2].mat[0][i][j] = num_list[k];
                k++;
            }
    }
    else if(!strcmp(tmp_name, "MAT_D"))
    {
        for(i=0;i<SIZE;i++)
            for(j=0;j<SIZE;j++)
            {
                mats[3].mat[0][i][j] = num_list[k];
                k++;
            }
    }
    else if(!strcmp(tmp_name, "MAT_E"))
    {
        for(i=0;i<SIZE;i++)
            for(j=0;j<SIZE;j++)
            {
                mats[4].mat[0][i][j] = num_list[k];
                k++;
            }
    }
    else if(!strcmp(tmp_name, "MAT_F"))
    {
        for(i=0;i<SIZE;i++)
             for(j=0;j<SIZE;j++)
             {
                mats[5].mat[0][i][j] = num_list[k];
                k++;
             }
    }
    else
    {
        printf("No such matrix name.");
    }
}

私の general_structs.h ファイル:

#define SIZE 4
#define SIZE_NAME 5
#define SIZE_FUNC 10

typedef double matrix[SIZE][SIZE];

matrix MAT_A, MAT_B, MAT_C, MAT_D, MAT_E, MAT_F;

void read_mat(void);

struct 
{
    char name[SIZE_NAME];
    matrix *mat;
} mats[] = {
        {"MAT_A", &MAT_A},
        {"MAT_B", &MAT_B},
        {"MAT_C", &MAT_C},
        {"MAT_D", &MAT_D},
        {"MAT_E", &MAT_E},
        {"MAT_F", &MAT_F},
        {"non", NULL}
      };

struct
{
    char name[SIZE_FUNC];
    void (*func)(void);
} cmd[] = {
        {"read_mat", read_mat},
        {"not_valid", NULL}
      };

私のメイクファイル:

int_loop: my_math.o int_loop.o
    gcc -g -ansi -Wall -pedantic my_math.o int_loop.o -o int_loop

int_loop.o : int_loop.c
    gcc -c -ansi -Wall -pedantic int_loop.c -o int_loop.o

my_math.o : my_math.c
    gcc -c -ansi -Wall -pedantic my_math.c -o my_math.o

この問題をさまざまな手法で解決しようとしていますが、まだ成功していません。

私が受け取るエラーは次のとおりです。

gcc -g -Wall -ansi -pedantic main.o mat.o -o mamantest
mat.o:(.data+0x0): multiple definition of `mats'
main.o:(.data+0x0): first defined here
mat.o:(.data+0x70): multiple definition of `cmd'
main.o:(.data+0x70): first defined here
collect2: ld returned 1 exit status
make: *** [mamantest] Error 1

なぜこのエラーが発生するのですか? これを解決するにはどうすればよいですか?

ありがとう

4

1 に答える 1