簡體   English   中英

如何在gcc linux中編譯多個.c和.h文件?

[英]How to compile multiple .c and .h files in gcc linux?

所以我有一個源mainClass.c,我有主要的定義。 我有一個頭文件class1.h和class1.c中class1.h中定義的所有函數的實現。 我在class1.h中有兩個變量(全局),名為cond和mutex,現在正在class1.c中使用,可能在將來我也將在mainClass.c中使用它。 現在編譯所有源文件以生成一個目標文件我正在做如下的事情:

gcc -Wall -pthread -I/home/2008/ariarad/mainClass1 mainClass1.c class1.c -o out

/ home / 2008 / ariarad / mainClass1是我的所有頭文件和源文件所在的位置,我在其中一個.c文件中使用pthead.h。 即使我把它包括在那里它抱怨所以我必須包括它。

現在當我運行上面的命令時,我得到以下錯誤:

class1.c:3:16: error: redefinition of ‘cond’
class1.h:66:16: note: previous definition of ‘cond’ was here
class1.c:4:17: error: redefinition of ‘mutex’
class1.h:67:17: note: previous definition of ‘mutex’ was here

以防我在class1.h周圍有一個ifndef和endif塊以避免多次包含。 我絕對不會重新定義.c文件中頭文件中定義的變量。 那么有人可以幫助我為什么它仍然給我錯誤?

您無法在頭文件中定義全局變量。 您必須在其中一個.c文件中定義它們,然后在頭文件中使用extern

在其中一個.c文件中:

int cond;

在其中一個.h文件中,必須包含所有需要該變量的.c文件:

extern int cond;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM