選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

245 行
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 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 now be able to run this executable and see our ``Hello, world!``::
  72. > ./_build/hello-world
  73. Hello, world!
  74. More Sources
  75. ************
  76. Modularizing our program is good, right? Let's do that.
  77. Add a Header
  78. ************
  79. Create a new subdirectory of ``src``, and we'll call it ``hello``::
  80. > mkdir src/hello
  81. Within this directory, create a ``strings.hpp``. Edit the content in your
  82. editor of choice:
  83. .. code-block:: c++
  84. :caption: ``<root>/src/hello/strings.hpp``
  85. :linenos:
  86. #ifndef HELLO_STRINGS_HPP_INCLUDED
  87. #define HELLO_STRINGS_HPP_INCLUDED
  88. #include <string>
  89. namespace hello {
  90. std::string get_greeting();
  91. }
  92. #endif
  93. Change our ``main()``
  94. *********************
  95. Modify the content of ``<root>/src/hello-world.main.cpp`` to include our new
  96. header and to use our ``get_greeting()`` function:
  97. .. code-block:: c++
  98. :caption: ``<root>/src/hello-world.main.cpp``
  99. :linenos:
  100. :emphasize-lines: 1, 6
  101. #include <hello/strings.hpp>
  102. #include <iostream>
  103. int main() {
  104. std::cout << hello::get_greeting() << '\n';
  105. }
  106. Compiling Again, and Linking...?
  107. ********************************
  108. If you run the ``dds build`` command again, you will now see an error:
  109. .. code-block:: text
  110. [12:55:25] [info ] [dds-hello] Link: hello-world
  111. [12:55:25] [info ] [dds-hello] Link: hello-world - 57ms
  112. [12:55:25] [error] Failed to link executable '<root>/_build/hello-world'.
  113. ...
  114. <additional lines follow>
  115. The problem, of course, is that we've declared ``get_greeting`` to *exist*, but
  116. be haven't *defined it*.
  117. Adding Another Compiled Source
  118. ******************************
  119. We'll add another compilable source file to our project. In the same
  120. directory as ``strings.hpp``, add ``strings.cpp``:
  121. .. code-block:: c++
  122. :caption: ``<root>/src/hello/strings.cpp``
  123. :linenos:
  124. #include <hello/strings.hpp>
  125. std::string hello::get_greeting() {
  126. return "Hello, world!";
  127. }
  128. Compiling and Linking!
  129. **********************
  130. Run the ``dds build`` command again, and you'll find that the application
  131. successfully compiles and links!
  132. If you've used other build systems, you may have noticed a missing step: We
  133. never told ``dds`` about our new source file. Actually, we never told ``dds``
  134. about *any* of our source files. We never even told it the name of the
  135. executable to generate. What gives?
  136. It turns out, we *did* tell ``dds`` all of this information by simply placing
  137. the files on the filesystem with the appropriate file paths. The name of the
  138. executable, ``hello-world``, was inferred by stripping the trailing ``.main``
  139. from the stem of the filename which defined the entry point.
  140. Cleaning Up
  141. ***********
  142. There's one final formality that should be taken care of before proceeding:
  143. Creating a package manifest file.
  144. ``dds`` will work happily with packages that do not declare themselves, as long
  145. as the filesystem structure is sufficient. However: To use features covered in
  146. later tutorials, we'll need a simple ``package.json5`` file to declare
  147. information about are package. This file should be placed directly in the
  148. package root:
  149. .. code-block:: js
  150. :caption: ``<root>/package.json5``
  151. {
  152. name: 'hello-dds',
  153. version: '0.1.0',
  154. namespace: 'tutorial',
  155. }
  156. .. note::
  157. The ``namespace`` option will be discussed later.
  158. Rebuilding the project will show no difference at the moment.
  159. .. seealso::
  160. Creating a single application executable is fine and all, but what if we
  161. want to create libraries? See the next page: :doc:`hello-lib`