How to add or use C++ library in application

Hello,
I ran into this issue today where I couldnt use some of basic libraries of C++ such as iostream.h and sstream.h while writing application. My application requires concatinating float data and string data hence these are necessary to me. Can anybody tell me how can I utilise these libraries while building the app? Thansk in advance.

     #include "interfaces.h"
     #include "legato.h"
     #include <termios.h>
     #include <unistd.h>
     #include <string.h>
     #include <iostream.h>


     // Used to convert GPS int to double
     #define GPS_DECIMAL_SHIFT 6
     // Used for distance calculations
     #define MIN_REQUIRED_HORIZ_ACCURACY_METRES 10  // TODO validate that this is realistic
     #define POLL_PERIOD_SEC 2 * 60 // 2 minutes
     #define RETRY_PERIOD_SEC 1


     static le_posCtrl_ActivationRef_t posCtrlRef;
     static le_timer_Ref_t pollingTimer;
     static struct {
      double lat;
      double lon;
      double horizAccuracy;
      uint64_t datetime;
       } lastReading;

       /*** Determine if we have a reading * * (other things make factor in here down the road)*/
       static bool hasReading() {
       return lastReading.datetime != 0;
        }

          /**
          * Determine if we can provide an IPC caller
          * with a location
          */
          static bool canGetLocation() {
          return hasReading() && posCtrlRef != NULL;
           }

            /**
            * IPC function to get location
             */
             le_result_t brnkl_gps_getCurrentLocation(double* latitude,
                                     double* longitude,
                                     double* horizontalAccuracy,
                                     uint64_t* readingTimestamp) {
              if (!canGetLocation()) {
              return LE_UNAVAILABLE;
                }
            *latitude = lastReading.lat;
            *longitude = lastReading.lon;
            *horizontalAccuracy = lastReading.horizAccuracy;
            *readingTimestamp = lastReading.datetime;
             return LE_OK;
                }

              /**
               * Main polling function
               *
               * Change MIN_REQUIRED_HORIZ_ACCURACY_METRES if
               * a more/less accurate fix is required
               */
               int open_uart1 (  char *dev)
               {
                int     fd;
                fd = open (dev, O_RDWR | O_NOCTTY | O_NDELAY);
                struct termios options;
                // The old way. Let's not change baud settings
                fcntl (fd, F_SETFL, 0);

                 // get the parameters
                 tcgetattr (fd, &options);
                 // Set the baud rates to 115200...
                  cfsetispeed(&options, B9600);
                  cfsetospeed(&options, B9600);

                  // Enable the receiver and set local mode...
                  options.c_cflag |= (CLOCAL | CREAD);

                  // No parity (8N1):
                 options.c_cflag &= ~PARENB;
                 options.c_cflag &= ~CSTOPB;
                 options.c_cflag &= ~CSIZE;
                 options.c_cflag |= CS8;

                  // enable hardware flow control (CNEW_RTCCTS)
                 // options.c_cflag |= CRTSCTS;
                 // if(hw_handshake)
                  // Disable the hardware flow control for use with mangOH RED
                  options.c_cflag &= ~CRTSCTS;

                  // set raw input
                 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
                   options.c_iflag &= ~(INLCR | ICRNL | IGNCR);

                  // set raw output
                  options.c_oflag &= ~OPOST;
                  options.c_oflag &= ~OLCUC;
                  options.c_oflag &= ~ONLRET;
                   options.c_oflag &= ~ONOCR;
                   options.c_oflag &= ~OCRNL;

                  // Set the new options for the port...
                  tcsetattr (fd, TCSANOW, &options);


                  return fd;
                  }


                    void write_uart1 (int fd,char *cmd)
                    {
                     //	fd_set  rfds;
                    //	struct timeval timeout;
                 int     wrote = 0;
                wrote = write (fd, cmd, strlen (cmd));
                LE_INFO("wrote  %d ",wrote);
                      }


                   static void getLocation(le_timer_Ref_t timerRef) {
                       le_timer_Stop(timerRef);
                       char myString;
                       int serial_fd;
                        char north="N";
                       char east="E";
                        serial_fd= open_uart1("/dev/ttyHS0");
                          LE_DEBUG("Checking GPS position");
                         int32_t rawLat, rawLon, rawHoriz;
                           le_result_t result = le_pos_Get2DLocation(&rawLat, &rawLon, &rawHoriz);
                         bool isAccurate = rawHoriz <= MIN_REQUIRED_HORIZ_ACCURACY_METRES;
                            bool resOk = result == LE_OK;
                         if (resOk && isAccurate) {
                         double denom = powf(10, GPS_DECIMAL_SHIFT);  // divide by this
                         lastReading.lat = ((double)rawLat) / denom;
                         lastReading.lon = ((double)rawLon) / denom;
                          // no conversion required for horizontal accuracy
                          //lastReading.horizAccuracy = (double)rawHoriz;
                         //lastReading.datetime = GetCurrentTimestamp();
                         LE_INFO("Got reading...");
                        LE_INFO("lat: %f, long: %f, horiz: %f",
                           lastReading.lat, lastReading.lon, lastReading.horizAccuracy);
                         le_timer_SetMsInterval(timerRef, POLL_PERIOD_SEC * 1000);
                            } else {
                          if (!isAccurate && resOk) {
                         LE_INFO("Rejected for accuracy (%d m)", rawHoriz);
                         double denom = powf(10, GPS_DECIMAL_SHIFT);  // divide by this
                          lastReading.lat = ((double)rawLat) / denom;
                          lastReading.lon = ((double)rawLon) / denom;
                          // no conversion required for horizontal accuracy
                           //lastReading.horizAccuracy = (double)rawHoriz;
                           //lastReading.datetime = GetCurrentTimestamp();
                             LE_INFO("Got reading...");
                            LE_INFO("%fN %fE , horiz: %f",
                              lastReading.lat, lastReading.lon, lastReading.horizAccuracy);
                               le_timer_SetMsInterval(timerRef, POLL_PERIOD_SEC * 1000);
                            myString= to_string(lastReading.lat)+ "N";
                              myString=  myString + to_string(lastReading.lon)+ "E";
                             write_uart1(serial_fd,myString);
                              LE_INFO("Sent via UART0");
                                }
                               LE_INFO("Failed to get reading... retrying in %d seconds",
                                 RETRY_PERIOD_SEC);
                                    le_timer_SetMsInterval(timerRef, RETRY_PERIOD_SEC * 1000);
                                   }
                                  le_timer_Start(timerRef);
                                   }

                                     /**
                                     * Perform all required setup
                                         *
                                         * Note that we run this on a timer to avoid
                                      * blocking up the main (only) thread. If this 
                                       * was run in a while(true) that sleeps,
                                         * the IPC caller would be blocked indefinitely
                                           */
                                       static void gps_init() {
                                         posCtrlRef = le_posCtrl_Request();
                                          LE_FATAL_IF(posCtrlRef == NULL, "Couldn't activate positioning");
                                         pollingTimer = le_timer_Create("GPS polling timer");
                                           le_timer_SetHandler(pollingTimer, getLocation);
                                           le_timer_SetRepeat(pollingTimer, 1);
                                           le_timer_SetMsInterval(pollingTimer, 0);
                                           le_timer_Start(pollingTimer);
                                           }

                                       COMPONENT_INIT {
                                          gps_init();
                                            }

