tnbeatty
tnbeatty

Reputation: 315

Linking Mac Frameworks using Premake and GNU Make

I have a "cross platform" application that uses two code repositories at the moment, maintained relatively independently, and built with VS / Xcode depending on the target platform (win or mac respectively). I fell in love with Premake after using it on a few previous projects and am trying to pull all of my code for this application together into a single cross-compilable codebase.

I don't want to rely on Xcode, and instead want any developer to be able to build on Mac using either Xcode or gmake. I have a non-standard framework that I want to link to and include in the repository (it won't be located in /Library/Frameworks or any of the default mac framework search paths). I've added the framework file in a directory in my project /lib/TheFramework.framework. My premake file contains the following under the project definition:

    includedirs {".", "lib", "lib/TheFramework.framework/Headers"}
    libdirs {"lib"}
    links {"TheFramework.framework"}

When I compile, (running $ premake5 gmake and then $ make), I get a header file not found error. Is there something wrong with my search paths? Am I missing a path or a flag somewhere?

Thanks!

Upvotes: 1

Views: 2013

Answers (1)

Johannes
Johannes

Reputation: 6419

Before looking at what you need to do with premake, let's first look at what needs to happen under the hood.

When compiling a mac program with a non-standard framework on gcc or clang (which is what your resulting make file does) it is necessary to do two things:

  1. Specify the name of the framework, via -framework TheFramework - This is what premake does when you provide it with links {"TheFramework.framework"
  2. Specify the location of the framework, via -F /Path/To/Framework/ - This is currently not being handled automatically by premake.

I've made a simple test c program that uses the SDL2 framework and compiled it with gcc: https://gist.github.com/JohannesMP/6ff3463482ebbdc82c2e - notice how when I leave off the -F /... flag I get an error that is probably similar to what you described.


So what is happening is, although you are providing premake with the include dir, premake will not add that the proper -F flag.

One way around this is to do the following:

configuration {"macosx", "gmake"}
    buildoptions {"-F /Path/To/Framework"}
    linkoptions {"-F /Path/To/Framework"}

(See here for an example project: https://gist.github.com/JohannesMP/9a9b5263c127103f1861#file-premake5-lua-L24-L26 )

In premake5 this will blindly append the code provided to both the build step as well as the link step. It is necessary to do it both for build as well as link.

Just keep in mind that, because premake doesn't process or check the build/link options for being valid, a user will receive an error if the provided path doesn't exist on their machine. For example while you might have a framework in your user-specific directory ~/Library/Frameworks, since that folder doesn't exist by default another user might be using the global /Library/Frameworks instead, and when they try to compile your premake project with gmake they will get a warning:

ld: warning: directory not found for option '-F/Users/<NAME>/Library/Frameworks'

At this point, it seems that there is no 'safe' way to get premake5 to try to include the framework path, but that may change in the future.

Check out this issue I posted on the premake repo: https://github.com/premake/premake-core/issues/196

Upvotes: 2

Related Questions