Thread: Issue with SDL type returns

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    20

    Question Issue with SDL type returns

    This is probably amateur-ish, but I've been going through the Lazy Foo Productions tutorials to learn how to use SDL, but since I'm such a stubborn programmer I decided to write my code in C rather than C++, everything I've been doing up unitl a certain point, works. But then I got tired of writing the same functions and variables, over and over again, so I decided to create a header to declare my global Macros and functions and intialized those in a C source file, problem is, I get some weird compiling errors now:

    Code:
    #ifndef SDL_TUTORIAL_HANDLER_H_INCLUDED
    #define SDL_TUTORIAL_HANDLER_H_INCLUDED
    
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    
    #define SCREEN_WIDTH    640
    #define SCREEN_HEIGHT   480
    #define SCREEN_BPP      32
    
    #define TRUE    1
    #define FALSE   0
    
    SDL_Surface *load_image( char* filename );
    void apply_surface( int Pos_X, int Pos_Y, SDL_Surface* source, SDL_Surface* destination );
    
    typedef int Bool;
    
    Bool init();
    Bool load_files();
    void clean_up();
    
    #endif // SDL_TUTORIAL_HANDLER_H_INCLUDED
    This is all fine, but depending on where my pointers are for the C file:

    Code:
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    
    #include "SDL_Tutorial_Handler.h"
    
    SDL_Surface *image  = NULL;
    SDL_Surface *screen = NULL;
    
    SDL_Surface *load_image( char* filename ){
        SDL_Surface* loaded_image       = NULL;
        SDL_Surface* optimized_image    = NULL;
    
        loaded_image = IMG_Load( filename );
    
        if ( loaded_image != NULL ){
            optimized_image = SDL_DisplayFormat( loaded_image );
    
            SDL_FreeSurface( loaded_image );
    
            }
    
        return *optimized_image; \* There is a return error here *\
    
        }
    
    void apply_surface( int Pos_X, int Pos_Y, SDL_Surface* source, SDL_Surface* destination ){
        SDL_Rect offset;
    
        offset.x = Pos_X;
        offset.y = Pos_Y;
    
        SDL_BlitSurface( source, NULL, destination, &offset );
    
        }
    
    Bool init(){
        if ( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ){
            return FALSE;
    
            }
    
        screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
        if ( screen == NULL ){
            return FALSE;
    
            }
    
        SDL_WM_SetCaption( "Image Library Tutorial", NULL );
    
        return TRUE;
    
        }
    
    Bool load_files(){
        image = load_image( "Hello.png" );
    
        if ( image == NULL ){
            return FALSE;
    
            }
    
        return TRUE;
    
        }
    
    void clean_up(){
        SDL_FreeSurface( image );
    
        SDL_Quit();
    
        }
    I get slapped with a multiple definitions error, so I changed those pointers and now I get a single error telling me that the return type for the *load_image function is incompatible for the function, even though they are both SDL_Surface. For anyone who can understand what I'm talking about, any advice?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Try
    Code:
    #include <SDL/SDL.h>
    #include <SDL/SDL_image.h>
    Using quotes stuffs the contents of the header file directly into your C file when it pre-processes. This means you're doing it once in your .h file and again in your .c file. The <> processes it differently. Not sure what compiler you're using, but here's what gnu/gcc says about it: Include Syntax - The C Preprocessor.

    EDIT: I don't think that applies if they used include guards, which I'm sure they did. I will have to think this over this some more.
    Last edited by anduril462; 12-20-2010 at 12:02 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to return a pointer, return a pointer. "optimized_image" is a pointer. "*optimized_image" is not.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you change the return type for load_image in the .c file, you have to change it in your .h file too, otherwise the compiler gets confused. Some actual compiler error messages would be nice if that doesn't fix it.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    20

    Unhappy Hmm...

    The preprocessor loads didn't really change much, and if I change the variable to a pointer it reports all the functions as multiple definitions. Neither of these things did much.

    I'm using MinGW GCC, in case you're wondering.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ahernan17 View Post
    The preprocessor loads didn't really change much, and if I change the variable to a pointer it reports all the functions as multiple definitions. Neither of these things did much.

    I'm using MinGW GCC, in case you're wondering.
    I have no idea what this could even mean. The variable already is a pointer, so how you could change the variable to be a pointer doesn't follow.

    Multiple definitions generally means either "I've put a function in a header file" or "I'm including a .c file because I don't know how to work the linker".

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please be more specific with what you changed. "those pointers" and "the variable" are a bit vague. Give the variable names what are the old and new types are. What did you change the return type for load_image to? What are the exact error messages you get for each case?

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    Yeah, it doesn't make sense to me either, all I know is that a simple pointer operator on that variable (or any other for that matter), solves the multiple definition issue, but then there is the incompatibility issue left.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    "*optimized_image", this is the one giving me trouble.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ahernan17 View Post
    "*optimized_image", this is the one giving me trouble.
    Again, "optimized_image" is a pointer. This means that "*optimized_image" isn't a pointer. If you want to return a pointer, you need to return a pointer, not not-a-pointer.

    And again, make sure you aren't #include'ing a .c file.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    I know that, but if I change to an actual pointer (optimized_image), all my functions become invalid because of "multiple definitions" which they aren't.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    I'm not even sure if that particular variable (optimized _image), is the problem.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ahernan17 View Post
    I know that, but if I change to an actual pointer (optimized_image), all my functions become invalid because of "multiple definitions" which they aren't.
    If they weren't multiple definitions, you wouldn't be getting the error. "Multiple definitions" is a linker problem, not a compiler problem, which is why you didn't see it earlier -- if the files don't even compile, the linker doesn't even get called. You have two separate source files with the same function in them, which then are linked together -- or at least try to get linked together. But they can't be linked together, because they each have a function with the same name. So look at your .c files, and figure out which of them contain functions with the same name.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Normally if you get a redefinition error, it tells you where the old definition was and where the new, conflicting one is. Maybe you can glean some insight from those messages, and share the offending lines with us so we can take a look too.

  15. #15
    Registered User
    Join Date
    Sep 2010
    Posts
    20

    Unhappy You'd think that'd be the problem but...

    There are no reports of these functions being declared more than once, which I find to be very odd.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 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. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  5. ftn that returns type
    By ariamundi in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2001, 03:54 PM

Tags for this Thread