使用带有C ++继承的CMake时如何组织目录结构?

问题描述 投票:0回答:1

目前,我有一个如下所示的目录结构

.
├── CMakeLists.txt
├── Base
│   ├── CMakeLists.txt
│   ├── include
│   │   └── base.h
│   └── src
│       └── base.cpp
├── Derived
│   ├── CMakeLists.txt
│   ├── include
│   │   └── derived.h
│   └── src
│       └── derived.cpp
└── src
    └── main.cpp

CMakeLists.txt文件如下

./ CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

set(CMAKE_CXX_STANDARD 11)

project(MyProj)

add_subdirectory(Base)
add_subdirectory(Derived)

add_executable(main src/main.cpp)
target_link_libraries(main Base)
target_link_libraries(main Derived)

./ Base / CMakeLists.txt

add_library(Base STATIC src/base.cpp)
target_include_directories(Base PUBLIC include)

./ Derived / CMakeLists.txt

add_library(Derived STATIC src/derived.cpp)
target_include_directories(Derived PUBLIC include)
target_link_libraries(Derived Base)

我想知道在C ++中使用继承时,这是否是构造CMake项目的合适方法。如果有一种更惯用的方式来构造它,我欢迎提出建议。

c++ cmake directory-structure
1个回答
0
投票

为每个类创建一个库是过大的,可能会减慢某些构建系统上的编译速度。

在我的某些项目中,我有300多种类型,不包括模板和lambda。我无法想象为每个类创建一个库。

我想知道在C ++中使用继承时这是否是构造CMake项目的合适方法

您正在使用的功能不应更改代码的物理组织方式。相反,您的文件布局应以代码的逻辑自包含部分为基础,并且各部分之间具有明确的依赖性。

唯一的选择:使用此表单将库链接在一起:

target_link_libraries(main PUBLIC Base) # or private

您的其余CMake都充分利用了基于目标的API。我唯一要更改的就是在构成库的方面不那么精细。

© www.soinside.com 2019 - 2024. All rights reserved.