C ++项目源代码布局 [英] C++ project source code layout

查看:220
本文介绍了C ++项目源代码布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

组织项目目录的一种流行方式或多或少是这样的:

One of the popular way to organize project directory is more or less like this:


MyLib
     +--mylib_class_a.h
        mylib_class_a.cpp
        mylib_library_private_helpers.h
        mylib_library_private_helpers.cpp

MyApp
     +--other_class.h
        other_class.cpp
        app.cpp

app.cpp

#include "other_class.h"
#include <mylib_class_a.h> // using library MyLib

全部 .h .cpp 文件位于同一目录中。为了避免名称冲突,文件名通常用公司名称和/或库名称前缀。 MyLib将在MyApp的标题搜索路径等。我不是前缀文件名的粉丝,但我喜欢看看 #include 的想法,知道确切地在哪里头文件所属。我不喜欢这种组织文件的方法,但我认为应该有一个更好的方法。

All .h and .cpp files for the same library are in the same directory. To avoid name collision, file names are often prefix with company name and/or library name. MyLib will be in MyApp's header search path, etc. I'm not a fan of prefixing filenames, but I like the idea of looking at the #include and know exactly where that header file belongs. I don't hate this approach of organizing files, but I think there should be a better way.

因为我开始一个新的项目,我想征求一些目录组织思想。目前我喜欢这个目录结构:

Since I'm starting a new project, I want to solicit some directory organization ideas. Currently I like this directory structure:


ProjA
    +--include
             +--ProjA
                    +--mylib
                           +--class_a.h
                    +--app
                           +--other_class.h
    +--src
         +--mylib
                +--class_a.cpp
                   library_private_helpers.h
                   library_private_helpers.cpp
         +--app
              +--other_class.cpp
                 app.cpp
                 util.h

app.cpp

#include "util.h" // private util.h file
#include <ProjA/app/other_class.h> // public header file
#include <ProjA/mylib/class_a.h> // using class_a.h of mylib
#include <other3rdptylib/class_a.h> // class_a.h of other3rdptylib, no name collision
#include <class_a.h> // not ProjA/mylib/class_a.h
#include <ProjA/mylib/library_private_helpers.h> // error can't find .h

.cpp 文件和私有(只对立即库可见) .h 文件存储在src目录下(src有时称为lib)。公共头文件被组织成一个project / lib目录结构,并通过< ProjectName / LibraryName / headerName.h> 包括。文件名不带任何前缀。如果我需要打包MyLib以供其他团队使用,我可以简单地更改我的makefile来复制相应的二进制文件和整个include / ProjA目录。

.cpp files and private (only visible to immediate library) .h files are stored under the src directory (src is sometimes called lib). Public header files are organized into a project/lib directory structure and included via <ProjectName/LibraryName/headerName.h>. File names are not prefixed with anything. If I ever needed to package up MyLib to be used by other teams, I could simply change my makefile to copy the appropriate binary files and the whole include/ProjA directory.

一旦文件被检查到源代码控制,并且人们开始在它们上工作,将很难改变目录结构。它是更好地得到它正确的开始。

Once files are checked into source control and people start working on them it will be hard to change directory structure. It is better to get it right at the get-go.

有组织这样的源代码经验的人吗?你不喜欢什么吗?

Anyone with experience organizing source code like this? Anything you don't like about it? If you have a better way to do it, I would very much like to hear about it.

推荐答案

好吧,这一切都取决于这些项目有多大。

Well, it all depends on how big these projects are. If you've only got a few files, then whack them all in one folder.

如果您没有多少文件需要管理,在我看来太多文件夹过度。当你只有几个文件时,它会让你恼火的进入和退出文件夹。

Too many folders when you haven't got many files to manage is in my opinion overkill. It gets annoying digging in and out of folders when you've only got a few files in them.

此外,这取决于谁使用这个东西。如果你正在写一个库并且它将被其他程序员使用,那么最好将它们想要使用的头文件组织到一个include文件夹中。如果你创建了一些库并发布它们,那么你的结构可能会工作。但是,如果他们是独立的库,并且开发不是一起完成,并且他们在不同的时间进行版本化和发布,你最好坚持让一个项目的所有文件在一个文件夹中可定位。

Also, it depends on who's using this stuff. If you're writing a library and its going to be used by other programmers, then it's good to organize the headers they want to use into an include folder. If you're creating a number of libraries and publishing them all, then your structure might work. But, if they're independent libraries, and the development isn't all done together and they get versioned and released at different times, you'd be better off sticking with having all files for one project locatable within one folder.

事实上,我会说,把一切都保存在一个文件夹中,直到你找到它的unmanagable,然后重新组织成一个聪明的方案,将源分成文件夹你做了。你可能知道如何从你遇到的问题中组织它。

In fact, I would say keep everything in one folder, until you get to a point where you find its unmanagable, then reorganize into a clever scheme of dividing the source up into folders like you've done. You'll probably know how it needs to be organized from the problems you run into.

KISS通常总是编程中的解决方案 - >保持一切尽可能简单。

KISS is usually always the solution in programming -> keep everything as simple as possible.

这篇关于C ++项目源代码布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