Subscribe to mavros/global_position/global


I’m trying to subscribe to the topic mavros/global_position/global using C++ and print the information on the screen. I followed this tutorial to create the subscriber node. However, the type of the mavros topic is sensor_msgs/NavSatFix I tried with cout because I don’t know how it would be with ROS_INFO()

#include “ros/ros.h”
#include “std_msgs/String.h”
#include “sensor_msgs/NavSatFix.h”
using namespace std;

void chatterCallback(const sensor_msgs::NavSatFix::ConstPtr& msg)
cout << *msg << endl;

int main(int argc, char **argv)

ros::init(argc, argv, "listener1");

ros::NodeHandle n;

ros::Subscriber sub = n.subscribe("/mavros/global_position/global", 1, chatterCallback);


return 0;


But when I compile it and run it nothing happens. Does anyone know how can I do it? Also, how can I store the information ( longitude, altitude, and latitude) that comes from the topic in a variable and use it in the program?