Skip to content

Commit a3f3fd9

Browse files
authored
DOCSP-48728: CMake project instructions (#104)
* DOCSP-48728: CMake project instructions * edits * AS feedback * edit * edit * tech review
1 parent f1075b8 commit a3f3fd9

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

source/get-started.txt

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,7 @@ your operating system:
204204
vcpkg install mongo-c-driver
205205

206206
After you install the driver, the standard CMake configuration file
207-
is available, as well as the generated ``IMPORTED`` targets. You can
208-
create a ``CMakeLists.txt`` file as shown in the following example:
209-
210-
.. code-block:: cmake
211-
212-
find_package(mongoc CONFIG REQUIRED)
213-
target_link_libraries(my-application PRIVATE mongoc::mongoc)
214-
215-
.. note::
216-
217-
In the preceding example, the ``mongoc::mongoc`` target
218-
points to either the static library or the shared library.
219-
The library type depends on which one is available and
220-
whichever type the user specifies in the ``MONGOC_DEFAULT_IMPORTED_LIBRARY_TYPE``
221-
configuration setting. If you don't set this value and
222-
both library types are available, ``mongoc::mongoc`` uses
223-
the static library.
207+
and the generated ``IMPORTED`` targets are available.
224208

225209
To configure the CMake project with vcpkg integration, set the CMake
226210
toolchain file in the configuration command by running the following
@@ -257,13 +241,13 @@ your MongoDB database in the cloud.
257241
.. procedure::
258242
:style: connected
259243

260-
.. step:: Create a Free MongoDB deployment on Atlas
244+
.. step:: Create a free MongoDB deployment on Atlas
261245

262246
Complete the :atlas:`Get Started with Atlas </getting-started>`
263247
guide to set up a new Atlas account and load sample data into a new free
264248
tier MongoDB deployment.
265249

266-
.. step:: Save your Credentials
250+
.. step:: Save your credentials
267251

268252
After you create your database user, save that user's
269253
username and password to a safe location for use in an upcoming step.
@@ -293,7 +277,7 @@ To connect to an instance or deployment not hosted on Atlas, see the
293277
.. procedure::
294278
:style: connected
295279

296-
.. step:: Find Your MongoDB Atlas Connection String
280+
.. step:: Find your MongoDB Atlas connection string
297281

298282
To retrieve your connection string for the deployment that
299283
you created in the :ref:`previous step <c-get-started-create-deployment>`,
@@ -314,15 +298,15 @@ To connect to an instance or deployment not hosted on Atlas, see the
314298
Deselect the :guilabel:`Include full driver code example` option to view
315299
only the connection string.
316300

317-
.. step:: Copy Your Connection String
301+
.. step:: Copy your connection string
318302

319303
Click the button on the right of the connection string to copy it
320304
to your clipboard, as shown in the following screenshot:
321305

322306
.. figure:: /includes/figures/atlas_connection_copy_c.png
323307
:alt: The copy button next to the connection string in the Atlas UI
324308

325-
.. step:: Update the Placeholders
309+
.. step:: Update the placeholders
326310

327311
Paste this connection string into a file in your preferred text editor and
328312
replace the ``<db_username>`` and ``<db_password>`` placeholders with your
@@ -348,7 +332,7 @@ To learn more about the ``sample_mflix`` database, see :atlas:`Sample Mflix Data
348332
.. procedure::
349333
:style: connected
350334

351-
.. step:: Create a Project Directory
335+
.. step:: Create a project directory
352336

353337
In your shell, navigate to where you want to create your
354338
application, then run the following command to create a
@@ -380,28 +364,60 @@ To learn more about the ``sample_mflix`` database, see :atlas:`Sample Mflix Data
380364
cd c-quickstart
381365
type nul > quickstart.c
382366

383-
.. step:: Create Your C Driver Application
367+
.. step:: Create your C Driver application
384368

385369
Copy and paste the following code into the ``quickstart.c`` file, which queries
386370
the ``movies`` collection in the ``sample_mflix`` database:
387371

388372
.. literalinclude:: /includes/get-started/quickstart.c
389373
:language: c
390374

391-
.. step:: Assign the Connection String
375+
.. step:: Assign the connection string
392376

393377
Replace the ``<connection string>`` placeholder with the
394378
connection string that you copied from the :ref:`c-get-started-connection-string`
395379
step of this guide.
396380

381+
.. step:: Set up a CMake application
382+
383+
To configure your application, create a ``CMakeLists.txt`` file in
384+
your project directory. Then, add the following code to the file:
385+
386+
.. literalinclude:: /includes/get-started/CMakeLists.txt
387+
:language: CMake
388+
:dedent:
389+
390+
The preceding code performs the following actions:
391+
392+
- Configures a C project.
393+
- Creates a ``quickstart.out`` executable for your application.
394+
- Finds and requires the {+driver-short+}. Replace the ``<version>``
395+
placeholder with your {+driver-short+} version number, such as ``{+full-version+}``.
396+
- Links the program to the ``libmongoc`` library.
397+
398+
.. note::
399+
400+
In the sample ``CMakeLists.txt`` file, the ``mongoc::mongoc`` target
401+
points to either the static library or the shared library.
402+
The library type depends on which one is available and
403+
whichever type the user specifies in the ``MONGOC_DEFAULT_IMPORTED_LIBRARY_TYPE``
404+
CMake configuration setting. If you don't set this value and
405+
both library types are available, ``mongoc::mongoc`` uses
406+
the static library.
407+
408+
You can use the ``mongoc::static`` target to explicitly use the
409+
static library or the ``mongoc::shared`` target to use the shared
410+
library.
411+
397412
.. step:: Run Your C Application
398413

399-
In your shell, run the following commands to compile and run this application:
414+
In your shell, run the following commands to build and run this application:
400415

401416
.. code-block:: none
402417

403-
gcc -o quickstartc quickstart.c $(pkg-config --libs --cflags libmongoc-1.0)
404-
./quickstartc
418+
cmake -S. -Bcmake-build
419+
cmake --build cmake-build --target quickstart.out
420+
./cmake-build/quickstart.out
405421

406422
The command line output contains details about the retrieved movie
407423
document:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmake_minimum_required(VERSION 3.11)
2+
project(c-quickstart LANGUAGES C)
3+
add_executable (quickstart.out quickstart.c)
4+
find_package(mongoc <version> REQUIRED)
5+
target_link_libraries(quickstart.out mongoc::mongoc)

0 commit comments

Comments
 (0)