Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chown33

Moderator
Staff member
Aug 9, 2009
10,753
8,441
A sea of green
Please post the complete text of the error. It's unclear whether you're getting a C compiler error or a linking error. My guess is it's a C compiler error, but guessing is generally a poor approach to debugging.

Also post what the #includes are for the file with that line of code in it.


You'll need to include the CoreFoundation framework in order to get the typedef for CFBundleRef and the function declaration for CFBundleGetMainBundle. Here's an example showing the correct includes:

Be sure to read the linked discussion here:

I found those results with search terms: cfbundleref include file


If you haven't added the CoreFoundation framework to the project, then the linker will fail. A likely failure at the linking stage is an unresolved CFBundleGetMainBundle() or similar CFBundle function.
 

scott455

macrumors member
Original poster
Jun 5, 2022
40
0
Code:
#include <CoreFoundation/CoreFoundation.h>
Above include line solved the compilation error.
However, CFBundleCopyResourceURL returns null. In my code, I need to know the path the file projfile.bin located so that I can pass it to open file routine.

Code:
CFBundleRef mb = CFBundleGetMainBundle();
CFURLRef ur = CFBundleCopyResourceURL(mb, CFSTR("projfile"), CFSTR("bin"), NULL);

I dragged and dropped the Fldr1 to my xcode project. this is my xcode project tree:
Code:
-proj1
    |->
        -Proj1
            |->
                -Fldr1
                    |->
                        -projfile.bin
               -main.c
 
Last edited:

Senor Cuete

macrumors 6502
Nov 9, 2011
423
30
I use:
Code:
NSBundle *mainBundle = [NSBundle mainBundle];
and
Code:
 NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
Is there some reason to use a CFBundleRef?
You say that it's a C project. You might have to do this in a .m file and #include the cocoa headers to access the Cocoa APIs.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,753
8,441
A sea of green
Code:
#include <CoreFoundation/CoreFoundation.h>
Above include line solved the compilation error.
However, CFBundleCopyResourceURL returns null. In my code, I need to know the path the file projfile.bin located so that I can pass it to open file routine.

Code:
CFBundleRef mb = CFBundleGetMainBundle();
CFURLRef ur = CFBundleCopyResourceURL(mb, CFSTR("projfile"), CFSTR("bin"), NULL);

I dragged and dropped the Fldr1 to my xcode project. this is my xcode project tree:
Code:
-proj1
    |->
        -Proj1
            |->
                -Fldr1
                    |->
                        -projfile.bin
               -main.c
The structure of your Xcode project isn't necessarily relevant. What's important is the structure within the app-bundle. That is, the code running in the app only sees the structure within the app-bundle. The code doesn't see the Xcode project.

First, exactly what files and dirs are being placed into your app-bundle? You'll need to look inside the actual app-bundle in the Xcode output dir for your project, and expand all the folders there.

Do you see a "projfile.bin" anywhere in the app-bundle? If so, what is its relative pathname within the bundle?

If you don't see a "projfile.bin" anywhere in the app-bundle, then you'll need to add some actions to the build process in your Xcode project. The action should be to copy the "projfile.bin" from its location within the project files into the app-bundle at your desired destination.

As a test before adding to the Xcode build process, you can manually copy and paste a "projfile.bin" into the app-bundle, then run your app to see if it correctly finds the file. This lets you see what the Xcode build steps need to produce.
 

scott455

macrumors member
Original poster
Jun 5, 2022
40
0
need to state that this is a console application in xcode / C. Maybe I got it wrong; console applications do not have bundle app folder. There is a executable folder. I followed the step to manually copy the projFile.bin to folder that has the executable , did that and worked. Now, I am trying to come up with bundle function calls that in the main does the copying.
 
Last edited:

mfram

Contributor
Jan 23, 2010
1,311
352
San Diego, CA USA
I'm confused. Console (i.e., command-line) utilities in C don't have bundles. So why are you messing with bundle reference calls for such a utility? If you want to know the pathname of where the utility is called you can simply look at argv[0] in main().
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.