views:

236

answers:

3

I am trying out lua script with C++ in Mac OS X. I was finding a way to make the program returning the current working directory. That's no problem with getcwd, but then I came one thing:

My foo.lua stays at its initial path only. When I compile program, it is not being copied over to the build/Debug directory. Sure, I can grab my script there, but that's just impractical. XCode or any IDE should carry resources to the build zone. XCode does this automatically with iPhone app, but this seems to be a different case. For this case, how to command XCode to put the respective resources in the build directories?

int main (int argc, char * const argv[]) {

...
...

    luaL_dofile(luaVM,"/Users/yourNameHere/Desktop/LuaSandbox/LetsTryLua/foo.lua");
    //typing the whole absolute path here is just ugly and impractical.
...
...

    printf("working directory: %s", buffer);
    //output is: working directory: /Users/yourNameHere/Desktop/LuaSandbox/LetsTryLua/build/Debug

...
...
+2  A: 

Because you're using a .lua file as a resource, I suspect that isn't recognised as a standard resource type and hence it hasn't been automatically copied. You should be able to do this though by adding an extra Copy Bundle Resources build step to your target and then add your file to it in the project view.

the_mandrill
+1  A: 

If you're creating a command line tool that is not a bundle, then there's never going to be a good solution. If you're creating a regular app then the aforementioned solution will work, but you're going to have to stop assuming that your working directory is set to anything even remotely meaningful at any point in time and use the appropriate methods for finding resources stored within your bundle.

Azeem.Butt
+2  A: 

Rather than hard code the path to your Lua script you may want to use the NSBundle API's to find it:

NSBundle * mainNSBundle = [NSBundle mainBundle]; NSString * luaFilePath = [mainNSBundle pathForResource:@"foo" ofType:@"lua" inDirectory:NULL forLocalization:NULL];

luaL_dofile(luaVM,[luaFilePath UTF8String]);

This will find it in the bundle's folder (if you added the "Copy Bundle Resources" build step to your target as the above poster suggested.

geowar