ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Boost::Thread and ROS Fuerte CMake help

asked 2015-03-01 19:14:38 -0600

TheHenshinger gravatar image

updated 2015-03-05 13:58:33 -0600

Ok, so I am trying to utilize boost threads in a ROS application and I have been trying to figure out how to link boost to the the final exe. I keep finding conflicting ways on how to do this and not sure if i am doing it right.

This is my CMake file:

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

# Set the build type.  Options are:
#  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
#  Debug          : w/ debug symbols, w/o optimization
#  Release        : w/o debug symbols, w/ optimization
#  RelWithDebInfo : w/ debug symbols, w/ optimization
#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)

rosbuild_init()

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

find_package(Boost REQUIRED COMPONENTS thread)
rosbuild_add_boost_directories()


#uncomment if you have defined messages
#rosbuild_genmsg()
#uncomment if you have defined services
#rosbuild_gensrv()

#common commands for building c++ executables and libraries
rosbuild_add_library(Joystick src/CJoystick.cpp)
rosbuild_add_library(Frame src/CFrame.cpp)
rosbuild_add_library(Control src/CControl.cpp)
rosbuild_add_library(Vec2 src/Classes/Vec2/Vec2.cpp)
rosbuild_add_library(Vec3 src/Classes/Vec3/vec3.cpp)
rosbuild_add_library(Vec4 src/Classes/Vec4/Vec4.cpp)
rosbuild_add_library(Mat3 src/Classes/Mat3/mat3.cpp)
rosbuild_add_library(Waypoint src/Classes/Waypoint/waypoint.cpp)
rosbuild_add_library(UAV src/Classes/UAV/UAV.cpp)
#rosbuild_add_library(ARDrone src/ARDrone.cpp)
rosbuild_add_executable(ARDroneV4 src/ARDrone.cpp)
target_link_libraries(ARDroneV4 UAV Waypoint Frame Control Mat3 Vec2 Vec3 Vec4 Joystick)
rosbuild_link_boost(ARDroneV4 thread system)

Though it compiles fine the threading never happens. Any idea on what I am doing wrong?

Update:

This is the basic outline . Everything not associated with the thread is not included.

#include <boost/thread.hpp>

 void workfunc(){
   ROS_INFO("New Thread");

 }

int main(int argc,char **argv)
{
  time_t rawtime;
  std::string logfile;

  ros::init(argc,argv, "ARDroneControl"); //create node
  ros::NodeHandle n;

  ros::Rate rate(FREQ); //cycle per seconds

  while(n.ok())
  {
  boost::thread(workfunc);


    ros::spinOnce();
    rate.sleep();
  }

  return 0;
}
edit retag flag offensive close merge delete

Comments

If it complies fine and you get no error when you create a boost::thread object then the problem does not lie in the linking of the library. To check if the library is linked properly you can type in the terminal: ldd ARDroneV4 | grep boost.

gpldecha gravatar image gpldecha  ( 2015-03-02 02:06:31 -0600 )edit

This is the output I get

libboost_thread.so.1.46.1 => /usr/lib/libboost_thread.so.1.46.1 (0xb76f0000)

and so forth.

I didn't think it was a linker problem but not even a simple function that prints "Hello World" doesn't print to screen. So I am not too sure what's going on then.

TheHenshinger gravatar image TheHenshinger  ( 2015-03-02 12:35:57 -0600 )edit

Then I suggest you update your question with a simple example which prints hello world in a new thread, so we might understand what is going wrong.

gpldecha gravatar image gpldecha  ( 2015-03-02 12:54:24 -0600 )edit

I updated it

TheHenshinger gravatar image TheHenshinger  ( 2015-03-05 13:58:46 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2015-03-05 14:26:41 -0600

Dirk Thomas gravatar image

You invoke the thread constructor but don't assign the thread to any variable. Therefore it immediately goes out of scope and doesn't have a chance to start running.

edit flag offensive delete link more

Comments

Derp!!!! Much Obliged, now if you mind I will go hit my head against my desk for a while.

TheHenshinger gravatar image TheHenshinger  ( 2015-03-06 14:21:51 -0600 )edit

If you really feel the need for it and think it helps please go ahead. But please also consider to mark the answer that it solved your problem so others can find it too.

Dirk Thomas gravatar image Dirk Thomas  ( 2015-03-06 15:26:49 -0600 )edit

You might want to look up what the word figuratively means.

TheHenshinger gravatar image TheHenshinger  ( 2015-03-06 17:59:40 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-03-01 19:14:38 -0600

Seen: 338 times

Last updated: Mar 05 '15