将项目拆分为库和应用程序
splitting a project into a library and a application
我使用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)
- VS2017 中缺少 Win32 控制台应用程序.如何创建C++空项目?
- 如何将此项目更改为 OpenCV 实时人脸检测应用程序?
- 在 C++/CLI/C# 项目中启用"Native Code Debugging"导致应用程序崩溃
- 将 Dr. Memory 与 Visual Studio 项目配合使用时出错:缺少应用程序所需的库
- 无法编译QT创建快速应用程序项目
- 从 WebAPI 项目运行静态类时出现 StackOverflow 异常 - 从控制台应用程序运行时工作正常
- 将函数从控制台应用程序移动到共享库项目似乎会带来不相关的编译错误
- 如何将解决方案中的项目从应用程序更改为静态库
- 如何在 silverlight 应用程序中引用 C++/CLI 项目
- 如何将boost.asio添加到windows通用应用程序项目中
- 如何将MFC应用程序项目添加到Visual C 2008中的Win32应用程序项目
- 一个解决方案,两个项目:如何从 Windows 应用程序调用控制台项目?C++
- 在Visual Studio 2010中将控制台项目转换/移植到无形应用程序
- 如何在应用程序项目中使用android原生代码
- 用C++创建一个写作应用程序(我的第一个项目)(学习C++)
- 如何从终端为本机应用程序创建android项目
- 使用Visual Studio c++的Windows窗体应用程序,该应用程序链接到CUDA项目
- 引用Windows应用商店应用程序项目中的C++WinRT组件
- visual 无法在控制台应用程序项目中包含头文件C++
- C/ c++应用程序项目(NetBeans)中的多个源文件