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

How to link with a framework on macOS ? #393

Open
hakonhagland opened this issue Apr 21, 2021 · 3 comments
Open

How to link with a framework on macOS ? #393

hakonhagland opened this issue Apr 21, 2021 · 3 comments

Comments

@hakonhagland
Copy link
Contributor

See this question on stackoverflow. In order to compile the following C program on macOS :

#include <ApplicationServices/ApplicationServices.h>
#include <unistd.h>

void mmove(int x, int y);

int main() {
    mmove(100,100);
}

void mmove(int x, int y) {
    CGEventRef move = CGEventCreateMouseEvent( NULL, kCGEventMouseMoved, CGPointMake(x, y), kCGMouseButtonLeft );
    CGEventPost(kCGHIDEventTap, move);
    CFRelease(move);
}

the following command line can be used:

cc -o foo foo.c -framework ApplicationServices

But passing LIBS => " -framework ApplicationServices" to WriteMakefile() does not work. For example:

$ perl -MData::Dumper -MExtUtils::Liblist -E ' print Dumper(ExtUtils::Liblist->ext("-framework ApplicationServices"))'
$VAR1 = '';
$VAR2 = '';
$VAR3 = '';
$VAR4 = '';

However, passing LDDLFLAGS => "$Config{lddlflags} -framework ApplicationServices" does work, but shouldn't it be possible to use LIBS here ?

@mohawk2
Copy link
Member

mohawk2 commented Apr 21, 2021

It sounds like Liblist would need to gain knowledge about -framework. To do so would be platform-specific, which could either be crowbarred in as-is, or be done after a somewhat-needed restructuring of Liblist to fit with (and probably be incorporated into) the MM_* modules.

@Leont
Copy link
Member

Leont commented Apr 21, 2021

It sounds like Liblist would need to gain knowledge about -framework.

Yeah. Liblist keeps being a PITA.

@kiwiroy
Copy link

kiwiroy commented Jun 15, 2021

Workaround:

Add a library (e.g. -lm) that can be found and increment $found so @extralibs are returned.

LIBS => "-lm -framework ApplicationServices"

unless ( $found ) {
return ( '', '', '', '', ( $give_libs ? \@libs : () ) );
}
else {
return ( "@extralibs", "@bsloadlibs", "@ldloadlibs", join( ":", @ld_run_path ), ( $give_libs ? \@libs : () ) );
}

Gaining knowledge:

It might be that the standard paths get searched - from Including Frameworks

/System/Library/Frameworks directory and the /Library/Frameworks directory

and possibly $DYLD_FRAMEWORK_PATH.

diff --git a/lib/ExtUtils/Liblist/Kid.pm b/lib/ExtUtils/Liblist/Kid.pm
index 5eb8a8a0..3fd56e4b 100644
--- a/lib/ExtUtils/Liblist/Kid.pm
+++ b/lib/ExtUtils/Liblist/Kid.pm
@@ -97,6 +97,12 @@ sub _unix_os2_ext {
         }
 
         if ( $thislib =~ m!^-Wl,! ) {
+            if (@extralibs && $extralibs[$#extralibs] eq '-Wl,-framework') {
+              (my $framework = $thislib) =~ s/^-Wl,//;
+              for (qw(/System/Library/Frameworks /Library/Frameworks), split(/:/, $ENV{DYLD_FRAMEWORK_PATH})) {
+                $found++ if (-d File::Spec->catdir($_, "$framework.framework"));
+              }
+            }
             push( @extralibs,  $thislib );
             push( @ldloadlibs, $thislib );
             next;

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

4 participants