Visual Studio For Mac Create Static Library

EnArBgDeElEsFaFiFrHiHuItJaKnKoMsNlPlPtRuSqThTrUkZh

  • 2Creating a shared library
  • 3Creating a static library

Introduction

This tutorial illustrates different approaches for using a custom library in your application on Windows. The first part explains how to create a shared library and how to link against it in your application. The second part is about creating and using a static library.

To create an MFC static library, select Win32 Static Library and specify the directory. Then, in the Win32 Static Library - mark the MFC Support check box. Visual Studio 2013 (professional) crashes on my laptop even you just open up the IDE Clean Uninstall of Visual Studio 2010 and 2012 and Install Free Visual Studio 2013 Community Version! Microsoft Visual Studio is a powerful development tool that is widely used.

The header files and static and shared versions of library are also provided; these can only be used by Visual Studio 14 2015 or later (2017 or 2019) (in either release or debug mode). However, if you plan to use the library, it may be advisable to build it with the compiler you are using for your own code using Installation with cmake. I have personally followed the procedure in Walkthrough: Creating and Using a Static Library (C) with a proper CUDA build customization and generation of relocatable code, but neither.obj nor.lib files have been generated. Is there a possibility to use the Visual Studio 2010 IDE to create a CUDA static library? Thank you very much in advance.

To organize a bigger project with libraries and executables, take a look at SUBDIRS - handling dependencies

Creating a shared library

When creating a shared library that you would like to link against, then you need to ensure that the symbols that are going to be used outside the library are properly exported when the library is created. Subsequently imported when you are linking against the library. This can be done using Q_DECL_EXPORT and Q_DECL_IMPORT as shown in the following example:

test.h

test.cpp

test.pro

On Windows, MinGW will output .a and .dll, MSVC will output .lib and .dll.

On Linux, gcc/clang will output .so, .so.1, .so.1.0 and .so.1.0.0 - .lib, .a and .so are import libraries. They help link your code to the library and is needed when you build your file(.a files not all the time).

See also the documentation on Creating Shared Libraries.

Linking your application against the shared library

In order to use the shared library in your application, then you can include the headers of your library in your code and use the methods. Compile with linking to the .lib file. At runtime this loads the dll which has the implementation.

To set this up, then in your application's .pro file you need to inform the application where to find the headers and the library. The INCLUDEPATH needs to point to the directory where the headers are installed, and the LIBS variable needs to point to the directory of the .lib file. In addition you need to ensure that the .dll is found by either putting it in the application's directory or in the global PATH.

For example:

loadTestLib.pro

main.cpp

alternatively you can right-click your project in Qt Creator and select 'Add Library...', choose 'External library' and browse for your library file:

  • For libraries compiled with MSCV compiler in windows, you look for .lib or .dll
  • On Windows, MinGW compiled linking libraries are in .a, but you will need to add it manually (as of Qt Creator 2.7). You could also try simply linking the .dll directly cause it would probably work. Don't try this with a MSVC compiled library .
  • On Linux you look for the .so file

This will append the following code to your *.pro file:

$$PWD is used here to specify the full path leading to the directory containing your .pro file.

Visual Studio For Mac

Note that for Unix/Linux systems the library file name is case sensitive, but for Windows you have to leave in all lower case.


Using QLibrary to load the shared library

QLibrary can be used for loading shared libraries at runtime. In this case you only need access to the .dll, access to the headers and .lib file(s) is not necessary.

The following example shows how to set up a library for usage with QLibrary. For the function names to be resolvable, they must be exported as C functions (i.e., without name mangling) from the library. This means that the functions must be wrapped in an extern 'C' block if the library is compiled with a C++ compiler.

Since we are doing this on Windows we also must explicitly export the function from the DLL using Q_DECL_EXPORT and Q_DECL_IMPORT.

qlibraryLibrary.pro

widget.h

widget.cpp

Loading the library using QLibrary

To load the library using QLibrary, you can simply pass in the .dll to the QLibrary constructor. Make sure the .dll is available in the application directory or in the global PATH. To use functions from the library in your application, you need to resolve them using QLibrary::resolve().

The example below loads the library created above and uses one of its functions to create and show a widget.

Creating a static library

When creating a static library you need to specify the staticlib option to CONFIG in the .pro file. In contrast to the shared library example, you don't need to set up anything special for exporting and importing symbols in your .h file, since the library will be built into the application, for example:

test.pro

Using the static library in your application

Similar to what we did for the shared library loading, you need to set up the INCLUDEPATH to point to the directory where the headers are installed and the LIBS variable to point to the .lib file, for example:

useStaticLib.pro

main.cpp

Installing a library

When you build your libraries, it could be useful to have one build for one framework and to centralize them : for example, you could having one library for Android, one for Windows and QT5.4 and one for Windows with Qt5.5 without having specific configurations.The easiest way is to put your files in the Qt folders by adding in your *.pro file :

You need to specify what files you want to copy with $$OUT_PWD and where you want to put them by using $$QT_INSTALL_HEADERS and $$QT_INSTALL_LIBS.

For more information, see Installing Files.

Which approach to choose

Which approach to choose depends on your needs. When creating a shared library, you need to deploy it with your application. On the plus side, applications and libraries linked against a shared library are small. Whether to use QLibrary to load the .dll or just standard linking, depends on whether you have access to the headers and the .lib files, if you don't have access to those, then QLibrary is an alternative.

Static linking results in a stand-alone executable. The advantage is that you will only have a few files to deploy. The disadvantage is that the executables are large. See the Deployment documentation for more details on shared and static builds.

Retrieved from 'https://wiki.qt.io/index.php?title=How_to_create_a_library_with_Qt_and_use_it_in_an_application&oldid=37752'

Make sure you have first followed theinstructions to download Skia.

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.

Visual Studio For Mac Create Static Library C++

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”.

Mac

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.