CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Installation (https://www.cfd-online.com/Forums/openfoam-installation/)
-   -   [OpenFOAM.org] Build OPENFOAM from Source and Debug with VisualCode (https://www.cfd-online.com/Forums/openfoam-installation/246646-build-openfoam-source-debug-visualcode.html)

Claudia.Park December 14, 2022 10:47

Build OPENFOAM from Source and Debug with VisualCode
 
Hello,
Season's greetings!
Can someone please help me with setting up the openfoam repository in visual studio code or any other opensource IDE. I wish to debug into the source code repository with my added functions.

Any lead or step-by-step process to do this will be appreciated. Thanks

reverseila December 16, 2022 17:03

Hello Claudia

First compile OpenFOAM in debug mode. For that you have to set the following environment variable to Debug in
$WM_PROJECT_DIR/etc/bashrc before compiling OpenFOAM

Code:

# [WM_COMPILE_OPTION] - Optimised(default), debug, profiling, other:
# = Opt | Debug | Prof
export WM_COMPILE_OPTION=Opt  # change this to Debug

Tobias Holzmann have a video series in his website that touch on this (watch 2nd video).

Then you have to install VSCode and the C/C++ extension (from MicroSoft)...

Now, load OpenFOAM environment and open the VSCode in the root of your project,

Code:

cd path/to/my/project
of22 # or your version of Foam
code .

Next, open an arbitrary source file (at least one that contains a #include "..." statement). VSCode will probably issues warnings/errors that it can't find some header files.

Hover over one of the missing header files and click on the "Quick Fix..." and then click on the "Edit includePath settings". It will open a tab + create a .vscode directory in the root of the workspace. Close the tab! Open the file [font=courier]c_cpp_properties.json[font] and edit it as follows

Code:

{
            ...
            "includePath": [
                "${workspaceFolder}/**",
                "${env:FOAM_SRC}/**"
            ],
            ...
}

With this, VSCode can find all the source codes in the OpenFOAM libraries ($FOAM_SRC)...
If you have any other directories that contain source code, you can also put them here. for example

Code:

{
            ...
            "includePath": [
                "${workspaceFolder}/**",
                "${env:FOAM_SRC}/**",
                "${env:WM_PROJECT_USER_DIR}/src/**",
                "${env:WM_PROJECT_USER_DIR}/applications/solvers/mySolver/"
            ],
            ...
}

After you've done that, VSCode does some magic (look at the fire icon down in the bottom bar) and then you can jump around in the code. You can find definitions of most of the functions and variables (hopefully) by doing a right click and go to definition or go to references... (Note that this doesn't work in snippet files like "createFields.H")

If you also want to run & debug your code in VSCode, you can follow links below,

Claudia.Park December 16, 2022 23:48

Hi reverseila! On it :)
Thank you so much for the details. Really appreciate it.


All times are GMT -4. The time now is 07:07.