CMake link to external library

后端 未结 4 2088
梦谈多话
梦谈多话 2020-11-21 04:53

How to get CMake to link an executable to an external shared library that is not build within the same CMake project?

Just doing target_link_libraries(GLBall $

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 05:21

    One more alternative, in the case you are working with the Appstore, need "Entitlements" and as such need to link with an Apple-Framework.

    For Entitlements to work (e.g. GameCenter) you need to have a "Link Binary with Libraries"-buildstep and then link with "GameKit.framework". CMake "injects" the libraries on a "low level" into the commandline, hence Xcode doesn't really know about it, and as such you will not get GameKit enabled in the Capabilities screen.

    One way to use CMake and have a "Link with Binaries"-buildstep is to generate the xcodeproj with CMake, and then use 'sed' to 'search & replace' and add the GameKit in the way XCode likes it...

    The script looks like this (for Xcode 6.3.1).

    s#\/\* Begin PBXBuildFile section \*\/#\/\* Begin PBXBuildFile section \*\/\
        26B12AA11C10544700A9A2BA \/\* GameKit.framework in Frameworks \*\/ = {isa = PBXBuildFile; fileRef = 26B12AA01C10544700A9A2BA \/\* GameKit.framework xxx\*\/; };#g
    
    s#\/\* Begin PBXFileReference section \*\/#\/\* Begin PBXFileReference section \*\/\
        26B12AA01C10544700A9A2BA \/\* GameKit.framework xxx\*\/ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GameKit.framework; path = System\/Library\/Frameworks\/GameKit.framework; sourceTree = SDKROOT; };#g
    
    s#\/\* End PBXFileReference section \*\/#\/\* End PBXFileReference section \*\/\
    \
    \/\* Begin PBXFrameworksBuildPhase section \*\/\
        26B12A9F1C10543B00A9A2BA \/\* Frameworks \*\/ = {\
            isa = PBXFrameworksBuildPhase;\
            buildActionMask = 2147483647;\
            files = (\
                26B12AA11C10544700A9A2BA \/\* GameKit.framework in Frameworks xxx\*\/,\
            );\
            runOnlyForDeploymentPostprocessing = 0;\
        };\
    \/\* End PBXFrameworksBuildPhase section \*\/\
    #g
    
    s#\/\* CMake PostBuild Rules \*\/,#\/\* CMake PostBuild Rules \*\/,\
                26B12A9F1C10543B00A9A2BA \/\* Frameworks xxx\*\/,#g
    s#\/\* Products \*\/,#\/\* Products \*\/,\
                26B12AA01C10544700A9A2BA \/\* GameKit.framework xxx\*\/,#g
    

    save this to "gamecenter.sed" and then "apply" it like this ( it changes your xcodeproj! )

    sed -i.pbxprojbak -f gamecenter.sed myproject.xcodeproj/project.pbxproj
    

    You might have to change the script-commands to fit your need.

    Warning: it's likely to break with different Xcode-version as the project-format could change, the (hardcoded) unique number might not really by unique - and generally the solutions by other people are better - so unless you need to Support the Appstore + Entitlements (and automated builds), don't do this.

    This is a CMake bug, see http://cmake.org/Bug/view.php?id=14185 and http://gitlab.kitware.com/cmake/cmake/issues/14185

提交回复
热议问题