Visual Studio For Mac Create Static Library
Visual Studio Solutions. If you use Visual Studio, you may want to pass -ide=vs to bin/gn gen to generate all.sln. That solution will exist within the GN directory for the specific configuration, and will only build/run that configuration. If you want a Visual Studio Solution that supports multiple GN configurations, there is a helper script. Windows users must add the Win64 suffix to their Visual Studio generator name if they want to build the library for 64-bit platforms, they'll omit it for 32-bit builds and define the TARGETCPU variable accordingly.
- Visual Studio C++ For Mac
- Visual Studio For Mac Create Static Library Linux
- Visual Studio For Mac Review
- Visual Studio For Mac Community
Make sure you have first followed theinstructions to download Skia.
I am trying to build a project consisting on a c console application and a c static library. I just create a default project for both. Visual Studio creates a default example function.
Skia uses GN toconfigure its builds.
is_official_build
and Third-party Dependencies
Most users of Skia should set is_official_build=true
, and most developersshould leave it to its false
default.
This mode configures Skia in a way that’s suitable to ship: an optimized buildwith no debug symbols, dynamically linked against its third-party dependenciesusing the ordinary library search path.
In contrast, the developer-oriented default is an unoptimized build with fulldebug symbols and all third-party dependencies built from source and embeddedinto libskia. This is how we do all our manual and automated testing.
Skia offers several features that make use of third-party libraries, likelibpng, libwebp, or libjpeg-turbo to decode images, or ICU and sftnly to subsetfonts. All these third-party dependencies are optional and can be controlled bya GN argument that looks something like skia_use_foo
for appropriate foo
.
If skia_use_foo
is enabled, enabling skia_use_system_foo
will build and linkSkia against the headers and libraries found on the system paths.is_official_build=true
enables all skia_use_system_foo
by default. You canuse extra_cflags
and extra_ldflags
to add include or library paths ifneeded.
Supported and Preferred Compilers
While Skia should compile with GCC, MSVC, and other compilers, a number ofroutines in Skia’s software backend have been written to run fastest whencompiled with Clang. If you depend on software rasterization, image decoding, orcolor space conversion and compile Skia with a compiler other than Clang, youwill see dramatically worse performance. This choice was only a matter ofprioritization; there is nothing fundamentally wrong with non-Clang compilers.So if this is a serious issue for you, please let us know on the mailing list.
Skia makes use of C++17 language features (compiles with -std=c++17
flag) andthus requires a C++17 compatible compiler. Clang 5 and later implement all ofthe features of the c++17 standard. Older compilers that lack C++17 support mayproduce non-obvious compilation errors. You can configure your build to usespecific executables for cc
and cxx
invocations using e.g.--args='cc='clang-6.0' cxx='clang++6.0'
GN build arguments, as illustrated inQuickstart. This can be useful for building Skia without needing tomodify your machine’s default compiler toolchain.
Quickstart
Run gn gen
to generate your build files. As arguments to gn gen
, pass a namefor your build directory, and optionally --args=
to configure the build type.
To build Skia as a static library in a build directory named out/Static
:
To build Skia as a shared library (DLL) in a build directory named out/Shared
:
If you find that you don’t have bin/gn
, make sure you’ve run:
For a list of available build arguments, take a look at gn/skia.gni
, or run:
GN allows multiple build folders to coexist; each build can be configuredseparately as desired. For example:
Once you have generated your build files, run Ninja to compile and link Skia:
If some header files are missing, install the corresponding dependencies:
To pull new changes and rebuild:
Android
To build Skia for Android you need anAndroid NDK.
If you do not have an NDK and have access to CIPD, you can use one of thesecommands to fetch the NDK our bots use:
When generating your GN build files, pass the path to your ndk
and yourdesired target_cpu
:
Other arguments like is_debug
and is_component_build
continue to work.Tweaking ndk_api
gives you access to newer Android features like Vulkan.
To test on an Android device, push the binary and resources
over, and run itas normal. You may find bin/droid
convenient.
ChromeOS
To cross-compile Skia for arm ChromeOS devices the following is needed:
- Clang 4 or newer
- An armhf sysroot
- The (E)GL lib files on the arm chromebook to link against.
To compile Skia for an x86 ChromeOS device, one only needs Clang and the libfiles.
If you have access to CIPD, you can fetch all of these as follows:
If you don’t have authorization to use those assets, then see the README.mdfiles forarmhf_sysroot,chromebook_arm_gles,andchromebook_x86_64_glesfor instructions on creating those assets.
Once those files are in place, generate the GN args that resemble the following:
Compile dm (or another executable of your choice) with ninja, as per usual.
Push the binary to a chromebook via ssh andrun dm as normal using the gles GPU config.
Most chromebooks by default have their home directory partition marked asnoexec. To avoid “permission denied” errors, remember to run something like:
Mac
Mac users may want to pass --ide=xcode
to bin/gn gen
to generate an Xcodeproject.
iOS
Run GN to generate your build files. Set target_os='ios'
to build for iOS.This defaults to target_cpu='arm64'
. Choosing x64
targets the iOS simulator.
This will also package (and for devices, sign) iOS test binaries. This defaultsto a Google signing identity and provisioning profile. To use a different oneset the GN args skia_ios_identity
to match your code signing identity andskia_ios_profile
to the name of your provisioning profile, e.g.
A list of identities can be found by typing security find-identity
on thecommand line. The name of the provisioning profile should be available on theApple Developer site. Alternatively, skia_ios_profile
can be the absolute pathto the mobileprovision file.
If you find yourself missing a Google signing identity or provisioning profile,you’ll want to have a read through go/appledev.
For signed packages ios-deploy
makes installing and running them on a deviceeasy:
Alternatively you can generate an Xcode project by passing --ide=xcode
tobin/gn gen
. If you are using Xcode version 10 or later, you may need to go toProject Settings...
and verify that Build System:
is set toLegacy Build System
.
Deploying to a device with an OS older than the current SDK can be done bysetting the ios_min_target
arg:
where <major>.<minor>
is the iOS version on the device, e.g., 12.0 or 11.4.
Windows
Skia can build on Windows with Visual Studio 2017 or 2019. If GN is unable tolocate either of those, it will print an error message. In that case, you canpass your VC
path to GN via win_vc
.
Skia can be compiled with the freeBuild Tools for Visual Studio 2017 or 2019.
The bots use a packaged 2019 toolchain, which Googlers can download like this:
You can then pass the VC and SDK paths to GN by setting your GN args:
This toolchain is the only way we support 32-bit builds, by also settingtarget_cpu='x86'
.
The Skia build assumes that the PATHEXT environment variable contains “.EXE”.
Highly Recommended: Build with clang-cl
Skia uses generated code that is only optimized when Skia is built with clang.Other compilers get generic unoptimized code.
Setting the cc
and cxx
gn args is not sufficient to build with clang-cl.These variables are ignored on Windows. Instead set the variable clang_win
toyour LLVM installation directory. If you installed the prebuilt LLVM downloadedfrom here in thedefault location that would be:
Follow the standard Windows path specification and not MinGW convention (e.g.C:Program FilesLLVM
not )./c/Program Files/LLVM
Visual Studio Solutions
If you use Visual Studio, you may want to pass --ide=vs
to bin/gn gen
togenerate all.sln
. That solution will exist within the GN directory for thespecific configuration, and will only build/run that configuration.
If you want a Visual Studio Solution that supports multiple GN configurations,there is a helper script. It requires that all of your GN directories be insidethe out
directory. First, create all of your GN configurations as usual. Pass--ide=vs
when running bin/gn gen
for each one. Then:
This creates a new dedicated output directory and solution fileout/sln/skia.sln
. It has one solution configuration for each GN configuration,and supports building and running any of them. It also adjusts syntaxhighlighting of inactive code blocks based on preprocessor definitions from theselected solution configuration.
Windows ARM64
There is early, experimental support forWindows 10 on ARM. Thiscurrently requires (a recent version of) MSVC, and theVisual C++ compilers and libraries for ARM64
individual component in theVisual Studio Installer. For Googlers, the win_toolchain asset includes theARM64 compiler.
To use that toolchain, set the target_cpu
GN argument to 'arm64'
. Note thatOpenGL is not supported by Windows 10 on ARM, so Skia’s GL backends are stubbedout, and will not work. ANGLE is supported:
This will produce a build of Skia that can use the software or ANGLE backends,in DM. Viewer only works when launched with --backend angle
, because thesoftware backend tries to use OpenGL to display the window contents.
CMake
We have added a GN-to-CMake translator mainly for use with IDEs that like CMakeproject descriptions. This is not meant for any purpose beyond development.
-->Tip
Portable Class Libraries (PCLs) are considered deprecated in the latest versions of Visual Studio.While you can still open, edit, and compile PCLs, for new projects it is recommended to use.NET Standard librariesto access a larger API surface area.
A key component of building cross-platform applications is being able to share code across variousplatform-specific projects. However, this is complicated by the fact that different platforms often use adifferent sub-set of the .NET Base Class Library (BCL), and therefore are actually built to a different .NETCore Library Profile. This means that each platform can only use class libraries that are targeted to thesame profile so they would appear to require separate class library projects for each platform.
There are three major approaches to code sharing that address this problem: .NET Standard projects, Shared Asset Projects, and Portable Class Library (PCL) projects.
- .NET Standard projects are the preferred approach for sharing .NET code, read more about .NET Standard projects and Xamarin.
- Shared Asset Projects use a single set of files and offers a quick and simple way in which to share code within a solution and generally employs conditional compilation directives to specify code paths for various platforms that will use it (for more information see the Shared Projects article).
- PCL projects target specific profiles that support a known set of BCL classes/features. However, the down side to PCL is that they often require extra architectural effort to separate profile specific code into their own libraries.
This page explains how to create a PCL project that targets a specific profile,which can then be referenced by multiple platform-specific projects.
What is a Portable Class Library?
When you create an Application Project or a Library Project, the resulting DLL is restricted toworking on the specific platform it is created for. This prevents you from writing an assembly for a Windowsapp, and then re-using it on Xamarin.iOS and Xamarin.Android.
When you create a Portable Class Library, however, you can choose a combination of platforms that you wantyour code to run on. The compatibility choices you make when creating a Portable Class Library are translatedinto a “Profile” identifier, which describes which platforms the library supports.
The table below shows some of the features that vary by .NET platform. To write a PCL assembly that isguaranteed to run on specific devices/platforms you simply choose which support is required when you createthe project.
Feature | .NET Framework | UWP apps | Silverlight | Windows Phone | Xamarin |
---|---|---|---|---|---|
Core | Y | Y | Y | Y | Y |
LINQ | Y | Y | Y | Y | Y |
IQueryable | Y | Y | Y | 7.5 + | Y |
Serialization | Y | Y | Y | Y | Y |
Data Annotations | 4.0.3 + | Y | Y | Y |
The Xamarin column reflects the fact that Xamarin.iOS and Xamarin.Android supports all the profiles shipped with Visual Studio, and the availability of features in any libraries you create will only be limited by the other platforms you choose to support.
This includes profiles that are combinations of:
- .NET 4 or .NET 4.5
- Silverlight 5
- Windows Phone 8
- UWP apps
You can read more about the different profiles' capabilities onMicrosoft’s websiteand see another community member's PCL profile summarywhich includes supported framework info and other notes.
Benefits
- Centralized code sharing – write and test code in a single project that can be consumed by other libraries or applications.
- Refactoring operations will affect all code loaded in the solution (the Portable Class Library and the platform-specific projects).
- The PCL project can be easily referenced by other projects in a solution, or the output assembly can be shared for others to reference in their solutions.
Disadvantages
- Because the same Portable Class Library is shared between multiple applications, platform-specific libraries cannot be referenced (eg. Community.CsharpSqlite.WP7).
- The Portable Class Library subset may not include classes that would otherwise be available in both MonoTouch and Mono for Android (such as DllImport or System.IO.File).
Note
Portable Class Libraries have been deprecated in the latest version of Visual Studio, and.NET Standard Libraries are recommended instead.
To some extent both disadvantages can be circumvented using the Provider pattern or Dependency Injection to code the actual implementation in the platform projects against an interface or base class that is defined in the Portable Class Library.
This diagram shows the architecture of a cross-platform application using a Portable Class Library to share code, but also using Dependency Injection to pass in platform-dependent features:
Visual Studio for Mac walkthrough
This section walks through how to create and use a Portable Class Library using Visual Studio for Mac. Refer the to PCL Example section for a complete implementation.
Creating a PCL
Adding a Portable Class Library to your solution is very similar to adding a regular Library project.
Visual Studio C++ For Mac
In the New Project dialog select the Multiplatform > Library > Portable Library option:
When a PCL is created in Visual Studio for Mac it is automatically configured with a Profile that works for Xamarin.iOS and Xamarin.Android. The PCL project will appear as shown in this screenshot:
Visual Studio For Mac Create Static Library Linux
The PCL is now ready for code to be added. It can also be referenced by other projects (Application projects, Library projects and even other PCL projects).
Editing PCL settings
To view and change the PCL settings for this project, right-click the project and choose Options > Build > General to see the screen shown here:
Click Change... to alter the target profile for this portable class library.
If the profile is changed after code has already been added to the PCL, it's possible that the library will no longer compile if the code references features that are not part of the newly-selected profile.
Working with a PCL
When code is written in a PCL library, the Visual Studio for Mac editor will recognize the limitations of the selected profile and adjust auto-complete options accordingly. For example, this screenshot shows the auto-complete options for System.IO using the default profile (Profile136) used in Visual Studio for Mac – notice the scrollbar which indicates about half of the available classes are displayed (in fact there are only 14 classes available).
Compare that with the System.IO auto-complete in a Xamarin.iOS or Xamarin.Android project – there are 40 classes available including commonly used classes like File
and Directory
which are not in any PCL profile.
This reflects the underlying trade-off of using PCL – the ability to share code seamlessly across many platforms means certain APIs are not available to you because they don’t have comparable implementations across all possible platforms.
Using PCL
Once a PCL project has been created, you can add a reference to it from any compatible Application or Library project in the same way you normally add references. In Visual Studio for Mac, right-click on the References node and choose Edit References... then switch to the Projects tab as shown:
The following screenshot shows the Solution pad for the TaskyPortable sample app, showing the PCL library at the bottom and a reference to that PCL library in the Xamarin.iOS project.
The output from a PCL (ie. the resulting assembly DLL) can also be added as a reference to most projects. This makes PCL an ideal way to ship cross-platform components and libraries.
Visual Studio walkthrough
This section walks through how to create and use a Portable Class Library using Visual Studio. Refer the toPCL Example section for a complete implementation.
Creating a PCL
Adding a PCL to your solution in Visual Studio is slightly different to adding a regular project:
In the Add New Project screen, select the Class Library (Legacy Portable) option. Note the description on the right advises that this project type has been deprecated.
Visual Studio will immediately prompt with the following dialog so that the profile can be configured.Tick the platforms you need to support and press OK.
The PCL project will appear as shown in the Solution Explorer – the text (Portable) appears beside the project name to indicate it is a PCL:
The PCL is now ready for code to be added. It can also be referenced by other projects (applicationprojects, library projects, and even other PCL projects).
Editing PCL settings
The PCL settings can be viewed and changed by right-clicking on the project and choosing Properties > Library , as shown in this screenshot:
If the profile is changed after code has already been added to the PCL, it’s possible that the library will no longer compile if the code references features that are not part of the newly-selected profile.
Tip
There is also a message advising that .NETStandard is the recommended method for sharing code. This is an indication that while PCLs are still supported, it is recommended to upgrade to .NET Standard.
Working with a PCL
When code is written in a PCL library, Visual Studio will recognize the limitations of the selected profile and adjust Intellisense options accordingly. For example, this screenshot shows the auto-complete options for System.IO using the default profile (Profile136) – notice the scrollbar which indicates about half of the available classes are displayed (in fact there are only 14 classes available).
Compare that with the System.IO auto-complete in a regular project – there are 40 classes available includingcommonly used classes like File
and Directory
which are not in any PCL profile.
This reflects the underlying trade-off of using PCL – the ability to share code seamlessly across many platforms means certain APIs are not available to you because they don’t have comparable implementations across all possible platforms.
Tip
.NET Standard 2.0 represents a much larger API surface area than PCLs, including the System.IO namespace. For newprojects, .NET Standard is recommended over PCL.
Using PCL
Once a PCL project has been created, you can add a reference to it from any compatible Application or Library project in the same way you normally add references. In Visual Studio, right-click on the References node and choose Add Reference...
then switch to the Solution > Projects tab as shown:
The following screenshot shows the Solution pane for the TaskyPortable sample app, showing the PCL library at the bottom and a reference to that PCL library in the Xamarin.iOS project.
The output from a PCL (ie. the resulting assembly DLL) can also be added as a reference to most projects.This makes PCL an ideal way to ship cross-platform components and libraries.
PCL example
The TaskyPortablesample application demonstrates how a Portable Class Library can be used with Xamarin.Here are some screenshots of the resulting apps running on iOS and Android:
Visual Studio For Mac Review
It shares a number of data and logic classes that are purely portable code, and it also demonstrates how toincorporate platform-specific requirements using Dependency Injection for the SQLite database implementation.
The solution structure is shown below (in Visual Studio for Mac and Visual Studio respectively):
Because the SQLite-NET code has platform-specific pieces (to work with the SQLite implementations on each different operating system) for demonstration purposes it has been refactored into an abstract class that can be compiled into a Portable Class Library, and the actual code implemented as subclasses in the iOS and Android projects.
TaskyPortableLibrary
Visual Studio For Mac Community
The Portable Class Library is limited in the .NET features that it can support. Because it is compiled to run on multiple platforms, it cannot make use of [DllImport]
functionality that is used in SQLite-NET. Instead SQLite-NET is implemented as an abstract class, and then referenced through the rest of the shared code. An extract of the abstract API is shown below:
The remainder of the shared code uses the abstract class to “store” and “retrieve” objects from the database. In any application that uses this abstract class we must pass in a complete implementation that provides the actual database functionality.
TaskyAndroid and TaskyiOS
The iOS and Android application projects contain the user-interface and other platform-specific code used to wire-up the shared code in the PCL.
These projects also contain an implementation of the abstract database API that works on that platform. OniOS and Android the Sqlite database engine is built-in to the operating system, so the implementation can use[DllImport]
as shown to provide the concrete implementation of database connectivity. An excerpt of the platform-specific implementation code is shown here:
The full implementation can be seen in the sample code.
Summary
This article has briefly discussed the benefits and pitfalls of Portable Class Libraries, demonstrated how to create and consume PCLs from inside Visual Studio for Mac and Visual Studio; and finally introduced a complete sample application – TaskyPortable – that shows a PCL in action.