源文件组织 [英] Source file organisation

查看:127
本文介绍了源文件组织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在组织我的源文件时遇到了一些麻烦。

I am having a bit of trouble organising my source files.

我有自己小的,但不断增长的代码集合,我想在各种项目中使用。文件和文件夹布局如下:

I have my own small, but growing collection of code that I would like to use in various projects. The file and folder layout is something like this:

library\sub1\source.h

library\sub1\source.cpp

library\sub2 \source.h

library\sub2\source.cpp

我的问题是,我想包括这个代码,如果需要,在我的其他项目。到目前为止,我已经使用绝对路径指向库的代码,但必须有一个更好的方法。

One of my problems is that I want to include this code, as needed, in my other projects. To date I have used absolute paths to point to the libary code, but there must be a better way.

另外,我需要添加每个库文件我使用到项目的文件Visual Studio为了它可以正确地编译。

Futhermore, I need to add every library file I use to a project's files Visual Studio in order for it to compile correctly.

所以我的问题简而言之是如何解决这个问题?

So my question in short is how do I fix this? What is the proper/best way to handle the above situation.

推荐答案

一般情况下,您不应该添加源文件库直接到其他项目。

You shouldn't, in general, add source files from libraries directly to other projects. Compile them separatly as a library and use those.

为了组织库的目录结构本身,现在我决定使用类似下面的结构:

For organising the library's directory structure itself, right now I settled on something like the following structure


  • library1 / widget.h

  • library1 / private / onlyinlib.h

  • library1 /private/widget.cpp

(如适用)


  • library1 / private / resources / widget.jpg

  • library1 / private / project / widget.xcode

我把所有头文件直接放在库路径中,并有一个子文件夹 private ,它将包含只由库使用的一切,共享/暴露。

I put all headers directly in the library path, and have a subfolder private which will contain everything that's only used by the library, but should never be shared / exposed.

最大的优点是每个项目我只需要一个指向包含我的库的目录的include路径,然后每个(public)include如

The greatest advantage is that every project I start only needs a include path pointing at the directory containing my libraries, then every (public) include is done like

#include "library1/widget.h"

私人包含只是

#include "onlyinlib.h"

这有许多优点:


  • 如果引入了新的库,就不会弄乱项目/编译器设置来获取头可见。

  • 移动到其他编译器/平台也非常简单。

  • 标题自动命名空间,即包含路径的一部分,它是不可能得到一个nameclash与包括

  • 立即显而易见的头部来自,如果头是公共接口的一部分

  • If new libraries are introduced, there's no messing with project /compiler settings to get the headers 'visible'.
  • Moving to other compilers / platforms is also very little hassle.
  • The headers are automatically 'namespaced', i.e. by including part of the path too, it's next to impossible to get a nameclash with the includes
  • It's immediatly obvious where a header comes from, and if a header is part of the public interface or not

这篇关于源文件组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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