我在用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.c
和SeqList.c
,然后就得到了兩份SeqList.c
中的函數(shù)定義對(duì)應(yīng)的匯編/機(jī)器碼。
接著會(huì)鏈接,有兩組名字一樣的函數(shù)定義,自然就鏈接不上了……
解決方案就是另外做一個(gè)SeqList.h
放函數(shù)的聲明,然后#include
.h文件
Linux不會(huì)報(bào)錯(cuò)大概是編譯器的容錯(cuò)機(jī)制做的好吧……
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。