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.

hello-world.rst 6.5KB

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