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

C++ Code compiling Issue in ROS

asked 2014-03-05 13:49:53 -0500

nicobari gravatar image

This post was updated on Mar 04, 2014; 1:26pm. Hi, I just started learning ROS and was able to build and run talker and listener example. After that I thought I should write a simple program to print "hello world" in the terminal.I wrote a hworld.cpp file shown below (I borrowed the template from talker.cpp) and I ran into problem while building the code. I am sorry if this kind of question has been asked before. I tried searching for it but I couldn't find it (I would be grateful if someone can link to an earlier similar question). Thank you in advance.


#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>

int main(int argc, char **argv){
  ros::init(argc, argv, "hworld");
        ros::NodeHandle nh;
        ros::Rate loop_rate(10);

        int count =0;
        while (ros::ok()){

                cout<<"hello world"<<count<<endl;
        return 0;

Then I added this lines to the end of my CMakeList.txt file

add_executable(hwold src/hworld.cpp) target_link_libraries(hworld ${catkin_LIBRARIES}) add_dependencies(hworld beginner_tutorials_generate_messages_cpp)

but when I am running "catkin_make" I am getting the following error:

CMake Error at beginner_tutorials/CMakeLists.txt:176 (target_link_libraries): Cannot specify link libraries for target "hworld" which is not built by this project.

May be I am missing the concept behind building a code in roscpp??!!

Regards, TM

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-03-05 16:39:12 -0500

ahendrix gravatar image

You have a typo in your CMakeLists.txt.

You declare you executable as 'hwold' (no 'r') and then try to link and declare dependencies for 'hworld' (with an 'r').

Otherwise it looks like you are doing things properly.

edit flag offensive delete link more

answered 2014-03-06 02:36:09 -0500

nicobari gravatar image

updated 2014-03-06 08:31:09 -0500

Thank your very much, I feel like an Idiot for not reading the file properly. Thanks again for helping me out, I will run it and see if it works or not.

Edit: It works perfectly now, thanks again

Regards, TM

edit flag offensive delete link more


Hi nicobari, welcome to If you are satisfied with ahendrix's answer, please mark it as "accepted". You can do it by clicking the little round "tick" button on the top left corner of his answers.

Martin Peris gravatar image Martin Peris  ( 2014-03-06 13:50:41 -0500 )edit

Also, please, refrain from posting comments as new answers. If you want to comment someone's answer do so by using the "add a comment" button. Thank you so much for helping us to keep the forum clean and organised. I hope you can find all your answers here.

Martin Peris gravatar image Martin Peris  ( 2014-03-06 13:52:41 -0500 )edit

Question Tools

1 follower


Asked: 2014-03-05 13:49:53 -0500

Seen: 748 times

Last updated: Mar 06 '14