compiling with SDL issues

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

pythip
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sat Apr 24, 2010 11:25 pm

compiling with SDL issues

Post by pythip »

I am attempting to compile a simple test project with SDL with mingw on xp, have it working perfectly in ubuntu, and mingw works good, I should mention that I made a path system variable to C:\MingGw, so I can type c++ main.cpp -o test in cmd. I attempt to compile by using the following line "c++ -o test test.cpp -mwindows -lSDLmain -lSDL". The error message I receive is:
/mingw/lib/libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `Win
Main@16'
collect2: ld returned 1 exit status"

Code: Select all

#include <stdio.h>
#include "SDL/SDL.h"
      // include the SDL headers you put in /mingw/include/SDL/

int main(int argc, char *argv[]) {
      // with SDL, you need the argc and argv parameters

  printf("Hello world\n");
        // with SDL, anything you printf will get printed to the
        // file stdout.txt in the current directory, not to the screen
}
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: compiling with SDL issues

Post by XianForce »

Have you tried initializing SDL?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: compiling with SDL issues

Post by avansc »

XianForce wrote:Have you tried initializing SDL?
what he has is a linking issues.
dont know if sdl init does linking.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: compiling with SDL issues

Post by eatcomics »

that there is pretty good advice :P

Code: Select all

SDL_Init(SDL_INIT_EVERYTHING);
just put that at the beginning of main... and that oughta do er
and as for the linking, you want that in their even if it doesn't fix it...
and also, main needs to return an int
return 0; at the end
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: compiling with SDL issues

Post by XianForce »

avansc wrote:
XianForce wrote:Have you tried initializing SDL?
what he has is a linking issues.
dont know if sdl init does linking.
Ahh, yeah makes sense haha =p
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: compiling with SDL issues

Post by avansc »

XianForce wrote:
avansc wrote:
XianForce wrote:Have you tried initializing SDL?
what he has is a linking issues.
dont know if sdl init does linking.
Ahh, yeah makes sense haha =p
It is possible that it can fix that error. not sure.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
pythip
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sat Apr 24, 2010 11:25 pm

Re: compiling with SDL issues

Post by pythip »

I switched to code blocks and followed lazy foos tutorial this time, the code I posted was from some tutorial, I have SDL and SDL_image working now, but when I try to compile my real project it complains about redefinitions, I have 5 file, (main.cpp, functions.cpp, functions.h, globals.cpp, globals.h). I know this code works under linux using gcc.
The includes are as follows:
edit: changing functions.cpp and globals.cpp to .h makes a screen just open and close, forgot to put a picture in the folder, now it works thanks for the help everyone!
main

Code: Select all

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "functions.cpp"
#include "globals.cpp"
functions.cpp

Code: Select all

#include "functions.h"
#include "globals.h"
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
functions.h

Code: Select all

#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <SDL/SDL.h>
#endif
globals.cpp

Code: Select all

#include "globals.h"
globals.h

Code: Select all

#ifndef GLOBALS_H
#define GLOBALS_H
#include <SDL/SDL.h>
#endif
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: compiling with SDL issues

Post by avansc »

errrr....
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: compiling with SDL issues

Post by eatcomics »

Don't include headers in headers, they're bad... declare your prototypes in the include, then include the files you need in the header files cpp file with all the actual definitions
like

main.cpp

Code: Select all

#include header.h
int main(){yo}
header.h

Code: Select all

void ADD3AND3();
header.cpp

Code: Select all

#include <whatever yougotta include
void ADD3AND3() { return 3 + 3;
Image
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: compiling with SDL issues

Post by X Abstract X »

eatcomics wrote:Don't include headers in headers, they're bad...
Doing this just makes including headers a pain in the ass because you have to make sure you include them in the proper order. The header file you create should include all the dependencies and the cpp file should only include it's associated header. A header file should be able to compile with it's cpp file left empty.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: compiling with SDL issues

Post by eatcomics »

Plus you often get linker issues, due to linking the same thing over again,
Image
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: compiling with SDL issues

Post by X Abstract X »

eatcomics wrote:Plus you often get linker issues, due to linking the same thing over again,
Use include guards.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: compiling with SDL issues

Post by Falco Girgis »

eatcomics wrote:Don't include headers in headers, they're bad...
You do realize that while it is definitely good practice to keep header includes from headers to a minimal, simple class/function prototyping often isn't enough? Consider class A inheriting from class B. Class A REQUIRES Class B to be included, so that the compiler knows how much space to allocate in memory for an instance of class A.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: compiling with SDL issues

Post by Falco Girgis »

X Abstract X wrote:
eatcomics wrote:Don't include headers in headers, they're bad...
Doing this just makes including headers a pain in the ass because you have to make sure you include them in the proper order. The header file you create should include all the dependencies and the cpp file should only include it's associated header.
Cramming every include into a header file requires the header (and EVERY resulting .cpp including the header) to be rebuilt if any of the dependencies change. If you use function/class prototypes in the header file to begin with, only the .cpp files that actually USE the header files are rebuilt upon a dependency change. When you get into large applications, this can make the difference between a compile time taking 2 seconds and literally minutes.
X Abstract X wrote: A header file should be able to compile with it's cpp file left empty.
A header file has absolutely no dependence on a .cpp file whatsoever (unless you're #including a .cpp from within the header). This doesn't make any sense.
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: compiling with SDL issues

Post by X Abstract X »

GyroVorbis wrote:
X Abstract X wrote: A header file should be able to compile with it's cpp file left empty.
A header file has absolutely no dependence on a .cpp file whatsoever (unless you're #including a .cpp from within the header). This doesn't make any sense.
What I meant was that something like the following would not compile until you include <string> in the .cpp file (and it would have to be included before including "A.h"). Which is why you should include <string> inside the header instead, unless you can get away with a forward declaration instead. Am I wrong about this?

A.h

Code: Select all

#ifndef A_H
#define A_H

class A {
public:
    void doShit(const std::string& myString);
};

#endif
A.cpp

Code: Select all

#include "A.h"
Post Reply