Thread: How to work with different libraries in one project?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    How to work with different libraries in one project?

    Hi,
    I tried to work with two different libraries on the same project but something went wrong. Basically I'd like to know how to organize a project in C++, how to prevent collision between libraries and so.
    What I wanted to do is control a graphic element (SDL library) with my iPhone (through OSC). Just use the accxyz values (accelerometer) to move an image on the screen.
    For the OSC part I'm using the liblo library and it works just fine from console. The problem is when I put the code inside the SDL code.
    It works for about 30 seconds and then crashes with "segmentation fault". Is it related to the fact that both libraries using threads and they collide each other?

    The code for the OSC stuff based on this one:
    http://liblo.sourceforge.net/examples/example_server.c

    The code combined with SDL is:
    Code:
    #ifdef __cplusplus
        #include <cstdlib>
    #else
        #include <stdlib.h>
    #endif
    #ifdef __APPLE__
    #include <SDL/SDL.h>
    #else
    #include <SDL.h>
    #endif
    #include "lo/lo.h"
    
    int img_x, img_y;
    
    void error(int num, const char *m, const char *path);
    int generic_handler(const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
    
    int main ( int argc, char** argv )
    {
        // liblo - OSC library
        lo_server_thread st = lo_server_thread_new("8000", error);
        lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);
        lo_server_thread_start(st);
    
        // initialize SDL video
        if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
        {
            printf( "Unable to init SDL: %s\n", SDL_GetError() );
            return 1;
        }
    
        // make sure SDL cleans up before exit
        atexit(SDL_Quit);
    
        // create a new window
        SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
                                               SDL_HWSURFACE|SDL_DOUBLEBUF);
        if ( !screen )
        {
            printf("Unable to set 640x480 video: %s\n", SDL_GetError());
            return 1;
        }
    
        // load an image
        SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
        if (!bmp)
        {
            printf("Unable to load bitmap: %s\n", SDL_GetError());
            return 1;
        }
    
        // centre the bitmap on screen
        SDL_Rect dstrect;
        img_x = (screen->w - bmp->w) / 2;
        img_y = (screen->h - bmp->h) / 2;
        dstrect.x = img_x;
        dstrect.y = img_y;
    
        // program main loop
        bool done = false;
        while (!done)
        {
            // message processing loop
            SDL_Event event;
            while (SDL_PollEvent(&event))
            {
                // check for messages
                switch (event.type)
                {
                    // exit if the window is closed
                case SDL_QUIT:
                    done = true;
                    break;
    
                    // check for keypresses
                case SDL_KEYDOWN:
                    {
                        // exit if ESCAPE is pressed
                        if (event.key.keysym.sym == SDLK_ESCAPE){
                            done = true;
                            break;
                        }
                        if (event.key.keysym.sym == SDLK_RIGHT){
                            if(img_x<640-bmp->w){
                                img_x += 10;
                            }
                            break;
                        }
                        if (event.key.keysym.sym == SDLK_LEFT){
                            img_x -= 10;
                            break;
                        }
                    }
                } // end switch
            } // end of message processing
    
            // DRAWING STARTS HERE
    
            // clear screen
            SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));
    
            // draw bitmap
            dstrect.x = img_x;
            dstrect.y = img_y;
            SDL_BlitSurface(bmp, 0, screen, &dstrect);
    
            // DRAWING ENDS HERE
    
            // finally, update the screen :)
            SDL_Flip(screen);
        } // end main loop
    
        // free loaded bitmap
        SDL_FreeSurface(bmp);
    
        // all is well ;)
        printf("Exited cleanly\n");
        return 0;
    }
    
    void error(int num, const char *msg, const char *path)
    {
        printf("liblo server error %d in path %s: %s\n", num, path, msg);
    }
    
    int generic_handler(const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
    {
        int accval_x = argv[0]->f*10;
        int accval_y = argv[1]->f*10;
    
        if(strcmp(path, "/accxyz")==0){
            img_x += accval_x;
            img_y -= accval_y;
        }
    
        return 1;
    }
    Thanks

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You should be able to work with more than one library sure, maybe check SDL forums for any known issues relating to your problem. I do not think it is down to your project setup as If it compiles and builds to an executable then I would assume that part is ok...? No expert at all on these things myself, have you tried debugging, traces etc?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    I've found something. It's crashes whenever there is other message from OSC.
    in 'generic_handler' I have 'if(strcmp(path, "/accxyz")==0)' but I don't want any change if I get '/ping' for example or other message so why does it crash?
    Thanks,
    Last edited by Yaniv Vaish; 02-29-2012 at 06:32 AM.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Ok, I've found my big mistake. I left the declaration of accval_x, accval_y outside the if...)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wrong place? All I see is C, C, C and more C. Nowhere do I see C++...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    Wrong place? All I see is C, C, C and more C. Nowhere do I see C++...
    You mad bro... you should have had a closer look:

    Code:
    #ifdef __cplusplus
        #include <cstdlib>
    #else
        #include <stdlib.h>
    #endif

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you compile this with C++, you will/should get compile errors anyway, suggesting that this really isn't supposed to be compiled with C++.
    It's C, plain and simple.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. libraries in Bjarne Stroustrup's book wont work
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2011, 01:07 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Can't make a simple Project work
    By kantze in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2007, 03:21 AM
  4. Including libraries in project?
    By elliptic in forum C Programming
    Replies: 5
    Last Post: 06-05-2006, 03:09 PM
  5. I know OpenGL...now i want a good project 2 work on...
    By Zeeshan Zia in forum Game Programming
    Replies: 2
    Last Post: 10-20-2001, 12:18 PM