• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

BUILDH A D01-Dec-2020782 2623

MakefileH A D01-Dec-20201.4 KiB3610

README.mdH A D01-Dec-20205.8 KiB147110

WORKSPACEH A D01-Dec-20201.8 KiB5240

quickstart.ccH A D01-Dec-20201.4 KiB4323

README.md

1# HOWTO: using the Cloud Spanner C++ client in your project
2
3This directory contains small examples showing how to use the Cloud Spanner C++
4client library in your own project. These instructions assume that you have
5some experience as a C++ developer and that you have a working C++ toolchain
6(compiler, linker, etc.) installed on your platform.
7
8## Before you begin
9
10To run the quickstart examples you will need a working Google Cloud Platform
11(GCP) project, as well as a Cloud Spanner instance and database.
12The [Cloud Spanner quickstart][spanner-quickstart-link] covers the necessary
13steps in detail. Make a note of the GCP project id, the instance id, and the
14database id as you will need them below.
15
16## Configuring authentication for the C++ Client Library
17
18Like most Google Cloud Platform (GCP) services, Cloud Spanner requires that
19your application authenticates with the service before accessing any data. If
20you are not familiar with GCP authentication please take this opportunity to
21review the [Authentication Overview][authentication-quickstart]. This library
22uses the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to find the
23credentials file. For example:
24
25| Shell              | Command                                        |
26| :----------------- | ---------------------------------------------- |
27| Bash/zsh/ksh/etc.  | `export GOOGLE_APPLICATION_CREDENTIALS=[PATH]` |
28| sh                 | `GOOGLE_APPLICATION_CREDENTIALS=[PATH];`<br> `export GOOGLE_APPLICATION_CREDENTIALS` |
29| csh/tsch           | `setenv GOOGLE_APPLICATION_CREDENTIALS [PATH]` |
30| Windows Powershell | `$env:GOOGLE_APPLICATION_CREDENTIALS=[PATH]`   |
31| Windows cmd.exe    | `set GOOGLE_APPLICATION_CREDENTIALS=[PATH]`    |
32
33Setting this environment variable is the recommended way to configure the
34authentication preferences, though if the environment variable is not set, the
35library searches for a credentials file in the same location as the [Cloud
36SDK](https://cloud.google.com/sdk/). For more information about *Application
37Default Credentials*, see
38https://cloud.google.com/docs/authentication/production
39
40## Using with Bazel
41
42> :warning: If you are using Windows or macOS there are additional instructions
43> at the end of this document.
44
451. Install Bazel using [the instructions][bazel-install] from the `bazel.build`
46   website.
47
482. Compile this example using Bazel:
49
50   ```bash
51   cd $HOME/google-cloud-cpp/google/cloud/spanner/quickstart
52   bazel build ...
53   ```
54
55   Note that Bazel automatically downloads and compiles all dependencies of the
56   project. As it is often the case with C++ libraries, compiling these
57   dependencies may take several minutes.
58
593. Run the example, change the place holder to appropriate values:
60
61   ```bash
62   bazel run :quickstart -- [GCP PROJECT] [CLOUD SPANNER INSTANCE] [CLOUD SPANNER DATABASE]
63   ```
64
65## Using with CMake
66
67> :warning: If you are using Windows or macOS there are additional instructions
68> at the end of this document.
69
701. Install CMake. The package managers for most Linux distributions include a
71   package for CMake. Likewise, you can install CMake on Windows using a package
72   manager such as [chocolatey][choco-cmake-link], and on macOS using
73   [homebrew][homebrew-cmake-link]. You can also obtain the software directly
74   from the [cmake.org](https://cmake.org/download/).
75
762. Install the dependencies with your favorite tools. As an example, if you use
77   [vcpkg](https://github.com/Microsoft/vcpkg.git):
78
79   ```bash
80   cd $HOME/vcpkg
81   ./vcpkg install google-cloud-cpp
82   ```
83
84   Note that, as it is often the case with C++ libraries, compiling these
85   dependencies may take several minutes.
86
873. Configure CMake, if necessary, configure the directory where you installed
88   the dependencies:
89
90   ```bash
91   cd $HOME/gooogle-cloud-cpp/google/cloud/spanner/quickstart
92   cmake -H. -B.build -DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake
93   cmake --build .build
94   ```
95
964. Run the example, change the place holder to appropriate values:
97
98   ```bash
99   .build/quickstart [GCP PROJECT] [CLOUD SPANNER INSTANCE] [CLOUD SPANNER DATABASE]
100   ```
101
102## Platform Specific Notes
103
104### macOS
105
106gRPC [requires][grpc-roots-pem-bug] an environment variable to configure the
107trust store for SSL certificates, you can download and configure this using:
108
109```bash
110curl -Lo roots.pem https://raw.githubusercontent.com/grpc/grpc/master/etc/roots.pem
111export GRPC_DEFAULT_SSL_ROOTS_FILE_PATH="$PWD/roots.pem"
112```
113
114To workaround a [bug in Bazel][bazel-grpc-macos-bug], gRPC requires this flag on
115macOS builds, you can add the option manually or include it in your `.bazelrc`
116file:
117
118```bash
119bazel build --copt=-DGRPC_BAZEL_BUILD ...
120```
121
122### Windows
123
124To correctly configure the MSVC runtime you should change the CMake minimum
125required version to 3.15 or add `-DCMAKE_POLICY_DEFAULT_CMP0091=NEW` to the
126CMake configuration step.
127
128gRPC [requires][grpc-roots-pem-bug] an environment variable to configure the
129trust store for SSL certificates, you can download and configure this using:
130
131```console
132@powershell -NoProfile -ExecutionPolicy unrestricted -Command ^
133    (new-object System.Net.WebClient).Downloadfile( ^
134        'https://raw.githubusercontent.com/grpc/grpc/master/etc/roots.pem', ^
135        'roots.pem')
136set GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=%cd%\roots.pem
137```
138
139[bazel-install]: https://docs.bazel.build/versions/master/install.html
140[spanner-quickstart-link]: https://cloud.google.com/spanner/docs/quickstart-console
141[grpc-roots-pem-bug]: https://github.com/grpc/grpc/issues/16571
142[choco-cmake-link]: https://chocolatey.org/packages/cmake
143[homebrew-cmake-link]: https://formulae.brew.sh/formula/cmake
144[cmake-download-link]: https://cmake.org/download/
145[bazel-grpc-macos-bug]: https://github.com/bazelbuild/bazel/issues/4341
146[authentication-quickstart]: https://cloud.google.com/docs/authentication/getting-started 'Authentication Getting Started'
147