# How to compile and run the files.

**URL:** https://discourse.slicer.org/t/how-to-compile-and-run-the-files/33835
**Category:** Development
**Tags:** build
**Created:** [January 17, 2024, 4:19pm UTC](https://discourse.slicer.org/t/how-to-compile-and-run-the-files/33835 "2024-01-17T16:19:30Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Arun03Kumar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/arun03kumar/32/68980_2.png) [@Arun03Kumar](https://discourse.slicer.org/u/Arun03Kumar)
#### Post date: [January 17, 2024, 4:19pm UTC](https://discourse.slicer.org/t/how-to-compile-and-run-the-files/33835/1 "2024-01-17T16:19:30Z")

</div>

my build is completed just now, now I have two folders one contains the repository cloned file and another one contains the build files, the build folder now has a lot more folders, now I am totally confused that in which directory I have to change the source code??  
and let’s suppose I changed some code in a file then how can I see the reflection of it? by building it again or what commands should I use to compile the whole file or what is the procedure because till now I’ve worked with only one or two c++ files which I simply compile and run, but here I have no clue.

---

<div class="post-metadata">

### Author: ![RafaelPalomar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rafaelpalomar/32/1436_2.png) [@RafaelPalomar](https://discourse.slicer.org/u/RafaelPalomar)
#### Post date: [January 17, 2024, 5:45pm UTC](https://discourse.slicer.org/t/how-to-compile-and-run-the-files/33835/2 "2024-01-17T17:45:39Z")

</div>

Hello @Arun03Kumar.

Directly modifying 3D Slicer can be overwhelming and may not always be sustainable. Given the fast-paced development of the main repository, your changes might not align with the ongoing updates, making them challenging to integrate.

Perhaps a better approach would be to explore development through extensions. Extensions allow you to add functionality to 3D Slicer without altering the core codebase of 3D Slicer. This method ensures (to a large degree) your work remains compatible with future updates of 3D Slicer and is easier to manage. You can get more information in the [extensions documentation](https://slicer.readthedocs.io/en/latest/developer_guide/extensions.html)

However, if you still wish to modify the core code, remember that the repository you cloned is where you make changes. Your build directory is for compiled files and shouldn’t be modified for code changes. After editing the source code in the repository, return to the build directory and recompile to see your changes. The [developer guide](https://slicer.readthedocs.io/en/latest/developer_guide/index.html) will help you understand the structure of the project.

I hope this helps.

---

<div class="post-metadata">

### Author: ![Arun03Kumar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/arun03kumar/32/68980_2.png) [@Arun03Kumar](https://discourse.slicer.org/u/Arun03Kumar)
#### Post date: [January 17, 2024, 8:08pm UTC](https://discourse.slicer.org/t/how-to-compile-and-run-the-files/33835/3 "2024-01-17T20:08:30Z")

</div>

Thank you so much @RafaelPalomar.

so if I want to change some thing then I’ll change in the source directory, then go back to my build directory and just run “cmake --build” in build directory. then run the slicer.exe from slicer-build folder to see the changes.  
is this the correct process or should I have to do something else??  
can you please elaborate in step by step manner, till now I have just compiled only single c++ file using gcc command.

Thank you.

---

<div class="post-metadata">

### Author: ![RafaelPalomar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rafaelpalomar/32/1436_2.png) [@RafaelPalomar](https://discourse.slicer.org/u/RafaelPalomar)
#### Post date: [January 18, 2024, 5:51am UTC](https://discourse.slicer.org/t/how-to-compile-and-run-the-files/33835/4 "2024-01-18T05:51:29Z")

</div>

> [@Arun03Kumar](#):
>
> so if I want to change some thing then I’ll change in the source directory, then go back to my build directory and just run “cmake --build” in build directory. then run the slicer.exe from slicer-build folder to see the changes.

That’s correct. The cmake build command may require specification of a directory i.e., `cmake --build .`.

> [@Arun03Kumar](#):
>
> can you please elaborate in step by step manner, till now I have just compiled only single c++ file using gcc command.

3D Slicer uses the CMake build system to manage the project. Manually compiling individual files is not a good idea, introducing changes require updating the project output in a wider manner. You can refer to the [CMake documentation](https://cmake.org/cmake/help/latest/index.html) to understand the workflow used in CMake-based projects.

---

<div class="post-metadata">

### Author: ![Arun03Kumar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/arun03kumar/32/68980_2.png) [@Arun03Kumar](https://discourse.slicer.org/u/Arun03Kumar)
#### Post date: [January 18, 2024, 9:58am UTC](https://discourse.slicer.org/t/how-to-compile-and-run-the-files/33835/5 "2024-01-18T09:58:47Z")

</div>

okay, thank you. 👍

---

<div class="post-metadata">

### Author: ![Arun03Kumar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/arun03kumar/32/68980_2.png) [@Arun03Kumar](https://discourse.slicer.org/u/Arun03Kumar)
#### Post date: [January 20, 2024, 7:06pm UTC](https://discourse.slicer.org/t/how-to-compile-and-run-the-files/33835/6 "2024-01-20T19:06:06Z")

</div>

I tried to recompile the source after a small change with 'cmake --build ." command in SD folder which is build folder as specified on slice build documentation, but it still took the same time to build (around 9 hours).

I am not following that if I change something then should I have to wait until the next build.

or you just please the steps involved in compiling and seeing the output after a change in source directory.

my directory structure is same as specified on github build documentation  
source - "C:/D/S  
debug build - "C:/D/SD/

---

<div class="post-metadata">

### Author: ![Thibault\_Pelletier](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/thibault_pelletier/32/4767_2.png) [@Thibault\_Pelletier](https://discourse.slicer.org/u/Thibault_Pelletier)
#### Post date: [January 21, 2024, 7:30am UTC](https://discourse.slicer.org/t/how-to-compile-and-run-the-files/33835/7 "2024-01-21T07:30:29Z")

</div>

Hi @Arun03Kumar,

Slicer’s build system works with a superbuild, meaning that the “C:/D/S” folder contains both Slicer’s build tree and Slicer’s dependencies build trees.

After the first build, if you want to build only Slicer, you can navigate to the “C:/D/S/Slicer-build” dir and build only this one.

Note also that Slicer is modularized which can help if you want to build only the target you have modified (which will be much faster).

Best,  
Thibault

---

<div class="post-metadata">

### Author: ![RafaelPalomar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rafaelpalomar/32/1436_2.png) [@RafaelPalomar](https://discourse.slicer.org/u/RafaelPalomar)
#### Post date: [January 24, 2024, 4:02pm UTC](https://discourse.slicer.org/t/how-to-compile-and-run-the-files/33835/8 "2024-01-24T16:02:48Z")

</div>

> [@Arun03Kumar](#):
>
> but it still took the same time to build (around 9 hours).

Building 3D Slicer implies building many of its dependencies. You can improve the building time of Slicer (first-time, but also subsequent builds by the use parallel jobs i.e., `cmake --build -j9` where 9 was the number of parallel jobs). The rule of thumb is to use `number_of_threads+1`, but there is absolutely no science behind it and depends on what you build. In addition increasing the number of parallel jobs, generally, increases the memory required (if you go above, your computer can “freeze” while compiling). More info at [GNU/Linux systems — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/build_instructions/linux.html#build-slicer)
