# Native windows build of Garfield++

**URL:** <https://root-forum.cern.ch/t/native-windows-build-of-garfield/51915>\
**Category:** Garfield++\
**Created:** [October 10, 2022, 2:10pm UTC](https://root-forum.cern.ch/t/native-windows-build-of-garfield/51915 "2022-10-10T14:10:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shangjiaxuan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shangjiaxuan/32/29396_2.png) [@shangjiaxuan](https://root-forum.cern.ch/u/shangjiaxuan)\
**Post date:** [October 10, 2022, 2:10pm UTC](https://root-forum.cern.ch/t/native-windows-build-of-garfield/51915/1 "2022-10-10T14:10:18Z")

</div>

Been working on making garfield++ to compile as dynamic library on windows. Headers will still need to use \_\_declspec(dllimport) on most imported symbols, but some examples can run now.

> **[GitHub - shangjiaxuan/garfieldpp: My attempt to run and debug garfield++ on...](https://github.com/shangjiaxuan/garfieldpp/)**
>
> My attempt to run and debug garfield++ on windows with visual studio - GitHub - shangjiaxuan/garfieldpp: My attempt to run and debug garfield++ on windows with visual studio

Tested with ifort.

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [October 11, 2022, 1:24pm UTC](https://root-forum.cern.ch/t/native-windows-build-of-garfield/51915/2 "2022-10-11T13:24:26Z")

</div>

That’s great! Thanks so much. Do you think we could merge your changes to the main garfieldpp repository?

---

<div class="post-metadata">

**Author:** ![shangjiaxuan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shangjiaxuan/32/29396_2.png) [@shangjiaxuan](https://root-forum.cern.ch/u/shangjiaxuan)\
**Post date:** [October 11, 2022, 2:50pm UTC](https://root-forum.cern.ch/t/native-windows-build-of-garfield/51915/3 "2022-10-11T14:50:49Z")

</div>

Of course, but some places have hacks that may not be appropriate for normal compilation, need some work to make both compilation work.

Specifically, building megaboltz as dll on windows requires an export symbol table. I currently put that build file in megaboltz instead of make directory, and compile option of `target_link_options(${libname} PRIVATE /DEF:${CMAKE_CURRENT_SOURCE_DIR}/magboltz.def)` is used and replaced the unavailable `target_compile_options(${libname} PRIVATE -std=legacy)`, and need to manually remove the `/fpp` flag unconditionally generated by cmake by editing cache.

Will work on conditionally choosing former options tomorrow, but the `/fpp` cannot be easily removed and I’m currently filing an issue at cmake repository:

> **[IntelLLVM: Default Fortran flags on Windows hard-code /fpp (#24048) · Issues...](https://gitlab.kitware.com/cmake/cmake/-/issues/24048)**
>
> Came across a somewhat old piece of fortran code that cannot compile with /fpp specified in ifort: https://gitlab.cern.ch/garfield/garfieldpp/-/blob/master/Magboltz/magboltz.f With /fpp will output:

---

<div class="post-metadata">

**Author:** ![shangjiaxuan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shangjiaxuan/32/29396_2.png) [@shangjiaxuan](https://root-forum.cern.ch/u/shangjiaxuan)\
**Post date:** [October 12, 2022, 11:13am UTC](https://root-forum.cern.ch/t/native-windows-build-of-garfield/51915/4 "2022-10-12T11:13:58Z")

</div>

Updated to be ready-to-build. Also added instruction of how to build in `readme.md`. Non-windows and non-intel compiler platform should not be disturbed.

---

<div class="post-metadata">

**Author:** ![shangjiaxuan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shangjiaxuan/32/29396_2.png) [@shangjiaxuan](https://root-forum.cern.ch/u/shangjiaxuan)\
**Post date:** [October 12, 2022, 3:32pm UTC](https://root-forum.cern.ch/t/native-windows-build-of-garfield/51915/5 "2022-10-12T15:32:37Z")

</div>

Currently uses intel compiler to build, since it’s native to the visual studio build system. But should be possible to use mingw `gfortran` to build.

Since the c abi used is the same (assuming they have same struct alignment), one should be able to generate the `.lib` required by msvc to link and preload dynamic libraries (without explicit `dlopen` or `LoadLibrary`).

A custom build step using a `gfortran` compiler will work like this:

1. Compile using gfortran
2. Custom build step, generate `.lib` from `.dll`
3. Specify to link `.lib` into the executable in executable linker options

If the fortran runtime in turn relies on c-runtime, then binary copying on windows will become a bit tedious.

With some searches, it seems cmake already comes with a utility that does exactly this, thus does not need custom tool for this, will look into using this tomorrow:  
[https://www.kitware.com/fortran-for-cc-developers-made-easier-with-cmake/](https://www.kitware.com/fortran-for-cc-developers-made-easier-with-cmake/)

---

<div class="post-metadata">

**Author:** ![shangjiaxuan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shangjiaxuan/32/29396_2.png) [@shangjiaxuan](https://root-forum.cern.ch/u/shangjiaxuan)\
**Post date:** [October 13, 2022, 1:23pm UTC](https://root-forum.cern.ch/t/native-windows-build-of-garfield/51915/6 "2022-10-13T13:23:22Z")

</div>

Now the changes should be production ready, and should compile out of the box with correct envrionment variables set. Works with both gfortran and intel compilers.

A binary version of dependencies and megaboltz are uploaded to github release, should be able to choose from intel compilers and gfortran compiler. But since Garfield++ uses generated root dict file, and have absolute path in it, `Garfield.dll` itself is not portable and not uploaded.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [October 27, 2022, 1:23pm UTC](https://root-forum.cern.ch/t/native-windows-build-of-garfield/51915/7 "2022-10-27T13:23:35Z")

</div>

Closed on October 27, 2022, 1:23pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
