Results 1 to 6 of 6

Thread: Using Qt instead of lt_dlsym (libtool)

  1. #1
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using Qt instead of lt_dlsym (libtool)

    Hello everybody, I'm running a ported from Linux to Windows using Qt 4.8.2. I am in a dead end. I do not know how to make the conversion from libtool to Qt. The following is a piece of code under test:
    Qt Code:
    1. lt_dlhandle handle = NULL;
    2.  
    3. // the library name is the first word in the string
    4. char libname[256];
    5. sscanf( lib, "%s %*s", libname );
    6.  
    7. if(( handle = lt_dlopenext( libname ) ))
    8. {
    9. //printf( "]" );
    10.  
    11. model_callback_t initfunc = (model_callback_t)lt_dlsym( handle, "Init" );
    12. if( initfunc == NULL )
    13. {
    14. printf( "Model::LoadControllerModule() - (Libtool error: %s.) Something is wrong" \
    15. "with your plugin.\n", lt_dlerror() ); // report the error from libtool
    16. puts( "libtool error #1" );
    17. fflush( stdout );
    18. exit(-1);
    19. }
    20.  
    21. AddCallback( CB_INIT, initfunc, new CtrlArgs(lib, World::ctrlargs) ); // pass complete string into initfunc
    22. }
    To copy to clipboard, switch view to plain text mode 
    The functions involved in the exchange are: lt_dlhandle, lt_dlopenext (), lt_dlsym ().
    I have created the library wander.a (static) using Qt Creator 2.5.2, where there is the function Init (). The function Init () is the one that I use in the rest of the code after the second after loading the code above I would like to convert to Qt. The variable lib contains the name of the static library wander.
    As regards the function AddCallback () has the following code (it is not to touch as they are already in place in Qt):
    Qt Code:
    1. void Model::AddCallback( callback_type_t type, model_callback_t cb, void *user )
    2. {
    3. //callbacks[address].insert( cb_t( cb, user ));
    4. callbacks[type].insert( cb_t( cb, user ));
    5.  
    6. // debug info - record the global number of registered callbacks
    7. if( type == CB_UPDATE )
    8. {
    9. assert( world->update_cb_count >= 0 );
    10. world->update_cb_count++;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    Can you help me? I do not know how to do this part of code.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Using Qt instead of lt_dlsym (libtool)

    You can use QLibrary, there are couple of ways to do this, here is an example. Make sure you read the basics of how to use QLibrary
    Qt Code:
    1. char libname[] = "library1 library2";
    2. QString lib = QString(libname).split(" ").at(0);
    3.  
    4. QLibrary library(lib);
    5. model_callback_t initfunc = (model_callback_t)library.resolve("Init");
    6.  
    7. if(initfunc == 0)
    8. {
    9. qDebug() << "Model::LoadControllerModule() - (Libtool error: " << library.errorString() << ")"
    10. "Something is wrong with your plugin."; // report the error from QLibrary
    11. qDebug() << "QLibrary error #1";
    12. qApp->exit(-1);
    13. }
    14.  
    15. AddCallback(CB_INIT, initfunc, new CtrlArgs(lib, World::ctrlargs)); // pass complete string into initfunc
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using Qt instead of lt_dlsym (libtool)

    Thanks Santosh Reddy, I try now your solution. I am very grateful.
    I tried using your solution. Your piece of code is OK, the problem now is my static library libwander.a
    I get this response by running the code with your part:
    Qt Code:
    1. Model::LoadControllerModule() - [Ctrl "wander"
    2. Model::LoadControllerModule() - (Libtool error: "Cannot load library wander: Impossibile trovare il modulo specificato." ) Some thing is wrong with your plugin.
    3. QLibrary error #1
    To copy to clipboard, switch view to plain text mode 
    Carry a piece of code of my static library named libwander.a.
    Qt Code:
    1. extern "C" int Init( Model *mod, CtrlArgs *args )
    2. {
    3. // local arguments
    4. /* printf( "\nWander controller initialised with:\n"
    5.   "\tworldfile string \"%s\"\n"
    6.   "\tcmdline string \"%s\"",
    7.   args->worldfile.c_str(),
    8.   args->cmdline.c_str() );
    9.   */
    10.  
    11. robot_t *robot = new robot_t;
    12.  
    13. robot->avoidcount = 0;
    14. robot->randcount = 0;
    15.  
    16. robot->pos = (ModelPosition *)mod;
    17.  
    18. if( verbose )
    19. robot->pos->AddCallback( Model::CB_UPDATE, (model_callback_t)PositionUpdate, robot );
    20.  
    21. robot->pos->Subscribe(); // starts the position updates
    22.  
    23. robot->laser = (ModelRanger *)mod->GetChild( "ranger:1" );
    24. robot->laser->AddCallback( Model::CB_UPDATE, (model_callback_t)LaserUpdate, robot );
    25. robot->laser->Subscribe(); // starts the ranger updates
    26.  
    27. return 0; //ok
    28. }
    To copy to clipboard, switch view to plain text mode 
    The static library libwander.a was created in Qt. I report the project file:
    Qt Code:
    1. QT += opengl
    2.  
    3. TARGET = wanderd
    4. TEMPLATE = lib
    5. CONFIG += staticlib
    6.  
    7. INCLUDEPATH += C:/Qt/progetti/Simulatore
    8.  
    9. SOURCES += wander.cpp
    10.  
    11. HEADERS += \
    12. wander.h
    To copy to clipboard, switch view to plain text mode 
    The part of the code with your addition, it would now be:
    Qt Code:
    1. void Model::LoadControllerModule( const char *lib )
    2. {
    3. printf( "Model::LoadControllerModule() - [Ctrl \"%s\"", lib );
    4. fflush(stdout);
    5.  
    6. char libname[20];
    7. strcpy(libname, lib);
    8. QString libn = QString(libname).split(" ").at(0);
    9. QLibrary library(libn);
    10. model_callback_t initfunc = (model_callback_t)library.resolve("Init");
    11. printf( "]" );
    12.  
    13. if(initfunc == 0)
    14. {
    15. qDebug() << "\nModel::LoadControllerModule() - (Libtool error: " << library.errorString() << ")"
    16. "\nSomething is wrong with your plugin."; // report the error from QLibrary
    17. qDebug() << "QLibrary error #1";
    18. return;
    19. }
    20.  
    21. AddCallback(CB_INIT, initfunc, new CtrlArgs(lib, World::ctrlargs)); // pass complete string into initfunc
    22. }
    To copy to clipboard, switch view to plain text mode 
    Where did I go wrong ?
    Last edited by giorgik; 20th April 2013 at 19:18.

  4. #4
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using Qt instead of lt_dlsym (libtool)

    A part of my error is due to the fact that the library to be created should be shared, not static.
    But now I have another problem, I can not compile it as a shared library in Qt, as it tells me I can not find any references to the calling program (ie the one with your piece of code).
    I can not solve this problem. The strange thing is that Qt Creator (in the project shared library) sees references to the project where there is your code ... and then how to solve this thing ?
    Below I put the project file of the shared library:
    Qt Code:
    1. QT += opengl
    2.  
    3. TARGET = wander
    4. TEMPLATE = lib
    5. CONFIG += dll
    6.  
    7. DEFINES += WANDER_LIBRARY
    8.  
    9. SOURCES += wander.cpp
    10.  
    11. HEADERS += wander.h\
    12. wander_global.h
    To copy to clipboard, switch view to plain text mode 
    file wander.h:
    Qt Code:
    1. #include "wander_global.h"
    2. #include "C:/Qt/progetti/Simulatore/simulator.h"
    3.  
    4. namespace Sim
    5. {
    6.  
    7. class Model;
    8.  
    9. WANDERSHARED_EXPORT int Init( Model *mod, CtrlArgs *args );
    10. int LaserUpdate( Model *mod, robot_t *robot );
    11. int PositionUpdate( Model *mod, robot_t *robot );
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 
    file wander.cpp:
    Qt Code:
    1. #include "wander.h"
    2.  
    3.  
    4. static const double cruisespeed = 0.4;
    5. static const double avoidspeed = 0.05;
    6. static const double avoidturn = 0.5;
    7. static const double minfrontdistance = 1.0; // 0.6
    8. static const bool verbose = false;
    9. static const double stopdist = 0.3;
    10. static const int avoidduration = 10;
    11.  
    12.  
    13. using namespace Sim;
    14.  
    15.  
    16. // Simulator calls this when the model starts up
    17. extern "C" int Init( Model *mod, CtrlArgs *args )
    18. {
    19. // local arguments
    20. /* printf( "\nWander controller initialised with:\n"
    21.   "\tworldfile string \"%s\"\n"
    22.   "\tcmdline string \"%s\"",
    23.   args->worldfile.c_str(),
    24.   args->cmdline.c_str() );
    25.   */
    26.  
    27. robot_t *robot = new robot_t;
    28.  
    29. robot->avoidcount = 0;
    30. robot->randcount = 0;
    31.  
    32. robot->pos = (ModelPosition *)mod;
    33.  
    34. if( verbose )
    35. robot->pos->AddCallback( Model::CB_UPDATE, (model_callback_t)PositionUpdate, robot );
    36.  
    37. robot->pos->Subscribe(); // starts the position updates
    38.  
    39. robot->laser = (ModelRanger *)mod->GetChild( "ranger:1" );
    40. robot->laser->AddCallback( Model::CB_UPDATE, (model_callback_t)LaserUpdate, robot );
    41. robot->laser->Subscribe(); // starts the ranger updates
    42.  
    43. return 0; //ok
    44. }
    45.  
    46. // inspect the ranger data and decide what to do
    47. int LaserUpdate( Model *mod, robot_t *robot )
    48. {
    49. // get the data
    50. const std::vector<meters_t> &scan = robot->laser->GetSensors()[0].ranges;
    51. uint32_t sample_count = scan.size();
    52. if( sample_count < 1 )
    53. return 0;
    54.  
    55. bool obstruction = false;
    56. bool stop = false;
    57.  
    58. // find the closest distance to the left and right and check if
    59. // there's anything in front
    60. double minleft = 1e6;
    61. double minright = 1e6;
    62.  
    63. for (uint32_t i = 0; i < sample_count; i++)
    64. {
    65. if( verbose ) printf( "%.3f ", scan[i] );
    66.  
    67. if( (i > (sample_count / 3)) && (i < (sample_count - (sample_count / 3)))
    68. && scan[i] < minfrontdistance)
    69. {
    70. if( verbose ) puts( " obstruction!" );
    71. obstruction = true;
    72. }
    73.  
    74. if( scan[i] < stopdist )
    75. {
    76. if( verbose ) puts( " stopping!" );
    77. stop = true;
    78. }
    79.  
    80. if( i > sample_count / 2 )
    81. minleft = std::min( minleft, scan[i] );
    82. else
    83. minright = std::min( minright, scan[i] );
    84. }
    85.  
    86. if( verbose )
    87. {
    88. puts( "" );
    89. printf( "minleft %.3f \n", minleft );
    90. printf( "minright %.3f\n ", minright );
    91. }
    92.  
    93. if( obstruction || stop || (robot->avoidcount>0) )
    94. {
    95. if( verbose ) printf( "Avoid %d\n", robot->avoidcount );
    96.  
    97. robot->pos->SetXSpeed( stop ? 0.0 : avoidspeed );
    98.  
    99. // once we start avoiding, select a turn direction and stick with it for a few iterations
    100. if( robot->avoidcount < 1 )
    101. {
    102. if( verbose ) puts( "Avoid START" );
    103. robot->avoidcount = random() % avoidduration + avoidduration;
    104.  
    105. if( minleft < minright )
    106. {
    107. robot->pos->SetTurnSpeed( -avoidturn );
    108. if( verbose ) printf( "turning right %.2f\n", -avoidturn );
    109. }
    110. else
    111. {
    112. robot->pos->SetTurnSpeed( +avoidturn );
    113. if( verbose ) printf( "turning left %2f\n", +avoidturn );
    114. }
    115. }
    116.  
    117. robot->avoidcount--;
    118. }
    119. else
    120. {
    121. if( verbose ) puts( "Cruise" );
    122.  
    123. robot->avoidcount = 0;
    124. robot->pos->SetXSpeed( cruisespeed );
    125. robot->pos->SetTurnSpeed( 0 );
    126. }
    127.  
    128. // if( robot->pos->Stalled() )
    129. // {
    130. // robot->pos->SetSpeed( 0,0,0 );
    131. // robot->pos->SetTurnSpeed( 0 );
    132. // }
    133.  
    134. return 0; // run again
    135. }
    136.  
    137. int PositionUpdate( Model *mod, robot_t *robot )
    138. {
    139. Pose pose = robot->pos->GetPose();
    140.  
    141. printf( "Pose: [%.2f %.2f %.2f %.2f]\n", pose.x, pose.y, pose.z, pose.a );
    142.  
    143. return 0; // run again
    144. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by giorgik; 20th April 2013 at 21:00.

  5. #5
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using Qt instead of lt_dlsym (libtool)

    Ok solved, it was just add all the object modules *.o the calling program

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using Qt instead of lt_dlsym (libtool)

    Quote Originally Posted by giorgik View Post
    Ok solved, it was just add all the object modules *.o the calling program
    If you mean what I think you do then you have effectively compiled your library into your main program which means that you're not using the library at all since all the code is already in your main app.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt plugins - how to do a libtool-style autoload
    By KShots in forum Qt Programming
    Replies: 2
    Last Post: 7th February 2007, 12:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.