Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example usage as a static library? #7

Closed
ramiroaraujo opened this issue Jan 27, 2014 · 17 comments
Closed

example usage as a static library? #7

ramiroaraujo opened this issue Jan 27, 2014 · 17 comments

Comments

@ramiroaraujo
Copy link

disclaimer: I'm hugely noob with Obj-C, so I might be saying nonsense.

Is it possible to compile this as a static library to use in other projects? I'm trying to bundle this within my own binary for a cli app, but having a hard time figuring out what to do, or if it's even possible.

cheers

@mysteriouspants
Copy link
Owner

I just pushed up 76e2038 which includes a static library build of Argument Parser.

If you were to do this yourself, what you would do is add the source files for ArgumentParser and CoreParse to your project and ensure that you add the -fno-objc-arc flag to all the CoreParse files in Build Phases/Compile Sources.

Let me know if you have any further questions!

@ramiroaraujo
Copy link
Author

you rock!

@mysteriouspants
Copy link
Owner

Or actually I lied. The change set was 76e2038 (original comment has been updated). Today I learned that the ellipses in a Git push readout are not a contraction! (Specifically 92457b3..76e2038 means "went from 92457b3 to 76e2038").

@ramiroaraujo
Copy link
Author

:)

@ramiroaraujo
Copy link
Author

noob question n2: shouldn't the static library have a header file also?

@mysteriouspants
Copy link
Owner

In this case, the header file will be FSArguments.h and you'll need to ensure that all the other .h files are accessible.

@ramiroaraujo
Copy link
Author

you say I should copy all header files from your library (22 headers) onto my project? and the CoreParse files? I'm heavily lost :(

@mysteriouspants
Copy link
Owner

So when you build the static library it produces ArgumentParser-static.a file, which is just a bunch of object code that needs to be compiled into your library. The .a file itself doesn't have any information about the binary layout of itself. So there's really no way to tell where FSArgumentPackage is or any nonsense like that. You need to provide a way to symbolicate the binary, and we do that with a header file.

Therefore, once you build the ArgumentParser.a file, you copy the headers too which provides the information on how to use the .a file.

A slightly easier approach is to drag the ArgumentParser Xcode project into your own project and then mark the static library target as a dependency of your own build target. That's a little Xcode-specific, however, and therefore you're probably better served finding a tutorial for it. A quick Bing search revealed that Apple has a tutorial, though depending on your level of comfort with Xcode and the legendary terseness of Apple's docs, that might not be all the tutorial you'll need.

@ramiroaraujo
Copy link
Author

thanks for the help, it's terse indeed! I'm advancing, but not yet quite there... I'll keep smashing my head a little more, it's usually a painful but fast way of learning things deeply. :)

@ramiroaraujo
Copy link
Author

Ok, I'm really stuck. I tried 3 different ways, 2 of them compile, but give runtime exceptions, luckily the same :)

setup: default xcode OSX command line tool application, replace main.m content with ArgumentParser/example/desc.m

demo app: https://github.com/ramiroaraujo/cli-test
ArgumentParser fork: https://github.com/ramiroaraujo/ArgumentParser
place both projects in the same parent folder to have linked dependencies working without config.

Below are the 3 options, with the description of the steps I took, although if you checkout the branches in the demo apps the steps are already done.

Option 1: Link untouched ArgumentParser project
reproduce it by using the link-project branch in cli-test, and master branch in ArgumentParser

  • I linked the ArgumentParser code
  • I added the linked project to header search paths
  • I added libArgumentParser-Static.a to Build Phases -> Link Binary With Libraries

It doesn't compile, I get Match-O Linker errors, indicating Undefined symbols like OBJC_CLASS$_CPItem

Option 2: Link modified ArgumentParser project
reproduce it by using the link-project branch in cli-test, and static-dependencies branch in ArgumentParser

  • cli-test project remains the same as Option 1
  • I added missing implementation files from CoreParse in Build Phases -> Compile Sources in ArgumentParser project, and added the -fno-objc-arc flag to them. It had 47 items, it now has 58.

It compiles perfectly, it outputs text correctly with the -h flag, but then it shows:
https://gist.github.com/ramiroaraujo/8672441

Option 3: Add ArgumentParser and CoreParse sources to test-cli project
_reproduce it by using the copy-sources branch in cli-test. ArgumentParser project is not needed.

  • I copied the ArgumentParser and CoreParse source code and header files into the cli-test project
  • I added all ArgumentParser and CoreParse implementation files to Build Phases -> Compile Sources, and added the -fno-objc-arc flag to CoreParse files.

It compiles perfectly, it outputs text correctly with the -h flag, but then shows the exact same error as Option 2:
https://gist.github.com/ramiroaraujo/8672441

I think I've reached far beyond my skills here. Any more help or hints are highly appreciated!

Cheers!

@ramiroaraujo
Copy link
Author

follow up:
Using the same technique with https://github.com/barrelage/BRLOptionParser it worked just fine :)
Happy to have learned a lot, and anxious to see what went wrong with yours :)

@mysteriouspants
Copy link
Owner

It would appear that you're trying to use descriptionForLocale:indent: on an FSArgumentSignature instance. This method hasn't been implemented on that class (although I probably should).

If you instead use descriptionForHelp:terminalWidth: it should function as expected.

@mysteriouspants
Copy link
Owner

I should also note that the first argument of descriptionForLocale:indent is expecting either nil or an instance of NSLocale.

@ramiroaraujo
Copy link
Author

not sure I'm following. I copied examples/desc.m into main.m exactly. Also I don't see that code using descriptionForLocale, although it does use descriptionWithLocale

@mysteriouspants
Copy link
Owner

So it would appear that there is a bug in my example file that I need to fix. It should be calling descriptionForHelp:terminalWidth: (which I am now experiencing strange urges to rename descriptionForHelpWithIndent:terminalWidth:).

@mysteriouspants
Copy link
Owner

The example has been fixed, as well as the method renamed to be more idiomatic. (See 4e2c7d9 and 43df3eb).

@mysteriouspants
Copy link
Owner

I have no idea why this was still open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants