鍍金池/ 問(wèn)答/C  網(wǎng)絡(luò)安全/ C在編譯的時(shí)候顯示函數(shù)命名重復(fù)

C在編譯的時(shí)候顯示函數(shù)命名重復(fù)

我在用c寫程序,標(biāo)準(zhǔn)是C99,編譯器是gcc。用的是windows下的Clion
我有三個(gè)文件:main.c SeqList.c define.h
在程序中我會(huì)定義結(jié)構(gòu)體并且定義的結(jié)構(gòu)體可能出現(xiàn)在任意一個(gè)源文件中,所以我的想法是在define.c中定義,然后供各個(gè)源文件使用。下面的代碼是define.c中的全部代碼

#ifndef DATA_STRUCTURE_DEFINE_H
#define DATA_STRUCTURE_DEFINE_H
#define MAX 100
//線性表的順序存儲(chǔ)結(jié)構(gòu)
typedef struct
{
    //指向第一個(gè)節(jié)點(diǎn)的指針
    int *firstNode;
    //表當(dāng)前的長(zhǎng)度
    int length;
    //表最大的長(zhǎng)度
    int size;
}SeqList, *PSeqList;
#endif //DATA_STRUCTURE_DEFINE_H

我想在SeqList.c中使用define.c,下面是我的SeqList.c的代碼

#include "define.h"
#include <stdio.h>
#include <stdlib.h>

/**
 * 初始化
 * @param p
 * @return
 */
int initial(PSeqList p)
{
    p->length = 0;
    p->size = MAX;
    p->firstNode = (int *)malloc(sizeof(int)*(p->size));
}

/**
 * 創(chuàng)建
 * @param p
 * @return
 */
int create(PSeqList p)
{
    int i, length;
    int *index = p->firstNode;

    if(p->firstNode == NULL){
        printf("Invalid list\n");
        exit(-1);
    }

    printf("Input the length of the node\n");
    scanf("%d", &length);

    for(i = 0; i < length; i++){
        printf("Input the node value\n");
        scanf("%d", index++);
    }
}

下面的是main.c的代碼

#include <stdio.h>
#include "SeqList.c"

int main()
{
    SeqList seqList;

    initial(&seqList);
    create(&seqList);
}

然后我點(diǎn)了Clion中的運(yùn)行按鈕,如下圖
圖片描述

但是報(bào)錯(cuò)了,如下
C:Program FilesJetBrainsCLion 2017.3bincmakebincmake.exe" --build D:phpStudyWWWdata-structurecmake-build-debug --target data_structure -- -j 2
Scanning dependencies of target data_structure
[ 33%] Building C object CMakeFiles/data_structure.dir/main.c.obj
[ 66%] Building C object CMakeFiles/data_structure.dir/SeqList.c.obj
[100%] Linking C executable data_structure.exe
CMakeFilesdata_structure.dir/objects.a(SeqList.c.obj): In function `initial':
D:/phpStudy/WWW/data-structure/SeqList.c:11: multiple definition of `initial'
CMakeFilesdata_structure.dir/objects.a(main.c.obj):D:/phpStudy/WWW/data-structure/SeqList.c:11: first defined here
CMakeFilesdata_structure.dir/objects.a(SeqList.c.obj): In function `create':
D:/phpStudy/WWW/data-structure/SeqList.c:23: multiple definition of `create'
CMakeFilesdata_structure.dir/objects.a(main.c.obj):D:/phpStudy/WWW/data-structure/SeqList.c:23: first defined here
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: * [CMakeFilesdata_structure.dirbuild.make:122: data_structure.exe] Error 1
mingw32-make.exe[2]: * [CMakeFilesMakefile2:67: CMakeFiles/data_structure.dir/all] Error 2
mingw32-make.exe[1]: * [CMakeFilesMakefile2:79: CMakeFiles/data_structure.dir/rule] Error 2
mingw32-make.exe: * [Makefile:117: data_structure] Error 2

不知道是哪里做的不對(duì),求大神指教。PS:我在Linux的虛擬機(jī)下執(zhí)行沒(méi)有任何問(wèn)題

回答
編輯回答
六扇門

一般情況下不要#include.c文件?。?!
在編譯的時(shí)候,首先工作的是預(yù)處理器,預(yù)處理器將#include展開成對(duì)應(yīng)文件的內(nèi)容,這里你的SeqList.c文件里的函數(shù)定義就會(huì)被放進(jìn)main.c中。
接著編譯器會(huì)實(shí)際編譯被預(yù)處理器處理過(guò)的main.cSeqList.c,然后就得到了兩份SeqList.c中的函數(shù)定義對(duì)應(yīng)的匯編/機(jī)器碼。
接著會(huì)鏈接,有兩組名字一樣的函數(shù)定義,自然就鏈接不上了……
解決方案就是另外做一個(gè)SeqList.h放函數(shù)的聲明,然后#include .h文件
Linux不會(huì)報(bào)錯(cuò)大概是編譯器的容錯(cuò)機(jī)制做的好吧……

2017年5月20日 14:02