Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ SDL on macosx without Xcode

Tags:

c++

macos

g++

sdl

System: black Macbook running Mac os X 10.5.5 (Leopard)

I want to compile an SDL hello-world application using only g++. Xcode is good for macintosh but I want cross-platform compatibility, so I won't use any of the coaca framework (no menus, no buttons, etc). Also, porting Xcode projects to other os's is not something that sounds fun. I downloaded and installed SDL into /Library/Frameworks.

The big question is: what goes in the makefile (assuming just a helloWorld.cpp file in the source). I would like to avoid modifying the Helloworld file found here if possible.

like image 403
Kevin Kostlan Avatar asked Jan 04 '11 06:01

Kevin Kostlan


2 Answers

Took me a little while to figure this out myself, only I was already using SDL and was transitioning from C to C++ on Lion. Its not just the makefile that is the issue here, its probably your source file as well...

Download SDL-version.tar.gz

Extract and run at prompt

./configure --prefix=/home/user/SDL && make && make install

Assuming everything actually built, you now can use the sdl-config to build your source by executing:

g++ Main.cpp -o Main `/home/user/SDL/sdl-config --cflags --libs` \
    -framework OpenGL -framework Cocoa

which is the same as

g++ Main.cpp -o Main -I/home/user/SDL/include \
    -L/home/user/SDL/lib -lSDLmain -lSDL -framework OpenGL -framework Cocoa

Now the key here is that you are using C++... for SDL macros to correctly replace your main, you need to prevent the C++ compiler from mangling you main function call. To do this declare your main like the following:

extern "C" int main(int argc, char ** argv)
{
}

If you don't include the extern "C" stuff, C++ will change the name of your main function and SDL won't be able to automatically find it. If you don't use the int main(int argc, char ** argv) function signature, C++ will complain about type mismatches... so you've got to do it verbatim. (If using GCC, you exclude the extern "C" portion)

Chenz

like image 170
Crazy Chenz Avatar answered Nov 12 '22 01:11

Crazy Chenz


Try this:

all: helloWorld

helloWorld: helloWorld.o
    g++ -o helloWorld helloWorld.o `sdl-config --libs`

helloWorld.o: helloWorld.cpp
    g++ -c `sdl-config --cflags` helloWorld.cpp

sdl-config is a tool which should have come with your SDL install that outputs appropriate compiler and linker flags for when compiling with SDL.

like image 29
Null Set Avatar answered Nov 12 '22 03:11

Null Set