怎么办CMake的模块(How to do CMake modules)

2019-10-18 07:15发布

我尝试学习C ++通过CMake。 我曾经做过一个项目,cmake的运行,它编译,它的工作原理,这是好的,好的。 现在,我开始在想使用的第一个的某些类新项目。 我曾尝试阅读一些源代码,我已经明白,我需要做一个模块,我可以从两个应用程序读取。 因此,这将是我的仓库:

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

两个项目sw1sw2取决于模块Netowrk 。 在该文件夹的cmake必须有该FindNetwork.cmake文件,并在sw1sw2Network必须有在CMakeList.txt

但是..我搞砸了include_directories和其他cmake的晦涩..

可有人点我到一个很好的概述如何轻松地组织资源库和依赖于通用模块的软件?

Answer 1:

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)

  • 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.

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

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.)

In the sw1 dir

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)

In the sw2 dir

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)

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.

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



文章来源: How to do CMake modules
标签: c++ cmake