c++ - 如何将变量传递给 cmake 脚本
问题描述
我正在尝试将变量传递给 cmake 脚本,但显然我没有正确执行它。我正在尝试按照教程构建一个计时项目。我正在关注的教程在这里:
https://api.projectchrono.org/tutorial_install_project.html
CMakeLists.txt 修改如下:
#--------------------------------------------------------------
#
# Example of CMake configuration file to build an external
# project depending on Chrono and on optional Chrono modules.
#
# This minimal sample project can be used as a template for a
# user project. Modify sections 1, 2, and 3 below as appropriate.
#
#--------------------------------------------------------------
cmake_minimum_required(VERSION 2.8)
#--------------------------------------------------------------
# === 1 ===
# Modify the project name if you want:
#--------------------------------------------------------------
project(my_project)
#--------------------------------------------------------------
# === 2 ===
# Find the Chrono package and any REQUIRED or OPTIONAL modules
# by invoking the find_package function in CONFIG mode:
find_package(Chrono
COMPONENTS Irrlicht
CONFIG)
# The following Chrono modules can be requested (their names
# are case insensitive): Cascade, Cosimulation, FEA, Irrlicht,
# Matlab, Parallel, Postprocess, Python, Vehicle.
#
# Note that you will have to set the variable Chrono_DIR to
# specify the location of the ChronoConfig.cmake script, if
# it is not in its default install location.
# Chrono_DIR can be either a Chrono build tree or a Chrono install tree.
#
# The following variables are set and can be used further down:
# CHRONO_FOUND
# set to true if Chrono and all required components were found
# CHRONO_C_FLAGS
# CHRONO_CXX_FLAGS
# C and C++ compilation flags
# CHRONO_INCLUDE_DIRS
# additional paths for included headers
# CHRONO_LIBRARIES
# list of required libraries (with full path)
# CHRONO_LINKER_FLAGS
# additional linker flags
# CHRONO_DLLS
# list of all DLL dependencies (with full path)
# CHRONO_DATA_DIR
# path to the Chrono data make_directory
#
# In addition, for each requested component [COMPONENT], the
# following variable is set to true (ON) or false (OFF):
# CHRONO_[COMPONENT]_FOUND
#
# In this example, we only request the Irrlicht module (required)
#--------------------------------------------------------------
#Chrono_DIR = "/home/richard/chrono_build/lib"
LIST(APPEND CMAKE_PREFIX_PATH "${CMAKE_INSTALL_PREFIX}/../Chrono/lib")
find_package(Chrono
COMPONENTS Irrlicht
CONFIG)
#--------------------------------------------------------------
# Return now if Chrono or a required component was not found.
#--------------------------------------------------------------
if (NOT Chrono_FOUND)
message("Could not find Chrono or one of its required modules")
return()
endif()
#--------------------------------------------------------------
# Enable creation of "application bundles" on MacOSX.
#--------------------------------------------------------------
# This is necessary for any Irrlicht-based project (like the example here).
# For OpenGL-based or non-graphics projects, this is optional and the block
# below can be removed (or else explcitly set CMAKE_MACOSX_BUNDLE to 'OFF').
#
# If creating application bundles, the build output will be named 'myexe.app'.
# Use the convenience script 'run_app.sh' available under 'contrib/appbundle-macosx/'
# to run:
# start_demo.sh myexe.app
if(APPLE)
set(CMAKE_MACOSX_BUNDLE ON)
endif()
#--------------------------------------------------------------
# Add path to Chrono headers and to headers of all dependencies
# of the requested modules.
#--------------------------------------------------------------
include_directories(${CHRONO_INCLUDE_DIRS})
#-----------------------------------------------------------------------------
# Fix for VS 2017 15.8 and newer to handle alignment specification with Eigen
#-----------------------------------------------------------------------------
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
if(MSVC AND ${MSVC_VERSION} GREATER_EQUAL 1915)
add_definitions( "-D_ENABLE_EXTENDED_ALIGNED_STORAGE" )
endif()
endif()
#--------------------------------------------------------------
# Tweaks to disable some warnings with MSVC
#--------------------------------------------------------------
if(MSVC)
add_definitions("-D_CRT_SECURE_NO_DEPRECATE") # avoids deprecation warnings
add_definitions("-D_SCL_SECURE_NO_DEPRECATE") # avoids deprecation warnings
add_definitions( "-DNOMINMAX" ) # do not use MSVC's min/max macros
set(EXTRA_COMPILE_FLAGS "/wd4275") # disable warnings triggered by Irrlicht
else()
set(EXTRA_COMPILE_FLAGS "")
endif()
#--------------------------------------------------------------
# === 3 ===
# Add the executable from your project and specify all C++
# files in your project.
#--------------------------------------------------------------
add_executable(myexe my_example.cpp)
#--------------------------------------------------------------
# Set properties for your executable target
#
# Note that here we define a macro CHRONO_DATA_DIR which will
# contain the path to the Chrono data directory, either in its
# source tree (if using a build version of Chrono), or in its
# install tree (if using an installed version of Chrono).
#--------------------------------------------------------------
set_target_properties(myexe PROPERTIES
COMPILE_FLAGS "${CHRONO_CXX_FLAGS} ${EXTRA_COMPILE_FLAGS}"
COMPILE_DEFINITIONS "CHRONO_DATA_DIR=\"${CHRONO_DATA_DIR}\""
LINK_FLAGS "${CHRONO_LINKER_FLAGS}")
#--------------------------------------------------------------
# Link to Chrono libraries and dependency libraries
#--------------------------------------------------------------
target_link_libraries(myexe ${CHRONO_LIBRARIES})
#--------------------------------------------------------------
# === 4 (OPTIONAL) ===
#
# Optionally, add a custom command for copying all Chrono and
# dependency DLLs to the appropriate binary output folder.
# This function has effect only on Windows.
#
# Note that you must first set EXECUTABLE_OUTPUT_PATH
# (this can simply be ${CMAKE_BINARY_DIR}, like in this example)
#--------------------------------------------------------------
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
add_DLL_copy_command("${CHRONO_DLLS}")
我正在使用 Ubuntu 20.04,并尝试使用 cmake 设置构建。我正在使用的命令是:
cmake -S template_project/ -B build -Chrono_DIR:STRING=/home/richard/chrono_build/lib
我得到的错误是:
home/richard/chrono_build/lib
loading initial cache file hrono_DIR:STRING=/home/richard/chrono_build/lib
CMake Error: Error processing file: /home/richard/Chrono_workspace/hrono_DIR:STRING=/home/richard/chrono_build/lib
-- Could NOT find Chrono (missing: Chrono_DIR)
-- Could NOT find Chrono (missing: Chrono_DIR)
Could not find Chrono or one of its required modules
-- Configuring incomplete, errors occurred!
See also "/home/richard/Chrono_workspace/build/CMakeFiles/CMakeOutput.log".
显然,这不是正确设置“Chrono_Dir”的方法,因为我遇到了错误。使用 CMake 命令时如何传递该信息,还是需要将其添加到 cmakelists.txt 文件中?我知道这是一个新手问题,但我被困住了,需要一些帮助。我尝试关注这个 stackoverflow question,但我显然不明白发生了什么。
编辑:
我尝试下面的评论/答案是这样做的:
cmake -S template_project/ -B build -DChrono_DIR=/home/richard/chrono_build/lib
现在输出是:
-- Could NOT find Chrono (missing: Chrono_DIR)
-- Could NOT find Chrono (missing: Chrono_DIR)
Could not find Chrono or one of its required modules
-- Configuring done
-- Generating done
-- Build files have been written to: /home/richard/Chrono_workspace/build
解决方案
也许你可以试试:
cmake -S template_project/ -B build -DChrono_DIR=/home/richard/chrono_build/lib
cmake -D :创建或更新 CMake CACHE 条目。
然后你可以像这样在 CMakeList.txt 中访问它:
message("Chrono_DIR=" ${Chrono_DIR})
推荐阅读
- r - 用 Sys.time() 替换大型 POSIXct 中的 NA
- java - 如何将数组字符串与int值进行比较
- neo4j - Neo4j:查找与列表中所有节点有连接的节点
- spring-batch - 项目阅读器中的 Spring Batch 作业参数
- python - scikit KernelDensity 返回平面密度
- sql - 使用 left() 函数的左外连接
- c++ - 使用 TinyCbor C 库对 uint8_t 进行编码和解码
- c++ - 用特征计算两条线的交点
- reactjs - 完全没有路由的反应 SPA
- c# - 在 Visual Studio 7.5 mac 中找不到指定的框架“Microsoft.AspNetCore.App”,版本“2.1.0”