iT邦幫忙

0

c++的.h和cpp,放在相同目錄下好,還是頂層就用include,src分開好? [搬運/問答]

  • 分享至 

  • xImage
  •  

作者:icon-meh
鏈接:https://www.zhihu.com/question/8622503673/answer/72070024144
來源:知乎
著作權歸作者所有。

最開始學習的時候,直接照搬 Unix libraries 的組織方式:

./         Makefile and configure scripts.
./src      General sources
./include  Header files that expose the public interface and are to be installed
./lib      Library build directory
./bin      Tools build directory
./tools    Tools sources
./test     Test suites that should be run during a 'make test'

參考的是這個......

/usr/src      Sometimes contains sources for installed programs

/usr/include  Default include directory
/usr/lib      Standard library install path
/usr/share/projectname    Contains files specific to the project.

上班以後,接受部門培訓,老大要求 “分開,但不是盲目的按擴展名分,而是根據文件的 access”。比如有一個管理用戶信息的模塊,包含 User,UserChecker,程序的其它部分只需要知道 User,而 UserChecker 只用於 User 執行檢查,於是:

- include folder(Public folder)
    - user.h
- source folder (Private folder)
    - user.cpp
    - userchecker.h
    - userchecker.cpp

後來進項目組,發現每個小團體都有自己的風格,對這個 “部門規矩” 貫徹的並不好......

然後我師父說:

不要教條,要考慮項目的規模,要權衡一堆周邊的事兒(開發環境、構建工具、源碼控制),然後選擇一個適合自己(團隊)的即可......

------ 以前覺得師父很高大很嚴謹,後來發現師父其實很 “潦草”,教的時候是一套(“總是很有道理” 那套),乾的時候是另一套(“怎麼省事兒怎麼來” 那套)...... 對不住了,師父大人!

後來換了公司,發現有你這種的:

project/
|-- include/ 
|   |-- deepx/
|       |-- loss/
|           |-- cross_entropy.hpp
|-- src/
    |—- deepx/
         |-- 1oss/
              |-- cross_entropy.cpp

也有跟你另外那種差不多的:

- project/
    - src/
        -deepx/
            - loss/
                cross_entropy.hpp
                cross_entropy.cpp

後來還參考過這一套:
As an example, the layout of a simple libhello library could look like this:

libhello/
|— libhello/
|   |- hello.hpp
|   |- hello. cpp
|   |- hello.test.cpp
|- tests/
    |- basics/
        |- driver. cpp

突出一個 “在一起”......

這麼搞,有啥說法不?

有興趣的自己看吧 ------ Canonical Project Structure

https://ithelp.ithome.com.tw/upload/images/20251213/201699534i7iGTBv2A.jpg

我也不好說..... 反正就是某一天,一個碼農頭子把這篇文章發群里了,然後組織小夥伴們開了個會......


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言