No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

244 líneas
7.1KB

  1. A *Hello, world!* Application
  2. #############################
  3. Creating a *hello world* application is very simple.
  4. Creating a *Package Root*
  5. *************************
  6. To start, create a new directory for your project. This will be known as the
  7. *package root*, and the entirety of our project will be placed in this
  8. directory. The name and location of this directory is not important, but the
  9. contents therein will be significant.
  10. .. note::
  11. The term *package root* is further described in the :doc:`/guide/packages` page.
  12. From here on, this created directory will simply be noted as ``<root>``. In
  13. the examples, this will refer to the package root directory we have created.
  14. Creating the First *Source Root*
  15. ********************************
  16. Within the package root, we create our first *source root*. Since we are
  17. intending to compile files, we need to use the name that ``dds`` has designated
  18. to be the source root that may contain compilable source files: ``src/``:
  19. .. code-block:: bash
  20. mkdir src
  21. You should now have a single item in the package root, at ``<root>/src/``. This
  22. is the directory from which ``dds`` will search for source files.
  23. Creating an Application Entrypoint
  24. **********************************
  25. To add a source file to our project, we simply create a file within a source
  26. root with the appropriate file extension. Our source root is ``<root>/src/``,
  27. so we'll place a source file in there. In addition, because we want to create
  28. an *application* we need to designate that the source file provides an
  29. application *entry point*, i.e. a ``main()`` function. To do this, we simply
  30. prepend ``.main`` to the file extension. Create a file::
  31. > <root>/src/hello-world.main.cpp
  32. and open it in your editor of choice. We'll add the classic C++ *hello, world*
  33. program:
  34. .. code-block:: c++
  35. :linenos:
  36. :caption: ``<root>/src/hello-world.main.cpp``
  37. #include <iostream>
  38. int main() {
  39. std::cout << "Hello, world!\n";
  40. }
  41. Building *Hello, World*
  42. ***********************
  43. Now comes the fun part. It is time to actually compile the application!
  44. .. important::
  45. If you intend to compile with Visual C++, the build must be executed
  46. from within a Visual Studio or Visual C++ development command prompt. These
  47. program shortcuts should be made available with any standard installation
  48. of the Visual C++ toolchain.
  49. ``dds`` **will not** automatically load the Visual C++ environment.
  50. To build the program, we must provide ``dds`` with information about our
  51. program toolchain. ``dds`` comes with a few "built in" toolchain options that
  52. can be used out-of-the-box, and they'll be suitable for our purposes.
  53. - If you are compiling with GCC, the toolchain name is ``:gcc``
  54. - If you are compiling with Clang, the toolchain name is ``:clang``
  55. - If you are compiling with Visual C++, the toolchain name is ``:msvc``
  56. .. note::
  57. The leading colon ``:`` is important: This tells ``dds`` to use its
  58. built-in toolchain information rather than looking for a toolchain file of
  59. that name.
  60. To execute the build, run the ``dds build`` command as in the following
  61. example, providing the appropriate toolchain name in place of ``<toolchain>``::
  62. > dds build -t <toolchain>
  63. For example, if you are using ``gcc``, you would run the command as::
  64. > dds build -t :gcc
  65. If all is successful, ``dds`` will emit information about the compile and link
  66. process, and then exit without error.
  67. By default, build results will be placed in a subdirectory of the package root
  68. named ``_build``. Within this directory, you will find the generated executable
  69. named ``hello-world`` (with a ``.exe`` suffix if on Windows).
  70. We should now be able to run this executable and see our ``Hello, world!``::
  71. > ./_build/hello-world
  72. Hello, world!
  73. More Sources
  74. ************
  75. Modularizing our program is good, right? Let's do that.
  76. Add a Header
  77. ************
  78. Create a new subdirectory of ``src``, and we'll call it ``hello``::
  79. > mkdir src/hello
  80. Within this directory, create a ``strings.hpp``. Edit the content in your
  81. editor of choice:
  82. .. code-block:: c++
  83. :caption: ``<root>/src/hello/strings.hpp``
  84. :linenos:
  85. #ifndef HELLO_STRINGS_HPP_INCLUDED
  86. #define HELLO_STRINGS_HPP_INCLUDED
  87. #include <string>
  88. namespace hello {
  89. std::string get_greeting();
  90. }
  91. #endif
  92. Change our ``main()``
  93. *********************
  94. Modify the content of ``<root>/src/hello-world.main.cpp`` to include our new
  95. header and to use our ``get_greeting()`` function:
  96. .. code-block:: c++
  97. :caption: ``<root>/src/hello-world.main.cpp``
  98. :linenos:
  99. :emphasize-lines: 1, 6
  100. #include <hello/strings.hpp>
  101. #include <iostream>
  102. int main() {
  103. std::cout << hello::get_greeting() << '\n';
  104. }
  105. Compiling Again, and Linking...?
  106. ********************************
  107. If you run the ``dds build`` command again, you will now see an error:
  108. .. code-block:: text
  109. [12:55:25] [info ] [dds-hello] Link: hello-world
  110. [12:55:25] [info ] [dds-hello] Link: hello-world - 57ms
  111. [12:55:25] [error] Failed to link executable '<root>/_build/hello-world'.
  112. ...
  113. <additional lines follow>
  114. The problem, of course, is that we've declared ``get_greeting`` to *exist*, but
  115. be haven't *defined it*.
  116. Adding Another Compiled Source
  117. ******************************
  118. We'll add another compilable source file to our project. In the same
  119. directory as ``strings.hpp``, add ``strings.cpp``:
  120. .. code-block:: c++
  121. :caption: ``<root>/src/hello/strings.cpp``
  122. :linenos:
  123. #include <hello/strings.hpp>
  124. std::string hello::get_greeting() {
  125. return "Hello, world!";
  126. }
  127. Compiling and Linking!
  128. **********************
  129. Run the ``dds build`` command again, and you'll find that the application
  130. successfully compiles and links!
  131. If you've used other build systems, you may have noticed a missing step: We
  132. never told ``dds`` about our new source file. Actually, we never told ``dds``
  133. about *any* of our source files. We never even told it the name of the
  134. executable to generate. What gives?
  135. It turns out, we *did* tell ``dds`` all of this information by simply placing
  136. the files on the filesystem with the appropriate file paths. The name of the
  137. executable, ``hello-world``, was inferred by stripping the trailing ``.main``
  138. from the stem of the filename which defined the entry point.
  139. Cleaning Up
  140. ***********
  141. There's one final formality that should be taken care of before proceeding:
  142. Creating a package manifest file.
  143. ``dds`` will work happily with packages that do not declare themselves, as long
  144. as the filesystem structure is sufficient. However: To use features covered in
  145. later tutorials, we'll need a simple ``package.json5`` file to declare
  146. information about are package. This file should be placed directly in the
  147. package root:
  148. .. code-block:: js
  149. :caption: ``<root>/package.json5``
  150. {
  151. name: 'hello-dds',
  152. version: '0.1.0',
  153. namespace: 'tutorial',
  154. }
  155. .. note::
  156. The ``namespace`` option will be discussed later.
  157. Rebuilding the project will show no difference at the moment.
  158. .. seealso::
  159. Creating a single application executable is fine and all, but what if we
  160. want to create libraries? See the next page: :doc:`hello-lib`