C++ 如何将变量传递给cmake脚本
我试图将一个变量传递给cmake脚本,但显然我做得不正确。我正试图通过遵循教程来构建一个chrono项目。下面是我的教程: CMakeLists.txt修改如下:C++ 如何将变量传递给cmake脚本,c++,cmake,configuration,C++,Cmake,Configuration,我试图将一个变量传递给cmake脚本,但显然我做得不正确。我正试图通过遵循教程来构建一个chrono项目。下面是我的教程: CMakeLists.txt修改如下: #-------------------------------------------------------------- # # Example of CMake configuration file to build an external # project depending on Chrono and on opt
#--------------------------------------------------------------
#
# 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}")
我正在使用Ubuntu20.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,但我显然不明白发生了什么
编辑:
我尝试了下面的评论/答案:
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缓存项
然后您可以在CMakeList.txt中访问它,如下所示:
message("Chrono_DIR=" ${Chrono_DIR})
也许你可以试试:
cmake -S template_project/ -B build -DChrono_DIR=/home/richard/chrono_build/lib
cmake-D:创建或更新cmake缓存项
然后您可以在CMakeList.txt中访问它,如下所示:
message("Chrono_DIR=" ${Chrono_DIR})
您忘记了变量名之前的-D。尝试使用cmake-S template_project/-B build--DChrono_DIR:STRING=/home/richard/chrono_build/libtry-DChrono_DIR代替-chrono_DIR@AliRazmkhah有点进一步,但仍然不太工作更新了我的问题上面。Chrono_DIR仍然缺少描述:“Chrono_DIR可以是Chrono构建树或Chrono安装树。”。那么,您为
chrono\u DIR
变量指定的值(/home/richard/chrono\u build/lib
)是生成树还是安装树?根据目录命名,它闻起来像构建树是/home/richard/chrono\u build
,您将chrono\u DIR
设置到它的lib/
子目录,这与描述相矛盾。@Tsyvarev按照您的建议做了。仍然得到相同的输出。该文档还声明“如果ChronoConfig.cmake脚本不在其默认安装位置,则必须设置变量Chrono_DIR以指定ChronoConfig.cmake脚本的位置。”my Chrono Build中没有ChronoConfig.cmake脚本。。奇怪的我想你还得多上网看看。你忘了变量名前的-D。尝试使用cmake-S template_project/-B build--DChrono_DIR:STRING=/home/richard/chrono_build/libtry-DChrono_DIR代替-chrono_DIR@AliRazmkhah有点进一步,但仍然不太工作更新了我的问题上面。Chrono_DIR仍然缺少描述:“Chrono_DIR可以是Chrono构建树或Chrono安装树。”。那么,您为chrono\u DIR
变量指定的值(/home/richard/chrono\u build/lib
)是生成树还是安装树?根据目录命名,它闻起来像构建树是/home/richard/chrono\u build
,您将chrono\u DIR
设置到它的lib/
子目录,这与描述相矛盾。@Tsyvarev按照您的建议做了。仍然得到相同的输出。该文档还声明“如果ChronoConfig.cmake脚本不在其默认安装位置,则必须设置变量Chrono_DIR以指定ChronoConfig.cmake脚本的位置。”my Chrono Build中没有ChronoConfig.cmake脚本。。奇怪的我想我还得在网上多看些东西。我想还得再看一点,但还是不太管用。更新了我上面的问题。Chrono_DIR仍然是missing@Dick这确实解决了你的问题。您的设置有其他问题这一事实与此无关。用您遇到的下一个问题更新问题不是解决此问题的方法。标记为已接受,并为下一道题写一个新问题。每个问题一个问题。有关SO的规则和指导原则的更多信息,请查看。@Dick您可以在CMakeList.txt中访问它,如下所示:消息(“Chrono_DIR=“${Chrono_DIR}”)更进一步,但仍然不太有效更新了我上面的问题。Chrono_DIR仍然是missing@Dick这确实解决了你的问题。您的设置有其他问题这一事实与此无关。用您遇到的下一个问题更新问题不是解决此问题的方法。标记为已接受,并为下一道题写一个新问题。每个问题一个问题。有关SO的规则和指南的更多信息,请查看。@Dick您可以在CMakeList.txt中访问它,如下所示:message(“Chrono_DIR=“${Chrono_DIR})