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

NeoDarkboy

macrumors newbie
Original poster
Mar 10, 2011
6
0
Hi all, i'm new in Mac programming, and i have a question: is it posible to declare the main function inside a library? Because when i compile the main project, i get the error undefined symbol: _main, and i know is because is missing the int main, but i already defined it on a library.a, that lib is added to the main project, so i dont have any clue of why is causing this error.

If i define a main.m with the main function inside, i get the 2 main functions defined error, this is kind of weird.

Anybody have any clue for this?

By the way, i need the main in the library because i'm making a generic code, and need to do some specific stuff in the main

Regards
 
If you really are going to have the starting point for your program inside a library (which seems nonsensical), I would call it myMain or something similar and then each program that you link to this library will have its own main that just calls myMain in the library. I couldn't tell you about the intricacies of the compiler and linker and why you're having problems with it, but it definitely strikes me as wrong to have main in an object archive.

-Lee
 
it definitely strikes me as wrong to have main in an object archive.

It certainly doesn't seem very "generic" or portable to have main in an archive/library.

So, someone else using your code can't use their own main and do any specific setup they need to?

Maybe if you explain what specifically leads you to this conclusion you might find a better solution.

B
 
lol, ok, in the main, i initialize my application, and since is a library, all applications will start up in the same way, so there is no point of having a main repeated in every application, so that's why i decided to have the main in a library. It's like the main created for every cocoa application, has a:

return NSApplicationMain(argc, (const char **) argv);


you say, ok someone will need to do some setup, that could be done in the delegate class, so i think is better to have a main there, or what should i do?

Or im wrong and i should do something like Lee says, create an init method and call it from a main for every application? Or is there another way?

THank's for your repplies :)
 
Well, you just gave an example of NSApplicationMain. That is pretty much the same as my example of a myMain in the library and each application having its own main that calls myMain.

I couldn't parse the piece about the delegate situation.

If you want a single entry point into your library, and to control the whole of application execution in there, that's fine. You don't have to have a separate init method called from main if it makes more sense to have a single entry point that is intended to manage the whole lifecycle of the app. You just can't call it main. My suggestion stands, and i think your NSApplicationMain example reaffirms it.

-Lee
 
oooh ok, now i see :p


so then, i should have a createApplication in my library, seems ok to me


Thank you
 
Theoretically, you could just subclass NSApplication, create a framework, and build all you applications against that framework. It could be as simple as

Code:
// just override one method in the subclass

- (void)run {

    // do your special initialization here

    [super run];

    // you could also put common wrap up code here

}

This could indeed be the simplest way to put a common main function into a group of Cocoa apps. This is kind of like exactly what object oriented programming was designed for. Use this technique cautiously, though. Do not just jump into it without first reading all the available documentation. For instance, I would not count on any AppKit objects behaving in predictable ways prior to the call to super.
 
yeah, that's one of the things i'm doing, i have a View class that inherits from NSView in the library, and in the main project another class, ApplicationView, inherits from View class, so if there should be any modification, is done in the ApplicationView, i will try to subclass from NSApplication
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.