我在用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.c和SeqList.c,然后就得到了兩份SeqList.c中的函數(shù)定義對應的匯編/機器碼。
接著會鏈接,有兩組名字一樣的函數(shù)定義,自然就鏈接不上了……
解決方案就是另外做一個SeqList.h放函數(shù)的聲明,然后#include .h文件
Linux不會報錯大概是編譯器的容錯機制做的好吧……
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內(nèi)教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學校辦產(chǎn)業(yè)為響應國家深化產(chǎn)教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復興的升級產(chǎn)業(yè)鏈。利用北京大學優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓領域的先行者
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。