Compiling Multi-file C++ Source Code with CMake

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 เม.ย. 2023
  • There are plenty of C++ tutorials around, but most don't cover the essential step of how to compile multiple source files into one program. This video show how, using CMake...
    Click here for a summary (and to download the example/template):
    keasigmadelta.com/blog/compil...
    Want to learn more CMake? Click the following link:
    keasigmadelta.com/store/landi...
    #programming #tech #softwaredevelopment #cmake #cpp #cpptutorial #cmaketutorial
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 30

  • @freznelite
    @freznelite 8 หลายเดือนก่อน +5

    FINALLY! Somebody who described how to include multiple source files!

    • @KeaSigmaDelta
      @KeaSigmaDelta  8 หลายเดือนก่อน

      You're welcome. Glad it's helpful.

  • @Sokenro
    @Sokenro 2 หลายเดือนก่อน

    You have single handedly saved me hours of troubleshooting. Thank you! No Instructor in any of my CS classes or any resources online have talked about how to compile multi-file code and my project kept having so many errors upon building.

    • @KeaSigmaDelta
      @KeaSigmaDelta  2 หลายเดือนก่อน

      Glad the video helped.

  • @alessandro_yt
    @alessandro_yt 15 วันที่ผ่านมา

    Your videos covering cmake are awesome. Any plans to explain autotools with this kinda detail@ explanations?

    • @KeaSigmaDelta
      @KeaSigmaDelta  15 วันที่ผ่านมา +1

      No plans for autotools, because I find it to be an absolute nightmare to use. Some people love to hate CMake, but it's a huge step up from autotools.

  • @theintjengineer
    @theintjengineer ปีที่แล้ว +2

    Yeah, after I understood how to work with CMake, all my C++ Projects are CMake Projects.
    PS:
    3:18 actually you didn't really generate a Makefile. You're on Windows and used MSVC in its default form[it used -G "Visual Studio 16 2019"] -> you actually created a VS Solution.
    But I got you haha
    I sometimes use Linux, and before moving to using Ninja as my generator, I also had CMake generate Makefiles for me.
    Good video. Keep it up.

    • @KeaSigmaDelta
      @KeaSigmaDelta  ปีที่แล้ว +1

      > But I got you haha
      Nah, you're just nitpicking. :-P
      Yes, it created a VS solution. I'm using "make file" as a generic catch-all for the file(s) that are then used for the actual build. The video isn't intended only for Windows devs.

  • @punchline1729
    @punchline1729 11 หลายเดือนก่อน

    Hi, thank you for this tutorial.
    I have a question regarding the first part of the video; I've already created the build folder using the " command palette ", and also I use " visual studio " as my compiler, do I need to delete the build folder and do as you did or is there a way to make my compiler compile my files without having to recreate the build folder?
    perhaps via the terminal

    • @KeaSigmaDelta
      @KeaSigmaDelta  11 หลายเดือนก่อน

      Only if you want to follow the tutorial 100%, and do a complete build from scratch using the commands I give. Otherwise, if the build scripts have already been created in the build folder (by "cmake .."), then you only need to run "cmake --build ." from within the build folder to run the actual build.

    • @punchline1729
      @punchline1729 11 หลายเดือนก่อน

      @@KeaSigmaDelta thank you so much, I subed
      please don't mind my poor English, since It's not my native tongue

    • @KeaSigmaDelta
      @KeaSigmaDelta  11 หลายเดือนก่อน

      @@punchline1729 You're welcome.

  • @sergeyshestakov4936
    @sergeyshestakov4936 6 หลายเดือนก่อน

    The best explanation!

  • @divyahegde5256
    @divyahegde5256 3 หลายเดือนก่อน

    Hii, thank you so much for the video!
    I followed your video, and had one doubt...
    'add_executable() ' should be kept as it is in the video or should it include the project and source file name that i kept?

    • @KeaSigmaDelta
      @KeaSigmaDelta  3 หลายเดือนก่อน

      I recommend keeping it the way it is in the video. The ${PROJECT_NAME} variable contains the project's name, so the executable will be given the same name as the project. If you ever want to change the project name, then you only have to change it once (in the project() call) instead of twice. In programming you generally want to avoid repeating yourself...
      Using a ${SOURCE_FILES} variable is far more flexible than adding files directly to add_executable(). For example, you may have source files that are specific for Windows, MacOS, etc. It's easy to conditionally add them to the ${SOURCE_FILES} variable, whereas you'd have to have multiple add_executable() calls otherwise, with the common files repeated.

    • @divyahegde5256
      @divyahegde5256 3 หลายเดือนก่อน

      @@KeaSigmaDelta Thank you for the clarification👍🤝

    • @KeaSigmaDelta
      @KeaSigmaDelta  3 หลายเดือนก่อน

      You're welcome.

  • @sergeyshestakov4936
    @sergeyshestakov4936 6 หลายเดือนก่อน

    Thanks!

  • @oracleoftroy
    @oracleoftroy 23 วันที่ผ่านมา

    I personally don't get why people like to set a variable for source files. I'd just list them in the add_executable or add_library call directly, or better, use target_sources. That way you can enable modern C++ features like modules fairly easily and use generator expressions for any platform specific files. If the build becomes complicated enough to justify the variable and it cant be simplified, ok, introduce some variables, but I don't think they add any value as a default.

    • @KeaSigmaDelta
      @KeaSigmaDelta  23 วันที่ผ่านมา

      Part of it's probably habit, which started with older build systems. I do think the script looks cleaner when setting a variable for source-files, though. Plus it's easier to switch when you do get to the point that it becomes necessary.

  • @vinniciusrosa8284
    @vinniciusrosa8284 4 หลายเดือนก่อน

    How can I select the compiler? 6:21 It does not appers to me.

    • @vinniciusrosa8284
      @vinniciusrosa8284 4 หลายเดือนก่อน

      I have gcc compiler installed

    • @KeaSigmaDelta
      @KeaSigmaDelta  4 หลายเดือนก่อน

      You can skip that step if you only have one compiler installed. CMake will automatically use the compiler you installed.
      I have multiple compilers installed, and therefore need to tell it which one to use. To change the chosen compiler, you need to click the tool icon on the CMake status bar (the blue bar at the bottom of the video). In the video you can see [Visual Studio Community 2019 Release -amd64] to the right of the tool icon.
      Unfortunately, they changed the CMake Tools plugin since I created the video, and it now hides the status bar by default (and it's no longer blue). You can enable the status bar in the plugin's settings. However, there's no need to do this if you have only one compiler installed.

  • @bittondb
    @bittondb 3 หลายเดือนก่อน

    When the filecount gets high, is GLOB the right way to go?

    • @KeaSigmaDelta
      @KeaSigmaDelta  3 หลายเดือนก่อน +2

      My personal opinion: no. It's best to be 100% precise as to which files are included in the build. That means you add them all into the build script.
      If the list of files is starting to get too large, then it's probably time to organize your files into sub-directories for different parts of the code (e.g., graphics code and audio code in separate sub-directories). Then you add the files in separate CMakeLists.txt files which you include into the main one.
      This is one of the things I plan to cover in The CMake Tutorial: cmaketutorial.com/

    • @DrFooMod2
      @DrFooMod2 3 หลายเดือนก่อน +1

      @@KeaSigmaDelta thanks. makes sense.

  • @plattcriceta1719
    @plattcriceta1719 ปีที่แล้ว

    isn't it sad that you actually have to write just another source code just to tell the compiler how it should work?

    • @KeaSigmaDelta
      @KeaSigmaDelta  ปีที่แล้ว +2

      Not really. The compiler must do exactly what it's told, which means giving it precise instructions.
      I suppose you could write a compiler that tries to guess (e.g., just compile all the *.c/*.cpp files in a directory), but that's guaranteed to get things wrong. So, you'd be back to giving it more precise instructions again.
      A few quick examples where compiling everything would be a problem:
      - Some source files are for specific platforms (e.g., different files for Windows, Linux, MacOS X, etc.)
      - Some source files need to be compiled with special flags (e.g., some files should be compiled with SIMD enabled, and others without)
      On top of that, you may be using external libraries, and you may need specific versions of those.