noob's guide to using chipmunk with C++ anyone?

Official forum for the Chipmunk2D Physics Library.
Post Reply
Rawlyn
Posts: 33
Joined: Mon Mar 30, 2009 3:06 am
Contact:

noob's guide to using chipmunk with C++ anyone?

Post by Rawlyn »

Hey all,

I've been a big fan of pymunk for a while now, but something I'm working on could do with a little performance boost - so I've decided to try my hand at C++ and OpenGL. I downloaded chipmunk and ran "cmake ." (without the demos) and "make" and that all seems to be fine.

Problem is, I have no idea where to go from there.

This may seem like a really nooby thing to be asking (and rightly so), but what do I need to put into my source to include chipmunk? Do I need to do anything special with the options for g++ when I compile? Do I need to set up any environment variables to know where anything is? A step-by-step example would be great if anyone has the time.

Thanks in advance,
Rawlyn.
[url=http://www.xboxlc.com/profile/Rawlyn][img]http://www.xboxlc.com/cards/sig/black/Rawlyn.jpg[/img][/url]
Rawlyn
Posts: 33
Joined: Mon Mar 30, 2009 3:06 am
Contact:

Re: noob's guide to using chipmunk with C++ anyone?

Post by Rawlyn »

Update.... after a bit of fiddling, I've done this much:

- I ran "sudo make install" and crossed my fingers. This seemed to install all the headers to /usr/local/include and the lib to /usr/local/lib .

- I've set the environment variables
CPLUS_INCLUDE_PATH=/usr/local/include/chipmunk (g++ looks here for include apparently)
LD_LIBRARY_PATH=/usr/local/lib (found this in a tutorial http://www.adp-gmbh.ch/cpp/gcc/create_lib.html I was looking at for hints)

Now I can compile using:

g++ chiptest.cpp -o chiptest -lchipmunk -Wall -g

and my test program (includes chipmunk.h, calls cpInitChipmunk(), ends) now executes.

Does this sound about right to those of you who know about these things? I can't help but think there's something wrong with the environment variable to give the search path to the lib.
[url=http://www.xboxlc.com/profile/Rawlyn][img]http://www.xboxlc.com/cards/sig/black/Rawlyn.jpg[/img][/url]
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: noob's guide to using chipmunk with C++ anyone?

Post by Tam Toucan »

The -I option tells gcc where to look for header files. So -I /usr/local/include/chipmunk will add it to the search path so that #include <chipmunk.h> will be found.

The -L option tells the linker where to look for libraries specified by -l i.e. where to look for libchipmunk.a if you give it -lchipmunk.

You can have mulitple -I and -L options to add lots of paths.

LD_LIBRARY_PATH is the env var for the path to look for dynamically loaded libraries i.e. the linker can take a static library (.a) and put the code into your program at link time. Or it can take a dynamic library (.so) and look for it when your program runs.

You want to link statically since otherwise anyone running your program would need to have the library installed (or you would need to give them a copy and it be placed somewhere in the search path).

I actually don't install chipmunk into /usr/local and instead just have it as a library compiled as part of my projects. The reason I do this is it makes it easier to add debug (to find out when I've screwed up not Chipmunk :)), but if you're not needed to step through the code or debug it a lot then installing it is the way to go.

Hopefully of some help.
Rawlyn
Posts: 33
Joined: Mon Mar 30, 2009 3:06 am
Contact:

Re: noob's guide to using chipmunk with C++ anyone?

Post by Rawlyn »

Thanks Tom, that information is very useful to me right now!

How would I go about compiling chipmunk into a project like you suggest?
[url=http://www.xboxlc.com/profile/Rawlyn][img]http://www.xboxlc.com/cards/sig/black/Rawlyn.jpg[/img][/url]
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: noob's guide to using chipmunk with C++ anyone?

Post by Tam Toucan »

Well I use eclipse as my IDE, but with autoconf generated Makefiles underneath. For C++ development Code::Blocks is probably the best (I use Eclipse because I also do some Java, Flash, UML).

At the simplest level you can just have a Chipmunk directory with all the source and build it along with your source. There's nothing special about the Chipmunk code so you can just compile it as part of your own.

A better solution would be to have it as a separate project which you build and link against. So you have your own code project and a chipmunk project. The Chipmunk builds a library and your project references it i.e. the project settings tell it where the source code and library is. Unfortunately I've not used Code::Blocks, so I can't tell you how to do that. There is a thread here on using Code::Blocks that might help

TBH it wouldn't make much difference from the simple solution, since (guessing here since I've not used Code::Blocks) it would only really help if you have multiple projects i.e. they can all just reference the Chipmunk project, and it gets built once, but with the simple solution you might need multiple copies.

If you're not using anything and just want to compile it everything all the time something like this should work

Code: Select all

cd <your chipmunk source dir>
gcc --std=c99 -ffast-math -O3 *.c constraints/*.c
ar cru libchipmunk.a *.o
cd <your source dir>
g++ <your options> -I<your chipmunk source dir> -L<your chipmunk source dir> *.cpp -Lchipmunk
I've probably got a typo there, but you get the idea. Change the -O3 to -g if you are wanting to step through the code with a debugger (gdb), but you'll obviously lose performance with -g.
Rawlyn
Posts: 33
Joined: Mon Mar 30, 2009 3:06 am
Contact:

Re: noob's guide to using chipmunk with C++ anyone?

Post by Rawlyn »

Thanks again Tom, that's cleared everything up for me :) I was missing the -L option, so the static link to libchipmunk.a wasn't happening. With that in place everything builds perfectly.

Thanks also for the advice on IDEs. Currently I just use gedit, but will no doubt eventually be grateful for something to manage my code.
[url=http://www.xboxlc.com/profile/Rawlyn][img]http://www.xboxlc.com/cards/sig/black/Rawlyn.jpg[/img][/url]
Rawlyn
Posts: 33
Joined: Mon Mar 30, 2009 3:06 am
Contact:

Re: noob's guide to using chipmunk with C++ anyone?

Post by Rawlyn »

I just noticed your name is Tam, not Tom. Sincere apologies!
[url=http://www.xboxlc.com/profile/Rawlyn][img]http://www.xboxlc.com/cards/sig/black/Rawlyn.jpg[/img][/url]
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests