在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

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

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

#ifndef DATA_STRUCTURE_DEFINE_H
#define DATA_STRUCTURE_DEFINE_H
#define MAX 100
//線性表的順序存儲結(jié)構(gòu)
typedef struct
{
    //指向第一個節(jié)點的指針
    int *firstNode;
    //表當前的長度
    int length;
    //表最大的長度
    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);
}

然后我點了Clion中的運行按鈕,如下圖
圖片描述

但是報錯了,如下
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

不知道是哪里做的不對,求大神指教。PS:我在Linux的虛擬機下執(zhí)行沒有任何問題

回答
編輯回答
六扇門

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

2017年5月20日 14:02