# Problems building Garfield++ examples and linking in application

**URL:** <https://root-forum.cern.ch/t/problems-building-garfield-examples-and-linking-in-application/46035>\
**Category:** Garfield++\
**Tags:** installation\
**Created:** [July 27, 2021, 8:06pm UTC](https://root-forum.cern.ch/t/problems-building-garfield-examples-and-linking-in-application/46035 "2021-07-27T20:06:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lobis](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lobis/32/25455_2.png) [@lobis](https://root-forum.cern.ch/u/lobis)\
**Post date:** [July 27, 2021, 8:06pm UTC](https://root-forum.cern.ch/t/problems-building-garfield-examples-and-linking-in-application/46035/1 "2021-07-27T20:06:15Z")

</div>

Hello,

I installed Garfield according to [Files · 4.0 · garfield / garfieldpp · GitLab](https://gitlab.cern.ch/garfield/garfieldpp/-/tree/4.0) (I am using the tag 4.0). Installation worked without issues. I then sourced the setup script `source $GARFIELD_INSTALL/share/Garfield/setupGarfield.sh`. The environment variables look fine.

I go to the source code to build an example (`Gem`example) which has the following `CMakeLists.txt` ([Examples/Gem/CMakeLists.txt · 4.0 · garfield / garfieldpp · GitLab](https://gitlab.cern.ch/garfield/garfieldpp/-/blob/4.0/Examples/Gem/CMakeLists.txt))

```auto
# ########################################################################################
# Use the code in this section for standalone projects
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
project(gem)
if(NOT TARGET Garfield::Garfield)
  find_package(Garfield REQUIRED)
endif()
# ########################################################################################

# ---Build executable------------------------------------------------------------
add_executable(gem gem.C)
target_link_libraries(gem Garfield::Garfield)

# ---Copy all files locally to the build directory-------------------------------
foreach(_file ELIST.lis MPLIST.lis NLIST.lis PRNSOL.lis)
  configure_file(${_file} ${CMAKE_CURRENT_BINARY_DIR}/${_file} COPYONLY)
endforeach()

```

When doing the `cmake` command I get the following error:

```auto
lobis@sultan2:~/github/garfieldpp/Examples/Gem/build$ cmake ..
-- The C compiler identification is GNU 8.3.0
-- The CXX compiler identification is GNU 8.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
CMake Error at CMakeLists.txt:11 (add_executable):
  Target "gem" links to target "Garfield::Garfield" but the target was not
  found. Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?

-- Generating done
-- Build files have been written to: /home/lobis/github/garfieldpp/Examples/Gem/build
lobis@sultan2:~/github/garfieldpp/Examples/Gem/build$

```

If I change the line `target_link_libraries(gem Garfield::Garfield)` to `target_link_libraries(gem Garfield)` the example can be built and run correctly.

In order to link to Garfield in my application I follow the instructions, `find_package(Garfield REQUIRED)` works OK. However, if I add `Garfield::Garfield` to `target_link_libraries` I get the following cmake error:

```auto
CMake Error at CMakeLists.txt:22 (add_executable):
  Target "GarfieldExample" links to target "GSL::gsl" but the target was not
  found. Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?

```

I can still compile but it will give errors about missing libraries, using `Garfield` or `Garfield::Garfield` regardless.

I am using cmake version 3.13.4

Edit: I was building a Docker image to reproduce this problem but the installation / linking works without an issue. I guess there is a problem with my local instalation, any ideas? Thanks!

---

<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:** [July 28, 2021, 5:35am UTC](https://root-forum.cern.ch/t/problems-building-garfield-examples-and-linking-in-application/46035/2 "2021-07-28T05:35:03Z")

</div>

@caiazza, do you have time to take a look?

---

<div class="post-metadata">

**Author:** ![caiazza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/caiazza/32/13962_2.png) [@caiazza](https://root-forum.cern.ch/u/caiazza)\
**Post date:** [July 28, 2021, 6:15am UTC](https://root-forum.cern.ch/t/problems-building-garfield-examples-and-linking-in-application/46035/3 "2021-07-28T06:15:34Z")

</div>

I’ll try to take a look later

---

<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:** [August 11, 2021, 6:16am UTC](https://root-forum.cern.ch/t/problems-building-garfield-examples-and-linking-in-application/46035/4 "2021-08-11T06:16:23Z")

</div>

Closed on August 11, 2021, 6:16am UTC

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