Friday, 27 August 2010

Using a dylib file in XCode

main.dylib is the exported library. shownow is the exported method in main.dylib.

int main (int argc, const char * argv[]) {
char* lib_name = "./main.dylib";
void* lib_handle = dlopen(lib_name, RTLD_NOW);
if(lib_handle)
{
void (*shownow)(void) = dlsym(lib_handle, "shownow");
if(shownow)
{
shownow();
}
}
return 0;
}

Creating a dylib file in XCode

!--main.c file

#define EXPORT __attribute__((visibility("default")))



__attribute__((constructor))
static void initializer(void)
{
printf("%s",__FILE__);
}
__attribute__((destructor))
static void finalizer(void)
{
printf("%s",__FILE__);
}

EXPORT

void shownow (void) {
// insert code here...
printf("Hello, World!\n");
}

Use EXPORT before any funtion that you would like to export in dylib file. After you have written the file. You can create a dylib in terminal by typing the command below.
Locate the main.c file and type


gcc -dynamiclib -std=gnu99 "FileName" -current_version 1.0 -compatibility_version 1.0 -fvisibility=hidden -o "LibFileName"

Example:
"FileName" - main.c
"LibFileName"- main.dylib

C++ code to make your application a trial version

{
time_t rawtime, end;
struct tm * endinfo;
double dif;
time ( &rawtime );
endinfo = gmtime ( &rawtime );
endinfo->tm_year = year-1900;
endinfo->tm_mon = Month-1;
endinfo->tm_mday = Day;
end = mktime ( endinfo );
dif = difftime(end, rawtime);
if (dif less than 0)
printf("\nTrial Version Expired \n");
}
Year, Month and Day are all integers. Enter your Date on which you want the application to be expired.

OpenCV setup in MAC OSX

Install macports - > http://www.macports.org/install.php

Open terminal type sudo port -v selfupdate

type sudo port install opencv

Once the installation is successful. Include OpenCV in XCode by following the steps below.

These instructions were written for Xcode 3.1.x
Create a new XCode project using the Command Line Utility/Standard Tool template
Select Project -> Edit Project Settings
Select the Build tab
Set Configuration to All Configurations
In the Architectures section, double-click Valid Architectures and remove all the PPC architectures
In the Search Paths section set Header Search Paths to /usr/local/include/opencv
Close the Project Info window
Select Project -> New Group and create a group called OpenCV Frameworks
With the new group selected, select Project -> Add to Project…
Press the "/" key to get the Go to the folder prompt
Enter /usr/local/lib
Select libcxcore.dylib, libcvaux.dylib, libcv.dylib, libhighgui.dylib, and libml.dylib.
Click Add
Uncheck Copy Items… and click Add
Now you should be able to include the OpenCV libraries, compile, and run your project