Can you try #include <iostream> instead of #include <iostream.h> ?

Hi @JordanZhao

Thanks for replying I got the following error when I did as you said

C:\Legato\workspace1\load/textLocComponent/textLoc.c:6:20: fatal error: iostream: No such
file or directory

 #include <iostream>
                ^

compilation terminated.
ninja: build stopped: subcommand failed.

This is the same error I get even if I use iostram.h at the include

The source file textLoc.c should end up with cpp.

@JordanZhao

This is what I got after renaming the .c to .cpp

FAILED: /opt/swi/y17-ext/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ --sysroot=/opt/swi/y17-ext/sysroots/armv7a-vfp-neon-poky-linux-gnueabi -MMD -MF /tmp/build/load/Target_Legato_Debug/component/46d718d6c48e86e02d0860f6dcd90bb8/obj/2ac7f9ac8f068ffa1b45277234f2c7c1.o.d -c C:\Legato\workspace1\load/textLocComponent/textLoc.cpp -o /tmp/build/load/Target_Legato_Debug/component/46d718d6c48e86e02d0860f6dcd90bb8/obj/2ac7f9ac8f068ffa1b45277234f2c7c1.o -DLE_FILENAME=basename C:\Legato\workspace1\load/textLocComponent/textLoc.cpp -Wall -fPIC -Werror -fvisibility=hidden -DMK_TOOLS_BUILD -DLEGATO_EMBEDDED -g -I/legato/interfaces -I/legato/framework/c/inc -I/legato/interfaces -I/legato/interfaces/wifi -I/legato/interfaces/atServices -I/legato/interfaces/supervisor -I/legato/interfaces/logDaemon -I/legato/interfaces/airVantage -I/legato/interfaces/secureStorage -I/legato/interfaces/positioning -I/legato/interfaces/modemServices -IC:\Legato\workspace1\load -I/tmp/build/load/Target_Legato_Debug/component/46d718d6c48e86e02d0860f6dcd90bb8/src -I/tmp/build/load/Target_Legato_Debug/api/66dee3ecaa2d84623bc078c25538182c/client -I/tmp/build/load/Target_Legato_Debug/api/1a6001d9b1e3aef810d6348bffa5b140/client -I/tmp/build/load/Target_Legato_Debug/api/1aeda5159d91d1e827744cac772fba9d/client -I/tmp/build/load/Target_Legato_Debug/api/ca7c11c9910525ab0f7eba33c47c1b27/client -I/tmp/build/load/Target_Legato_Debug/api/bd014bc256e24839a10f783dbcb52a62/client -I/tmp/build/load/Target_Legato_Debug/api/d46d5dcf6dfbac3c4f5908b3233af748/client -I/tmp/build/load/Target_Legato_Debug/api/9db64609628347c0cfbb171f960ab680/client -DLE_COMPONENT_NAME=textLocComponent -DLE_LOG_SESSION=textLocComponent_LogSession -DLE_LOG_LEVEL_FILTER_PTR=textLocComponent_LogLevelFilterPtr “-DCOMPONENT_INIT=LE_CI_LINKAGE LE_SHARED void _textLocComponent_COMPONENT_INIT()”
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:6:22: fatal error: iostream.h: No such file or directory
#include <iostream.h>
^
compilation terminated.
ninja: build stopped: subcommand failed.
Makefile:44: recipe for target ‘target’ failed
make: Leaving directory ‘C:\Legato\workspace1\load/Target_Legato_Debug’

