如何做CMake模块 [英] How to do CMake modules

查看:97
本文介绍了如何做CMake模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用cmake学习c ++。我已经完成了一个项目,并且运行了cmake,可以编译,可以运行,很好,可以。现在,我开始了一个新项目,在其中我想使用第一个的某些类。我尝试阅读一些源代码,并且我了解我需要制作一个可以从两个应用程序中读取的模块。所以这将是我的存储库:

I try to learn c++ with cmake. I have done a project, and cmake runs, it compiles, it works, that is nice, ok. Now I started a new project in which I want to use some classes of the first one. I have tried to read some sourcecode and I have understand that I need to make a module that I can read from both the application. So this will be my repository:

/
/cmake
/modules/Network
/software/sw1
/software/sw2

两个项目 sw1 sw2 取决于模块 Netowrk 。在cmake文件夹中,必须有 FindNetwork.cmake 文件,在 sw1 中, sw2 Network 必须有 CMakeList.txt

both the projects sw1 and sw2 depends on the module Netowrk. In the folder cmake there has to be the FindNetwork.cmake file, and in sw1, sw2 and Network there has to be the CMakeList.txt .

但是。.我弄混了 include_directories 和其他cmake晦涩之处。

But.. I messed up with include_directories and other cmake obscurities..

有人可以给我指出一个很好的概述,如何使用依赖于通用模块的软件轻松组织存储库?

Can someone point me out to a nice overview how to easily organize a repository with softwares that depend on common modules?

推荐答案

此链接将为您提供一些示例。

This link will provide some examples for you.

就您对项目/基础架构的看法而言,最好不要混淆太多。因此,有两点可以帮助您入门(我希望)

In terms of how you are looking at the project/infrastructure then it's best to not confuse things too much. So here is a couple of points to get you started (I hope)


  • 在c ++中,模块是一个库(因此请参阅您的网络模块作为
    库)

  • 要包含库,您需要将其链接,并且还要使
    头文件可用。

  • In c++ a module is a library (so refer to your Network module as a library)
  • To include a library you need to link it and also make the header files available.

在cmake中,这是两个命令 target_link_libraries include_directories

In cmake this is two commands target_link_libraries and include_directories respectively.

考虑到这一点,项目结构可能是

With that in mind the project structure could be

/Network/include (api here)
/Network/src
/sw1/src
/sw2/src

并为您提供示例基础CmakeLists.txt文件:(放在项目的根目录中)

with an example base CmakeLists.txt file for you: (place in root dir of project)

cmake_minimum_required(VERSION 2.7) // your choice
project(myproject) // change name 
add_subdirectory(Network)
add_subdirectory(sw1)
add_subdirectory(sw2)

在网络目录中,您将拥有

in the Network Directory you would have this

add_library(Network net1.cc net2.cc etc.)

sw1目录

include_dirs(${MYPROJECT_SOURCE_DIR}/Network/include)
link_directories(${MYPROJECT_BINARY_DIR}/Network)
add_executable (sw1prog sw1.cc sw11.cc etc.)
target_link_libraries (sw1prog Network)

在sw2目录中

include_dirs(${MYPROJECT_SOURCE_DIR}/Network/include)
link_directories(${MYPROJECT_BINARY_DIR}/Network)
add_executable (sw2prog sw2.cc sw21.cc etc.)
target_link_libraries (sw2prog Network)

这是您可能需要的非常简化的版本,它消除了对FindXXModule.cmake文件的需要被创建并引用您隐式创建的库。我认为这是最适合您的机制,如果您确实想创建FindXXModule.cmake,那么我建议您将libs实际安装到计算机上,并希望其他人能够找到它,或者有一种机制用于多个项目相互链接的库。

This is a very simplified version of what you may require, it removes the need for a FindXXModule.cmake file to be created and refers to the library you create implicitly. I think this is the best mechanism for you, if you did want to create a FindXXModule.cmake then I would suggest it's when you actually install your libs to the machine and wish others to be able to find it, either that or have a mechanism for multiple projects to link to each other libraries.

我希望这会有所帮助,请记住 cmake网站中有一些示例, cmake --help 是您的朋友。

I hope this is a little useful, please bear in mind the cmake site has some examples and cmake --help is your friend.

这篇关于如何做CMake模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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