ROS/catkin编译的C++文件找不到图片源 [英] ROS/catkin compiled C++ file won't find image sources

查看:95
本文介绍了ROS/catkin编译的C++文件找不到图片源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 C++ 游戏,它使用同一父目录中文件夹中的图像.

I created a C++ game that uses images from a folder in the same parent directory.

/game_folder
----/Images
--------icon1.png
--------icon2.png
----game.cpp

程序使用 Allegro 5 库来包含图像:

Program uses Allegro 5 library to include images:

ALLEGRO_BITMAP* icon1 = al_load_bitmap("Images/icon1.png");
ALLEGRO_BITMAP* icon2 = al_load_bitmap("Images/icon2.png");

这很好用.

但是现在我需要将此程序放入 ROS/catkin 包beginner_tutorials"中.如果我将 Images 文件夹和 game.cpp 放在 ../catkin_ws/src/beginner_tutorials 中,它使用 catkin make<编译得很好,但我在运行时遇到 Segmentation Fault.

However now I need to place this program into ROS/catkin package 'beginner_tutorials'. If I place both Images folder and game.cpp into ../catkin_ws/src/beginner_tutorials it compiles fine using catkin make, but I get Segmentation Fault during runtime.

/catkin_ws/src/beginner_tutorials
----/Images
--------icon1.png
--------icon2.png
----game.cpp

我使用 gdb 并猜测错误是由程序未找到 Images 文件夹引起的.

I used gdb and guess the error is caused by the program not finding the Images folder.

我也尝试将Images文件夹放到../catkin_ws/devel/include/beginner_tutorials(包含头文件的同一个地方)

I also tried to place the Images folder to ../catkin_ws/devel/include/beginner_tutorials (the same place where header files are included)

/catkin_ws
----/devel/include/beginner_tutorials
--------/Images
------------icon1.png
------------icon2.png
----/src/beginner_tutorials
--------game.cpp

并相应地更改代码:

ALLEGRO_BITMAP* icon1 = al_load_bitmap("beginner_tutorials/Images/icon1.png");
ALLEGRO_BITMAP* icon2 = al_load_bitmap("beginner_tutorials/Images/icon2.png");

但这没有用.我应该将 Image 文件夹放在哪里才能成功包含在代码中?我是否还需要在 CMakeLists.txt 中进行调整?

But this didn't work. Where am I supposed to place the Image folder for it to be successfully included in the code? Do I also need to make adjustments in the CMakeLists.txt?

-编辑
用 CMakeLists.txt 尝试了一些但没有成功:

-EDIT
Tried something with CMakeLists.txt with no success:

set(game_SOURCES
  src/game.cpp
  src/Images/
)

add_executable(game ${game_SOURCES})
target_link_libraries(game ${allegro_LIBS})

推荐答案

这行不通的原因是你的代码被编译,然后放在catkin_ws/devel/lib/ 文件夹(lib 不是 include!).然后,当您启动代码时,它只会在相对于可执行文件的路径中查找.这意味着您实际上必须将它放在 catkin_ws/devel/lib/ 文件夹中.这样做的问题是,一旦您清理工作区,所有这些目录都将被删除,并且您必须在每次 catkin clean 后将所有文件重新复制到其中.

The reason why this does not work is that your code gets compiled and is then placed inside the catkin_ws/devel/lib/<package_name> folder (lib not include!). Then when you launch the code it will look only in paths relative to the executable. This means you would actually have to place it inside the catkin_ws/devel/lib/<package_name> folder. The problem with that is though that as soon as you clean the workspace all of these directories will be deleted and you would have to re-copy all the files to it after each catkin clean.

为此,ROS C++ API 具有允许您浏览 catkin_ws/src 文件夹内给定包的文件夹或显示所有可用包的功能包括头文件ros/package.h:

For this purpose the ROS C++ API has though functions that allow you to browse the folder of a given package inside the catkin_ws/src folder or display all available packages when including the header file ros/package.h:

#include <ros/package.h>

// ...

std::string const package_path = ros::package::getPath("beginner_tutorials");
ALLEGRO_BITMAP* icon1 = al_load_bitmap(package_path + "/Images/icon1.png");
ALLEGRO_BITMAP* icon2 = al_load_bitmap(package_path + "/Images/icon2.png");

应该可以解决问题.这将使代码看起来在 catkin_ws/src/beginner_tutorials 路径中.

should do the trick. This will make the code look in catkin_ws/src/beginner_tutorials path.

类似在 Python 中的语法是:

Similarly in Python the syntax would be:

import os
import rospkg

# ...

rospack = rospkg.RosPack()
package_path = rospack.get_path('beginner_tutorials')
icon1_path = os.path.join(package_path, "Images", "icon1.png")
icon2_path = os.path.join(package_path, "Images", "icon2.png")

这篇关于ROS/catkin编译的C++文件找不到图片源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