Can you combine my two comments? Using #include <iostream> with xxx.cpp?

1 Like

@JordanZhao

Thanks for replying that resolved the library issue however the code isnt building yet

Throwing following error

[1/6] Compiling C++ source
FAILED: /opt/swi/y17-ext/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ --sysroot=/opt/swi/y17-ext/sysroots/armv7a-vfp-neon-poky-linux-gnueabi -MMD -MF /tmp/build/load/Target_Legato_Debug/component/46d718d6c48e86e02d0860f6dcd90bb8/obj/2ac7f9ac8f068ffa1b45277234f2c7c1.o.d -c C:\Legato\workspace1\load/textLocComponent/textLoc.cpp -o /tmp/build/load/Target_Legato_Debug/component/46d718d6c48e86e02d0860f6dcd90bb8/obj/2ac7f9ac8f068ffa1b45277234f2c7c1.o -DLE_FILENAME=basename C:\Legato\workspace1\load/textLocComponent/textLoc.cpp -Wall -fPIC -Werror -fvisibility=hidden -DMK_TOOLS_BUILD -DLEGATO_EMBEDDED -g -I/legato/interfaces -I/legato/framework/c/inc -I/legato/interfaces -I/legato/interfaces/wifi -I/legato/interfaces/atServices -I/legato/interfaces/supervisor -I/legato/interfaces/logDaemon -I/legato/interfaces/airVantage -I/legato/interfaces/secureStorage -I/legato/interfaces/positioning -I/legato/interfaces/modemServices -IC:\Legato\workspace1\load -I/tmp/build/load/Target_Legato_Debug/component/46d718d6c48e86e02d0860f6dcd90bb8/src -I/tmp/build/load/Target_Legato_Debug/api/66dee3ecaa2d84623bc078c25538182c/client -I/tmp/build/load/Target_Legato_Debug/api/1a6001d9b1e3aef810d6348bffa5b140/client -I/tmp/build/load/Target_Legato_Debug/api/1aeda5159d91d1e827744cac772fba9d/client -I/tmp/build/load/Target_Legato_Debug/api/ca7c11c9910525ab0f7eba33c47c1b27/client -I/tmp/build/load/Target_Legato_Debug/api/bd014bc256e24839a10f783dbcb52a62/client -I/tmp/build/load/Target_Legato_Debug/api/d46d5dcf6dfbac3c4f5908b3233af748/client -I/tmp/build/load/Target_Legato_Debug/api/9db64609628347c0cfbb171f960ab680/client -DLE_COMPONENT_NAME=textLocComponent -DLE_LOG_SESSION=textLocComponent_LogSession -DLE_LOG_LEVEL_FILTER_PTR=textLocComponent_LogLevelFilterPtr “-DCOMPONENT_INIT=LE_CI_LINKAGE LE_SHARED void _textLocComponent_COMPONENT_INIT()”
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp: In function ‘void getLocation(le_timer_Ref_t)’:
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:134:14: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
char north=“N”;
^
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:135:13: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
char east=“E”;
^
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:136:38: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
serial_fd= open_uart1("/dev/ttyHS0");
^
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:166:46: error: ‘to_string’ was not declared in this scope
myString= to_string(lastReading.lat)+ north;
^
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:168:41: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
write_uart1(serial_fd,myString);
^
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:120:6: note: initializing argument 2 of ‘void write_uart1(int, char*)’
void write_uart1 (int fd,char *cmd)
^
cc1plus: all warnings being treated as errors
ninja: build stopped: subcommand failed.
make: *** [target] Error 1
Makefile:44: recipe for target ‘target’ failed
make: Leaving directory ‘C:\Legato\workspace1\load/Target_Legato_Debug’

