Page 1 of 2

compiling with SDL issues

Posted: Sun May 23, 2010 10:03 pm
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
}

Re: compiling with SDL issues

Posted: Sun May 23, 2010 10:17 pm
by XianForce
Have you tried initializing SDL?

Re: compiling with SDL issues

Posted: Sun May 23, 2010 10:27 pm
by avansc
XianForce wrote:Have you tried initializing SDL?
what he has is a linking issues.
dont know if sdl init does linking.

Re: compiling with SDL issues

Posted: Sun May 23, 2010 10:30 pm
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

Re: compiling with SDL issues

Posted: Sun May 23, 2010 10:42 pm
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

Re: compiling with SDL issues

Posted: Sun May 23, 2010 10:49 pm
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.

Re: compiling with SDL issues

Posted: Sun May 23, 2010 10:54 pm
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

Re: compiling with SDL issues

Posted: Sun May 23, 2010 11:05 pm
by avansc
errrr....

Re: compiling with SDL issues

Posted: Tue May 25, 2010 7:46 pm
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;

Re: compiling with SDL issues

Posted: Tue May 25, 2010 10:09 pm
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.

Re: compiling with SDL issues

Posted: Tue May 25, 2010 10:17 pm
by eatcomics
Plus you often get linker issues, due to linking the same thing over again,

Re: compiling with SDL issues

Posted: Wed May 26, 2010 12:29 am
by X Abstract X
eatcomics wrote:Plus you often get linker issues, due to linking the same thing over again,
Use include guards.

Re: compiling with SDL issues

Posted: Wed May 26, 2010 8:25 am
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.

Re: compiling with SDL issues

Posted: Wed May 26, 2010 8:29 am
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.

Re: compiling with SDL issues

Posted: Wed May 26, 2010 4:35 pm
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"