I have the following issue: I have written a simple ROS2 / Qt5 (Robot Operating System) project to show off a publisher and subscriber in GUIs. The project compiles just fine, and after copying all Qt5 and ROS dll's to the directory of the executable the application starts but then quickly closes/crashes without giving me any errors.
Attempt 2: Open a console and source the ROS2 installation (by running a batch-script which I believe loads environment variables into the console, right?). If I start the executable from within the ROS2 console now, everything works just fine.
So my assumption is, that when I compile my project and try to just start it, it is missing all the environment variables and then crashes.
Is there a way so that I can avoid having to start up a ROS2 console?
The batch-script to source the ROS2 installation also seems pretty complex to me. A cannot really understand what kind of variables it loads. Is there a way to use that batch-script and kind of "hook" it to my executable so that I don't have to find out what this script specifically does? Here is the batch-script:
:: 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
Don't modify any internal code from ROS2 (even batch file), please follow their rule to start the ros2 application. ROS2 is designed in a complex system in order to dynamic loading different kind of packages. I think you have break it's library searching rule by copying dll to the folder.
I suggest you writing another more simple batch file by invoking ..
call <path-to-your-ros2-ws>/setup.bat
and use official ros2 command line tool to run the application.
ros2 run <your-package> <executable>