将项目拆分为库和应用程序

splitting a project into a library and a application

本文关键字:应用程序 项目 拆分      更新时间:2023-10-16

我使用cmake为我的项目。不,我想把一些部分分成一个库,并将其用于两个不同的应用程序。

现在我不知道如何在cmake中做这个子项目。我的第一个尝试是使用add_subdirectory命令:
cmake_minimum_required(VERSION 2.8)
add_subdirectory(MSI)
message("Building MsiQtWizard with: ${MSI_INCLUDE_DIR}")
add_subdirectory(MsiQtWizard)

那么MSI就是我的库。在MSI文件夹中是另一个cmakelists,它基本上是一个用于构建库的独立列表。我认为我可以使MsiQtWizard项目也是一个独立的cmakelists,所以我理论上可以构建MSI并使用库来构建MsiQtWizard(和其他项目)。

根目录中的cmakelist只是一个助手,可以一步构建库和GUI。

问题是,为了构建MsiQtWizard,我需要包含msi和静态库二进制文件的路径。我试着在MIS/CMakelists.txt:

末尾做一些类似的事情
### Set variables, other scripts may use ###
SET(MSI_INCLUDE_DIR include)
MESSAGE("Include directory is: ${MSI_INCLUDE_DIR}")

和在MsiQtWizard/CMakelists中:

##### external libraries #####
#MSI
find_path(MSI_INCLUDE_DIR REQUIRED msi/Image.hpp
            PATH_SUFFIXES MSI/include include)

我的意图是,如果之前没有设置变量,MsiQtWizard将搜索msi(以便您可以将此cmakelist作为独立使用)。当构建MSI时,我想保存包含路径(以及后来的二进制位置)并将其传递给MsiQtWizard -但是一旦我回到根cmakelist中,该值就消失了。

这就是我所尝试的。我现在的问题是:我如何正确地将我的项目分成一个库和一个(后来是多个)应用程序,我能以一种我也可以独立构建它们的方式做到这一点吗?

或者,更具体地说:我如何将值从节点CMakelist传递到根CMakelist(就像我尝试过MSI_INCLUDE_DIR)?

如果您构建一个库-最好将其与应用程序构建完全分离。否则,您将使用cmake将库与应用程序耦合在一起,在我看来,这违背了构建库的目的。

当你构建你的库时,你会想要像

这样的东西
project (MSILibrary)
ADD_LIBRARY(MSILibrary  src/MSI1.cpp src/MSI2.cpp)
install (TARGETS MSILibrary DESTINATION lib)

,其中src包含库代码。然后你可以make然后sudo make install你的库到你的标准库位置(例如/usr/lib)。

您可以在任何后续项目中使用您的库。把这些放到一个新目录中,并为它们创建一个新的CMakeLists.txt

您将需要像

这样的内容
#include find modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
project (MSI-project-1)
find_package(MSILibrary REQUIRED)
IF(MSILibrary_FOUND)
  include_directories(${MSILibrary_INCLUDE_DIRS} 
ENDIF(MSILibrary_FOUND )

target_link_libraries (MSI-project-1 ${MSILibrary_LIBRARIES})
install (TARGETS MSI-project-1 DESTINATION bin)

现在你需要做的就是帮助cmake找到你的库。您可以为此包含一个模块。在文件./cmake/Modules/FindMSILibrary.cmake中输入如下内容:

# - Try to find MSILibrary library
# Once done, this will define
#
#  MSILibrary_FOUND - system has MSILibrary
#  MSILibrary_INCLUDE_DIRS - the MSILibrary include directories
#  MSILibrary_LIBRARIES - link these to use MSILibrary
## Google this script (I think its fairly standard, but was not bundled with my CMAKE)   - it helps find the macros.
include(LibFindMacros)
# Dependencies
libfind_package(MSILibrary)
# Use pkg-config to get hints about paths
libfind_pkg_check_modules(MSILibrary_PKGCONF MSILibrary)
# Include dir
find_path(MSILibrary_INCLUDE_DIR
  NAMES MSI.hpp
  PATHS ${MSI_Library_PKGCONF_INCLUDE_DIRS} 
)
# Finally the library itself
find_library(MSILibrary_LIBRARY
  NAMES MSILibrary 
  PATHS ${MSILibrary_PKGCONF_LIBRARY_DIRS} 
)
# Set the include dir variables and the libraries and let libfind_process do the rest.
# NOTE: Singular variables for this library, plural for libraries this this lib depends on.
set(MSILibrary_PROCESS_INCLUDES MSILibrary_INCLUDE_DIR MSILibrary_INCLUDE_DIRS)
set(MSILibrary_PROCESS_LIBS MSILibrary_LIBRARY MSILibrary_LIBRARIES)
libfind_process(MSILibrary)

应该是这样。

编辑:

如果你真的想用你的库打包你的应用程序(也许是一些示例应用程序),那么你可以这样做:

在根目录CMakeLists.txt

cmake_minimum_required (VERSION 2.6)
project (MSIProject)
# The version number.
set (MSIProject_VERSION_MAJOR 0)
set (MSIProject_VERSION_MINOR 1)
set (MSIProject_PATCH_LEVEL 3 )
# project options
OPTION( BUILD_SHARED_LIBS "Set to OFF to build static libraries" ON )
OPTION( BUILD_EXAMPLES "Set to OFF to skip building the examples" ON )
# Put the libaries and binaries that get built into directories at the
# top of the build tree rather than in hard-to-find leaf
# directories.
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

##########################################################################
#  Build the library 
##########################################################################
add_subdirectory(MSI-src)
##################
#  Build your example Apps if requested
############

IF( BUILD_EXAMPLES )
  add_subdirectory(example/MSI-project-1)
  add_subdirectory(example/MSI-project-2)
ENDIF( BUILD_EXAMPLES )

你的库MSI-src/CMakeFiles.txt将和以前一样,你的example/MSI-project-1/CMakeLists.txt将类似于

## Make the InferData example project
project (MSI-project-1)
#include MSI library
include_directories ("${MSILibrary_SOURCE_DIR}/include")
#include the includes of this project
include_directories ("${MSI-project-1_SOURCE_DIR}/../include")
#build
add_executable(MSI-project-1  src/P1.cpp)
target_link_libraries (MSI-project-1 MSILibrary) #link
install (TARGETS MSI-project-1 DESTINATION bin)