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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With