使用ROS2环境变量创建C ++项目

问题描述 投票:1回答:1

我有以下问题:我编写了一个简单的ROS2 / Qt5(机器人操作系统)项目来展示GUI中的发布者和订阅者。项目编译得很好,在将所有Qt5和ROS dll复制到可执行文件的目录后,应用程序启动但然后快速关闭/崩溃而不会给我任何错误。

尝试2:打开控制台并获取ROS2安装源(通过运行批处理脚本,我认为将环境变量加载到控制台中,对吧?)。如果我现在从ROS2控制台中启动可执行文件,一切正常。

所以我的假设是,当我编译我的项目并尝试启动它时,它会丢失所有环境变量然后崩溃。

有没有办法让我可以避免启动ROS2控制台?

用于获取ROS2安装的批处理脚本对我来说似乎也很复杂。 A无法真正理解它加载的变量类型。有没有办法使用该批处理脚本并将其“挂钩”到我的可执行文件,以便我不必找出这个脚本具体做什么?这是批处理脚本:

:: generated from colcon_core/shell/template/prefix.bat.em
@echo off

:: This script extends the environment with all packages contained in this
:: prefix path.

:: add this prefix to the COLCON_PREFIX_PATH
call:_colcon_prefix_bat_prepend_unique_value COLCON_PREFIX_PATH "%%~dp0"

:: get all packages in topological order
call:_colcon_get_ordered_packages _ordered_packages "%~dp0"

:: source packages
if "%_ordered_packages%" NEQ "" (
  for %%p in ("%_ordered_packages:;=";"%") do (
    call:_colcon_prefix_bat_call_script "%~dp0share\%%~p\package.bat"
  )
  set "_ordered_packages="
)

goto:eof


:: function to prepend a value to a variable
:: which uses semicolons as separators
:: duplicates as well as trailing separators are avoided
:: first argument: the name of the result variable
:: second argument: the value to be prepended
:_colcon_prefix_bat_prepend_unique_value
  setlocal enabledelayedexpansion
  :: arguments
  set "listname=%~1"
  set "value=%~2"

  :: get values from variable
  set "values=!%listname%!"
  :: start with the new value
  set "all_values=%value%"
  :: skip loop if values is empty
  if "%values%" NEQ "" (
    :: iterate over existing values in the variable
    for %%v in ("%values:;=";"%") do (
      :: ignore empty strings
      if "%%~v" NEQ "" (
        :: ignore duplicates of value
        if "%%~v" NEQ "%value%" (
          :: keep non-duplicate values
          set "all_values=!all_values!;%%~v"
        )
      )
    )
  )
  :: set result variable in parent scope
  endlocal & (
    set "%~1=%all_values%"
  )
goto:eof


:: Get the package names in topological order
:: using semicolons as separators and avoiding leading separators.
:: first argument: the name of the result variable
:: second argument: the base path to look for packages
:_colcon_get_ordered_packages
  setlocal enabledelayedexpansion

  :: check environment variable for custom Python executable
  if "%COLCON_PYTHON_EXECUTABLE%" NEQ "" (
    if not exist "%COLCON_PYTHON_EXECUTABLE%" (
      echo error: COLCON_PYTHON_EXECUTABLE '%COLCON_PYTHON_EXECUTABLE%' doesn't exist
      exit /b 1
    )
    set "_colcon_python_executable=%COLCON_PYTHON_EXECUTABLE%"
  ) else (
    :: use the Python executable known at configure time
    set "_colcon_python_executable=c:\python37\python.exe"
    :: if it doesn't exist try a fall back
    if not exist "!_colcon_python_executable!" (
      python --version > NUL 2> NUL
      if errorlevel 1 (
        echo error: unable to find python executable
        exit /b 1
      )
      set "_colcon_python_executable=python"
    )
  )

  set "_colcon_ordered_packages="
  for /f %%p in ('""%_colcon_python_executable%" "%~dp0_local_setup_util.py" --merged-install"') do (
    if "!_colcon_ordered_packages!" NEQ "" set "_colcon_ordered_packages=!_colcon_ordered_packages!;"
    set "_colcon_ordered_packages=!_colcon_ordered_packages!%%p"
  )
  endlocal & (
    :: set result variable in parent scope
    set "%~1=%_colcon_ordered_packages%"
  )
goto:eof


:: call the specified batch file and output the name when tracing is requested
:: first argument: the batch file
:_colcon_prefix_bat_call_script
  if exist "%~1" (
    if "%COLCON_TRACE%" NEQ "" echo call "%~1"
    call "%~1%"
  ) else (
    echo not found: "%~1" 1>&2
  )
goto:eof
c++ batch-file environment-variables ros ros2
1个回答
0
投票

不要修改ROS2的任何内部代码(即使是批处理文件),请按照他们的规则启动ros2应用程序。 ROS2设计在一个复杂的系统中,以动态加载不同类型的包。我认为你通过将dll复制到文件夹来打破它的库搜索规则。

我建议你通过调用来编写另一个更简单的批处理文件。

call <path-to-your-ros2-ws>/setup.bat

并使用官方ros2命令行工具来运行该应用程序。

ros2 run <your-package> <executable>
© www.soinside.com 2019 - 2024. All rights reserved.