简体   繁体   English

关于在Linux下组织源代码和构建C ++开发的建议(CMake as generator)

[英]Advice on organizing sources and builds in C++ development under Linux (CMake as generator)

Can someone propose some good practices for organizing your source files and managing builds when using C++ under Linux. 在Linux下使用C ++时,有人可以提出一些组织源文件和管理构建的好方法。 I use CMake to manage my builds although i do not use complex constructs at this point of time. 我使用CMake来管理我的构建,虽然我现在不使用复杂的构造。 Let us say we have the following three scenarios. 我们假设我们有以下三种情况。
1. For a makefile application to simply build a few executables from simple .cpp and .h files 1.对于makefile应用程序,只需从简单的.cpp和.h文件中构建一些可执行文件
2. For creating a static/shared library which uses other popular shared libraries say OpenCV and OpenGL, for example. 2.对于创建使用其他流行共享库的静态/共享库,例如OpenCV和OpenGL。
3. More complicated types, for example, let us say we need to create an executable whose source files use external libraries like OpenCV and also a custom static library that we have built ourselves(for example, a custom static library with associated headers that we built with step2 above). 3.更复杂的类型,例如,让我们说我们需要创建一个可执行文件,其源文件使用外部库(如OpenCV)以及我们自己构建的自定义静态库(例如,我们自己构建的自定义静态库)用上面的step2构建)。

I am sure many of you work on complicated library projects where the build process is not that simple. 我相信你们中的许多人都在复杂的库项目上工作,而构建过程并不那么简单。 I am really looking forward to amazing answers from open source enthusiasts and hackers who contribute to open source projects. 我真的很期待开源爱好者和黑客为开源项目做出贡献的惊人答案。 How do you guys organize your source code? 你们是如何组织源代码的?

Since you are using CMake I would suggest using out of source builds (Either completely outside or in a build subdirectory of the project root directory) When using more than one configuration and/or compiler at the same time you can create a separate build directory for each one. 由于您正在使用CMake,我建议使用源代码构建(完全在外部或在项目根目录的build子目录中)当同时使用多个配置和/或编译器时,您可以创建一个单独的构建目录每一个。

In the CMakeLists.txt in the project root directory I set up stuff that is to be used by all CMakeLists.txt files in the src subdirectory. 在项目根目录的CMakeLists.txt中,我设置了src子目录中所有CMakeLists.txt文件使用的东西。 I put all the sources for the executables and libraries in a src subdirectory and usually I group the sources that form a single library or executable inside their own subdirectory within src together with an accompanying CMakeLists.txt that describes how to build it. 我将可执行文件和库的所有源代码放在src子目录中,通常我将在src自己的子目录中构成单个库或可执行文件的源与一个描述如何构建它的CMakeLists.txt联。 I usually don't separate include files from the sources. 我通常不会从源中分离包含文件。

I also have a cmake subdirectory in the project root dir where i put files specific to CMake like find modules and in my case a special cmake module which fixed the paths that the Eclipse IDE autodiscovers. 我在项目根目录中也有一个cmake子目录,其中我将特定于CMake的文件放入查找模块,在我的例子中是一个特殊的cmake模块,它修复了Eclipse IDE自动发现的路径。

|--cmake
|  |
|  |--FindXXX.cmake
|
|--src
|  |
|  |--projectABC
|  |  |
|  |  |--CMakeLists.txt
|  |
|  |--library1
|  |  |
|  |  |--CMakeLists.txt
|  |
|  |--library2
|     |
|     |--CMakeLists.txt
|
|--CMakeLists.txt
|
|--build-release
|--build-debug
|--build-msvc-release
|--[...]

I suggest organizing sources by theme and having separate binary (or object) directories. 我建议按主题组织源并具有单独的二进制(或对象)目录。 Header files and source files in the same directory. 头文件和源文件在同一目录中。 One directory for each different compiler or platform: 每个不同编译器或平台的一个目录:

Fields
 |-- src
 |   field_int.hpp
 |   field_int.cpp
 |
 |-- obj_linux_gcc
 |   |
 |   |-- debug
 |   |
 |   |-- release
 |   
 |-- obj_windows_gcc
 |
 |-- obj_visual_studio

In recent years at various shops, I have found that separating header and source files into separate directories, without regards to theme, is a pain to maintain and also takes a long time to become acquainted with. 近年来在各个商店,我发现将头文件和源文件分成不同主题的单独目录是一件很难维护的事情,也需要很长时间才能熟悉。

"That's just my opinion, I could be wrong." “这只是我的意见,我可能是错的。” -- Dennis Miller, comedian. - 喜剧演员丹尼斯米勒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM