LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2011, 08:26 PM   #1
Malkom
LQ Newbie
 
Registered: Apr 2011
Posts: 5

Rep: Reputation: Disabled
Problem with C++, SDL and Code::Blocks


Hi.

First, this is my first post. Yay!

Okay, to business. I have a extremely irritating problem with getting Code::Blocks to compile my C++ SDL code. Here it is:

Code:
// Main program file of the SDL test.

#include <SDL.h>
#include <stdio.h>
#include <cstdlib>

int main() {
    SDL_Init(SDL_INIT_VIDEO);

    atexit(SDL_Quit);

    SDL_Surface *screen;

    screen = SDL_SetVideoMode(300, 300, 8, SDL_SWSURFACE);

    if(screen == NULL) {
        printf("Fatal error: Screen initialization failed.\n");
        exit(1);
    }

    atexit(SDL_Quit);

    

    return 0;
}

// This was copied straight from the SDL API Reference Guide. We'll
// see if it compiles...
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
    int bpp = surface->format->BytesPerPixel;
    /* Here p is the address to the pixel we want to set */
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

    switch(bpp) {
    case 1:
        *p = pixel;
        break;

    case 2:
        *(Uint16 *)p = pixel;
        break;

    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
            p[0] = (pixel >> 16) & 0xff;
            p[1] = (pixel >> 8) & 0xff;
            p[2] = pixel & 0xff;
        } else {
            p[0] = pixel & 0xff;
            p[1] = (pixel >> 8) & 0xff;
            p[2] = (pixel >> 16) & 0xff;
        }
        break;

    case 4:
        *(Uint32 *)p = pixel;
        break;
    }
}
I'm pretty sure the problem is with the code. The sample SDL project that C::B created for me runs fine with its default code, but when I plug mine into main.cpp it fails to compile with the following error message:

Code:

C:\MinGW32\lib\libSDLmain.a(SDL_win32_main.o)||In function `console_main':|
\Users\hercules\trunk\SDL-1.2\.\src\main\win32\SDL_win32_main.c|315|undefined reference to `SDL_main'|
||=== Build finished: 1 errors, 0 warnings ===|
Since it caused the same project that compiled the sample code to fail to compile this code, I'm sure that it was the code causing the problem. However, I've tried a number of things and been unable to fix it. Could you people give me a hand with this?

Thanks in advance.
 
Old 04-22-2011, 09:49 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Welcome to LQ.

The following is not a compilation issue; it is a linking issue.
Code:
... undefined reference to `SDL_main'
Anytime you see the "undefined reference" message, it is indicating to you that the function cannot be found. It may be due to cases where you have failed to implement the function, or it may be a case where you have failed to instruct the linker as to where it may find the function.

In your case, the linker is failing to find the SDL function which is implemented in an external library, notably libSDL.so. You need to figure out how to tell Code::Blocks that it needs to reference this library; typically one would specify "-lSDL", however it is better to specify "pkg-config --libs sdl". Similarly, for compiler flags, one specifies "pkg-config --cflags sdl".

I could tell you in a snap how to build your project from the command line, however since you are using and IDE, for which I care to know nothing about, I would expect you to RTFM concerning Code::Blocks on how to configure your compiler and linker options.
 
Old 04-22-2011, 09:54 PM   #3
Malkom
LQ Newbie
 
Registered: Apr 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
DARN it, read the WHOLE POST, man!
The sample app compiled fine, but my code did NOT! I DID add SDL to the linker links.
 
Old 04-22-2011, 10:03 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by Malkom View Post
DARN it, read the WHOLE POST, man!
The sample app compiled fine, but my code did NOT! I DID add SDL to the linker links.
Not according to the linker error you posted. Or are you wasting my time?
 
Old 04-23-2011, 01:27 PM   #5
alexcg
LQ Newbie
 
Registered: Mar 2010
Location: St. Petersburg, Russia
Distribution: Slackware, Debian
Posts: 13

Rep: Reputation: 2
Which libraries do you link against and in which order?
 
Old 04-23-2011, 01:41 PM   #6
alexcg
LQ Newbie
 
Registered: Mar 2010
Location: St. Petersburg, Russia
Distribution: Slackware, Debian
Posts: 13

Rep: Reputation: 2
Also, try to change the definition of the main function to:
Code:
int main(int argc, char *argv[])
{
  // put your code here
}

Last edited by alexcg; 04-23-2011 at 01:42 PM. Reason: Punctuation.
 
Old 04-23-2011, 03:05 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by alexcg View Post
Also, try to change the definition of the main function to:
Code:
int main(int argc, char *argv[])
{
  // put your code here
}
I often omit the arguments to main() and everything works fine. As far as I know it's not wrong to omit them.
 
Old 04-23-2011, 03:31 PM   #8
alexcg
LQ Newbie
 
Registered: Mar 2010
Location: St. Petersburg, Russia
Distribution: Slackware, Debian
Posts: 13

Rep: Reputation: 2
Quote:
Originally Posted by MTK358 View Post
I often omit the arguments to main() and everything works fine. As far as I know it's not wrong to omit them.
It's not wrong to omit them until you are writing a Windows application that uses the SDL library
As far as I remember the SDLmain.lib contains the WinMain function (which is often the entry point of a Windows application), which calls the main function and passes two arguments - argc and argv.
 
Old 04-23-2011, 03:38 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by alexcg View Post
It's not wrong to omit them until you are writing a Windows application that uses the SDL library
As far as I remember the SDLmain.lib contains the WinMain function (which is often the entry point of a Windows application), which calls the main function and passes two arguments - argc and argv.
I didn't know that.

But the OP is using MinGW. Is that like a Linux environment for Windows (or is it just a port of GCC, etc)? If so, then it needs to be written like a Linux SDL application.
 
Old 04-23-2011, 03:55 PM   #10
alexcg
LQ Newbie
 
Registered: Mar 2010
Location: St. Petersburg, Russia
Distribution: Slackware, Debian
Posts: 13

Rep: Reputation: 2
Quote:
Originally Posted by MTK358 View Post
Is that like a Linux environment for Windows?
No, it isn't. MinGW is just a port of GCC for writing native Windows applications.

The homepage of MinGW states that
Code:
MinGW, being Minimalist, does not, and never will, attempt to provide a POSIX runtime environment for POSIX application deployment on MS-Windows.
 
Old 04-24-2011, 02:26 PM   #11
Malkom
LQ Newbie
 
Registered: Apr 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks for the replies. However, I resolved this issue before I saw them. AlexCG was right -- the main() function needed argc and argv added. Apparently it looks for it under the name of SDL_main(), and so you need the arguments it expects.
 
Old 04-24-2011, 02:44 PM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Malkom View Post
I resolved this issue
Then mark the thread as solved.
 
Old 04-24-2011, 03:11 PM   #13
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by MTK358
Is that like a Linux environment for Windows?
You want Cygwin.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
code::blocks 10.05 does not highlight code? michaelinux Linux - Software 1 10-24-2010 11:25 AM
code::blocks elishac Linux - Software 40 01-25-2010 11:51 PM
Code::Blocks h3x0r Programming 2 04-13-2007 08:35 AM
SDL is installed, included and linked, but will not compile SDL code mansizerooster Programming 10 05-31-2006 04:18 AM
Sourcecompiling Problem...The sdl-config script installed by SDL could not be found. deepclutch Debian 1 12-15-2005 12:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration