How can I reference a static, native library in NativeScript iOS? - nativescript

I found a sample project describing how to do this here:
https://github.com/NativeScript/sample-iOS-StaticLibs
But I think it is out of date - it doesn't work on {N] 1.3. I've put together a very, very simple test - a static library containing one class, with one static string. I've successfully added it to Xcode by adding the .a file to the linked binaries, and the path to the .h file in the header search paths. So far so good - if I reference it in the native code it finds it. However, when I try to do the same in a JS file, it cannot find the variable.
There are no errors associated with it, so I'm not sure where to start looking. Any ideas?

You have to add module.modulemap file to your library to instruct metadata generator for which headers should generate metadata. You can read more about modulemap files here. You can find an example of modulemap file here. In your scenario, you have to add your module.modulemap in the same folder as your .h file and its content should be something like this:
module LibraryName {
umbrella header "HeaderName.h"
export *
module * { export * }
}

Related

Xcode and OpenCV

I am trying to compile a C++ project that uses OpenCV in Xcode 6.2. I have downloaded OpenCV and the header and library files and setting as follow:
Header Search Path: /usr/local/include/opencv2**
Library Search Path:/usr/local/lib
and opencv is truly in this Path,but I get error:
Lexical or Preprocessor Issue
/Users/radio_lee/Desktop/project/OpenCV/OpenCV/main.cpp:1:10: 'cv.h' file not found
when I change the Header Search Path and Library Search Path like that:
Header Search Path:
/usr/local/include/opencv2** and /usr/local/include/opencv
Library Search Path:/usr/local/lib
another error:
Lexical or Preprocessor Issue /usr/local/include/opencv/cv.h:63:10: 'opencv2/core/core_c.h' file not found
/Users/radio_lee/Desktop/project/OpenCV/OpenCV/main.cpp:1:10: In file included from /Users/radio_lee/Desktop/project/OpenCV/OpenCV/main.cpp:1:
OpenCV is a Private Framework,Since it is a Private Framework, it must be copied to the Frameworks/ directory of an Application Bundle, which means, it is useless for plain unix console applications.
We can see the detail in this web site :http://docs.opencv.org/doc/tutorials/introduction/table_of_content_introduction/table_of_content_introduction.html#table-of-content-introduction
This folder contains toolchains and additional files that are needed for cross compilation.
For more information see introduction tutorials for target platform in documentation.

Using header files from the static library in project

I'm developing application in OSX 10.9 and Xcode 5.0.2
I'm using making workspace. It separates on two parts. Static library and bundle application.
Bundle application should link static library and includes headers for the static library which I added to the Public header group in the Build Phase of the Target. Also I've prescribed $(PRODUCT_BUILD_DIR)/usr/local/include in the header search path of the bundle app.
But problem - bundle app target doesn't see header files of static library target.
I found answer. Predefined path was set as user header search path but in code I used #import "" instead of #import <>. So right answer - use proper import syntax.

static library in xcode 4.4.1

I need to make a static library in xcode of my project , so that i can use it other project by linking it, I have through the tutorial of making static library on icodeblog.com
I have some of the following questions ??
1)- Wat does a library actually contain ? does it contain compiled version of both the .h and .m files of the project or just of the .m files ?
2)-If It contains compiled .h and .m files , then why do we need to add .h files in the project in which we are using the static library(By using the copy headers option)
3)-Even after adding .h files to that project , then why does the following error comes ?
"$OBJC_CLASS_NAME appeared in CLASS.o" not found ...
To make a static library I heartily recommend this approach
To answer your questions:
It contains object code for the contents of your .h and .m files.
The header files lets you use the code in the library. If there are no header files, your project would not know what to call.
If you are using the correct header files, this indicates to me that you are not linking against the correct library or that the library is incorrectly built. Is the guide you have been using correct? The guide I am pointing to works for me and many others.
I got the answer for problem...it is because libraries cannot be tested on Simulator
need to test on devices only ..
thats why . i was getting the error mentioned in "3)"

Documenting autogenerated code (NSManageObjects) with Doxygen

Im about to documentate a library I have that uses NSManageObjects using Doxygen. Since NSManageObjects are autogenerated from the data model, if I change something in the model, the file containing the modified managedobject is going to be generated again.
I donĀ“t want to rewrite documentation everytime a change is made in the model.
Do you know any way to accomplish this?
Thanks.
I found out how to achieve it. Doxygen allow you to document classes from other files. There is no need to do it in the .h.
Doxygen parses the .h file and he creates the documentation skeleton for that class. Then you can add documentation from any other custom file.
The problem is that you have to manually specify some labels in order to link the information from the .doc file with the class declared in the .h file.
Please see:
Doxygen documentation at other places
and this: Properties not being documented
Should be enough to get an idea of how I did managed to do it.

How to organize C source file previously compiled by GCC Make and build them into an Xcode bundle? I have a Duplicate Symbol _main Error

How to build a .bundle from source code?
This might sound like a simple problem but it has been hurdling me for a week...
Here is my problem:
I have a bunch of .c and .h files that are organized in a folder and its sub folders. The source code was written and compiled with gcc make and tested by many other make tools. The source code has some utilities and command line tools and it has more code that serve as library for those utilities and tools. It is the files that serve as libraries that I want to reuse. (By library I don't mean static library or something, I just mean that some .c and .h files in certain subfolders provide functions that can be called by some other .c files. I want to be able to call those functions, too)
Yet my problem is more complex than that: I need to build those .c and .h into a bundle to reuse it. I am not writing my application in C; I am developing in Unity and Unity can only take in .bundle files on Mac OS.
Here is my goal:
Organize the source code folder in a proper way so that I can build them into a bundle in Xcode 4.
Here is where I got stuck:
When building the project I got the following error:
Duplicate symbol _main in
/Users/zeningqu/Library/Developer/Xcode/DerivedData/ccn-cfygrtkrshubpofnfxalwimtyniq/Build/Intermediates/ccn.build/Debug/ccn.build/Objects-normal/i386/ccndsmoketest.o
and
/Users/zeningqu/Library/Developer/Xcode/DerivedData/ccn-cfygrtkrshubpofnfxalwimtyniq/Build/Intermediates/ccn.build/Debug/ccn.build/Objects-normal/i386/ccnd_main.o
for architecture i386
I can relate to this error because I can find lots of main entries in the source code. Most of them are test utilities.
Here is what I tried:
I tried removing all those utility .c files but with no luck. The error is still there. I delete and delete until some files cannot find the definition of the function they are calling. So I had to stop there.
Though I wasn't able to build a bundle I was able to build a C/C++ static library (with an .a extension). After I got the .a file I tried to put it into another Xcode project and tried to build it into a bundle. I could build a bundle in that way, but then I had problem accessing the content of the bundle. How do I call functions defined in a .a static library if that library is hidden in a bundle? I read about Apple's documentation which says:
Note: Some Xcode targets (such as shell tools and static libraries) do
not result in the creation of a bundle or package. This is normal and
there is no need to create bundles specifically for these target
types. The resulting binaries generated for those targets are intended
to be used as is.
(quoted from: https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFBundles/AboutBundles/AboutBundles.html#//apple_ref/doc/uid/10000123i-CH100-SW1)
Here is what I thought about:
I thought about replacing all main with something like main_sth. But the source code was not written by me so I didn't want to modify it. (It just doesn't feel like a proper way of doing things to me...)
I learnt that Xcode has gcc compiler built in. So I guess if gcc can make it, so can Xcode? It's just a wild guess - I am not familiar with Xcode and gcc.
Here is a summary of my questions:
Is there a way to properly organize a pile of code previously compiled and made by gcc make so that they can be built into an Xcode bundle?
Is it meaningful to put a .a library in an Xcode project and build it into a bundle? If it is meaningful, how do I call functions defined in .a after it is built into a bundle?
Is it proper to just replace all main() entries with something else?
Alright I think I have figured out at least one solution to the problem.
The duplicate main error was caused by a bunch of main entries in my source code. When the code was compiled by gcc make, I guess the author defined a sort of compilation order so that duplicate mains won't be an issue. (If you know how to do this, please let me know. I barely know make tools.) But when I just add the entire source code folder into my Xcode project, of course Xcode would complain during linking...
As I was unwilling to modify the source code (because the source code library is not developed by me), I decided to use another strategy to walk around this problem.
If your duplicate main error was reported from your own code, you can stop reading here. But if you are like me, with a bunch of gcc compiled source code and badly need a bundle yet don't know what to do, I may be able to help.
Okay here is what I did:
I set up an empty workspace.
I built a C/C++ static library project.
Import my entire source code folder into the static library project.
Set some header search path for the static library project.
Build the static library project. (Now I have a .a library which I could link against)
I set up another project, with a bundle target.
At the bundle project -> Build Phases -> Link Binary with Libraries, add the .a library that I just built.
At the bundle project -> edit scheme -> Build, add the static library project to the scheme and move it up the list so that it is built prior to my bundle project.
Then add .h files of my library project to my bundle project as references.
After that, add a .c file in my bundle project that basically functions as a wrapper. I picked a function that I want to call in Unity, wrote a wrapper function in the new .c file, and was able to build the bundle.
After several trial and error, I was able to import the bundle into Unity and was able to call the test function from Unity.
I was really excited about this! Though it's not completed yet I think this gives me hope and I am confident I can use the source code now! And the best thing about this solution is that I don't have to modify the library code developed by others. Whenever they update their code, I just update my .a library and that's it!
Though I have listed 11 steps I still feel that there are lots of details that I missed. So here are my references:
I followed this tutorial to build my source code into a static library: http://www.ccnx.org/?post_type=incsub_wiki&p=1315
I followed this blog to link static library against my bundle code and twist build phases and search headers: http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/
I followed this doc to import my bundle to Unity3D Pro as a plugin: http://unity3d.com/support/documentation/Manual/Plugins.html
I strongly recommend the second reference because that's what solved my problem!
Though the problem is almost solved there are still a few things that I haven't figured out:
I don't know if a wrapper function is at all necessary. I will try this out tomorrow and come back to update.
-- I am coming back to update: the wrapper function is NOT necessary. Just make sure you have all the headers in your bundle project and you will be able to use all the data structures and call functions defined in your headers.
I haven't used NSBundle class though I read a few docs about it. Previously I was thinking about using that class to access my .a library encapsulated in my bundle, but as I found the solution I wrote above, I didn't try the class out.
Lastly, if you have better solution, please don't hesitate to let me know!
I tried to follow the steps in the accepted answer, but had no luck. In the end, I realised step 10 needed to be modified slightly:
Create a dummy.c under (.bundle) project and the dummy.c can just be totally empty.
Remove the setting for the library you want to link inside Link Binary With Libraries
Instead use -Wl,-force_load,$(CONFIGURATION_BUILD_DIR)/libYourLib.a or -all_load to Other Linker Flags
PS: And also can use sub-project instead of workspace. and use Target Dependencies instead of Edit Scheme to achieve the same effect.

Resources