Can you try to change char north=“N”; ---- TO —> char north = ‘N’; ?

1 Like

@JordanZhao

and Finally Im left with these now

[1/6] Compiling C++ source
make: *** [target] Error 1
FAILED: /opt/swi/y17-ext/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ --sysroot=/opt/swi/y17-ext/sysroots/armv7a-vfp-neon-poky-linux-gnueabi -MMD -MF /tmp/build/load/Target_Legato_Debug/component/46d718d6c48e86e02d0860f6dcd90bb8/obj/2ac7f9ac8f068ffa1b45277234f2c7c1.o.d -c C:\Legato\workspace1\load/textLocComponent/textLoc.cpp -o /tmp/build/load/Target_Legato_Debug/component/46d718d6c48e86e02d0860f6dcd90bb8/obj/2ac7f9ac8f068ffa1b45277234f2c7c1.o -DLE_FILENAME=basename C:\Legato\workspace1\load/textLocComponent/textLoc.cpp -Wall -fPIC -Werror -fvisibility=hidden -DMK_TOOLS_BUILD -DLEGATO_EMBEDDED -g -I/legato/interfaces -I/legato/framework/c/inc -I/legato/interfaces -I/legato/interfaces/wifi -I/legato/interfaces/atServices -I/legato/interfaces/supervisor -I/legato/interfaces/logDaemon -I/legato/interfaces/airVantage -I/legato/interfaces/secureStorage -I/legato/interfaces/positioning -I/legato/interfaces/modemServices -IC:\Legato\workspace1\load -I/tmp/build/load/Target_Legato_Debug/component/46d718d6c48e86e02d0860f6dcd90bb8/src -I/tmp/build/load/Target_Legato_Debug/api/66dee3ecaa2d84623bc078c25538182c/client -I/tmp/build/load/Target_Legato_Debug/api/1a6001d9b1e3aef810d6348bffa5b140/client -I/tmp/build/load/Target_Legato_Debug/api/1aeda5159d91d1e827744cac772fba9d/client -I/tmp/build/load/Target_Legato_Debug/api/ca7c11c9910525ab0f7eba33c47c1b27/client -I/tmp/build/load/Target_Legato_Debug/api/bd014bc256e24839a10f783dbcb52a62/client -I/tmp/build/load/Target_Legato_Debug/api/d46d5dcf6dfbac3c4f5908b3233af748/client -I/tmp/build/load/Target_Legato_Debug/api/9db64609628347c0cfbb171f960ab680/client -DLE_COMPONENT_NAME=textLocComponent -DLE_LOG_SESSION=textLocComponent_LogSession -DLE_LOG_LEVEL_FILTER_PTR=textLocComponent_LogLevelFilterPtr “-DCOMPONENT_INIT=LE_CI_LINKAGE LE_SHARED void _textLocComponent_COMPONENT_INIT()”
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp: In function ‘void getLocation(le_timer_Ref_t)’:
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:136:38: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
serial_fd= open_uart1("/dev/ttyHS0");
^
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:166:46: error: ‘to_string’ was not declared in this scope
myString= to_string(lastReading.lat)+ north;
^
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:168:41: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
write_uart1(serial_fd,myString);
^
C:\Legato\workspace1\load/textLocComponent/textLoc.cpp:120:6: note: initializing argument 2 of ‘void write_uart1(int, char*)’
void write_uart1 (int fd,char *cmd)
^
cc1plus: all warnings being treated as errors
ninja: build stopped: subcommand failed.
Makefile:44: recipe for target ‘target’ failed
make: Leaving directory ‘C:\Legato\workspace1\load/Target_Legato_Debug’

Please check those ERROR on Google for answers, Thanks.

Just a thought - have you tried compiling the C++ file as a C++ file with the c++ flags set.

In Component.cdef

cxxflags:
{
-std=c++0x
}

@johnofleek

Thanks for replying I hadnt added the directory “/dev/ttyHS0” to the .adef file. Igot it working thanks for suggestion.