用C構建項目的問題 (Problem with building project in C)


問題描述

用C構建項目的問題 (Problem with building project in C)

I am doing a project in C and I have a problem with building it. It is:

I have two separate sub systems[A and B] in my project that use the functionality of yet another sub system[C]. To do this, they #include the header files necessary. The obligation is that both the sub systems[A and B] have to be built separately, by which I mean that I have two separate 'Makefile's for the subsystems and I run 'make' on each separately. 

Now, when I try to unite all my subsystems[A,B,C] into one single project, gcc tells that some functions are already declared and these functions belong to subsystem C. 

I realize that header files are included at compile time, i.e., when I use 'make' on the sub systems[A and B]. So, when I am trying to unite all of them, they are actually being double declared.

Can someone help me with a solution to this? Any solution, that does not require me to compile both the sub systems[A and B] together will be good.

‑‑‑‑‑

參考解法

方法 1:

Well this is either a compile error or a linker error.

If it's a compile‑time error, you need to use some include guards in your .h files like this:

#ifndef FILE_NAME_H
#define FILE_NAME_H

// file contents here

#endif

This will stop a file being included more than once. Change FILE_NAME_H to reflect the file name so each include guard is unique.

If it's a link‑time error, this usually happens when you have function bodies (i.e. the {}) in the header files. Because multiple files will include the header, each will have its own definition in the object code which will cause the linker conflict.

The best way to get around this is to simply make sure all function bodies go in the .c files. You can try and __inline__ them if they are short enough, but the compiler isn't guaranteed to listen to you.

方法 2:

You can use #ifndef .. to avoid double inclusion of header file

#ifndef <some name identifying the header file>
#define <some name identifying the header file>
//
//

// header file content will go here 

//

// 
#endif

方法 3:

Does you C‑header file include only the declaration or also the implementation?

If it contains implementation, then you should take it out and compile it separately.

(by Bhakta NallMike WellerXinusTristram Gräbener)

參考文件

  1. Problem with building project in C (CC BY‑SA 3.0/4.0)

#project-management #C






相關問題

項目規劃,開發人員筆記工具 (Project planning, Note taking tool for developers)

你經歷過的最糟糕的項目失敗是什麼? (What is the worst project failure you've ever been on?)

用C構建項目的問題 (Problem with building project in C)

XCode 項目詳情? (XCode Project Details?)

您需要什麼技能來在Web Apps中進行正確的UI /交互/功能設計? (What skills do you need for proper UI/Interaction/Functional design in Web Apps?)

“變更管理”結束和“項目失敗”從哪裡開始? (Where does "Change Management" end and "Project Failure" begin?)

Subversion 存儲庫統計信息,不是 StatSVN? (Subversion repository statistics, other than StatSVN?)

對於企業 Web 應用程序,推薦的支持技術人員與開發人員的比例是多少? (What is a recommended support technician-to-developer ratio for an enterprise web application?)

將生產力提高到每人/天 1 個錯誤修正 (Improving productivity to 1 Bug correction per man/day)

學習從頭開始創建 Rails 應用程序? (Learning to create a Rails application from scratch?)

您使用項目日記或經驗數據庫嗎? (Do you use a project diary or experience database?)

誰應該編寫 dockerfile、SRE 或開發人員? (Who should write the dockerfile, SRE or developer?)







留言討論