# Adding an external library to my project

**URL:** https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784
**Category:** Usage
**Created:** [March 20, 2025, 6:13am UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784 "2025-03-20T06:13:58Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![fracton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@fracton](https://discourse.cmake.org/u/fracton)
#### Post date: [March 20, 2025, 6:13am UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/1 "2025-03-20T06:13:58Z")

</div>

Hi,

I am new to cmake and am trying to build my fortran project with an external library that has been added as a git submodule to my project repository. The external project has a CMakeLists.txt file and can be built using CMake. However, I am having hard time trying to build my project with it.

```cmake
....
include_directories(${CMAKE_SOURCE_DIR}/external/{package}/include)

add_executable(myproject ${SUBROUTINES} ${MODULES} ${libraries})

# Link to the library

target_link_libraries(myproject PUBLIC package)

# Link libraries

# target_link_libraries(myprojtect ${libraries} package)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)

```

I use simple `use {package}` in my fortran code to reference the package. However, when I try to build with CMake I get the following error:

```auto
~/myproject/src/mymodule.f90(4): error #7002: Error in opening the compiled module file. Check INCLUDE paths.

```

---

<div class="post-metadata">

### Author: ![LegalizeAdulthood](https://discourse.cmake.org/user_avatar/discourse.cmake.org/legalizeadulthood/32/5282_2.png) [@LegalizeAdulthood](https://discourse.cmake.org/u/LegalizeAdulthood)
#### Post date: [March 20, 2025, 9:24pm UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/2 "2025-03-20T21:24:16Z")

</div>

> [@fracton](#):
>
> The external project has a CMakeLists.txt file and can be built using CMake.

The usual thing to do is to use `add_subdirectory` for the project you’ve brought in as a git submodule. Did you try that?

---

<div class="post-metadata">

### Author: ![fracton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@fracton](https://discourse.cmake.org/u/fracton)
#### Post date: [March 20, 2025, 9:59pm UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/3 "2025-03-20T21:59:43Z")

</div>

I did but even then my target compilation fails as the module in my project is not ablet find the external package. How do I link my target explicitly with an external library compiled with cmake?

---

<div class="post-metadata">

### Author: ![LegalizeAdulthood](https://discourse.cmake.org/user_avatar/discourse.cmake.org/legalizeadulthood/32/5282_2.png) [@LegalizeAdulthood](https://discourse.cmake.org/u/LegalizeAdulthood)
#### Post date: [March 21, 2025, 3:27pm UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/4 "2025-03-21T15:27:58Z")

</div>

If you’ve done `add_subdirectory` then the external library should have been defined as a target in CMake and you use `target_link_libraries` to make your target link against the external library.

Did you do that?

---

<div class="post-metadata">

### Author: ![fracton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@fracton](https://discourse.cmake.org/u/fracton)
#### Post date: [March 21, 2025, 6:49pm UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/5 "2025-03-21T18:49:04Z")

</div>

> [@LegalizeAdulthood](#):
>
> If you’ve done `add_subdirectory` then the external library should have been defined as a target in CMake

I am sorry, but I am not sure where to define it as a target in my library’s CMake file?

---

<div class="post-metadata">

### Author: ![LegalizeAdulthood](https://discourse.cmake.org/user_avatar/discourse.cmake.org/legalizeadulthood/32/5282_2.png) [@LegalizeAdulthood](https://discourse.cmake.org/u/LegalizeAdulthood)
#### Post date: [March 21, 2025, 8:36pm UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/6 "2025-03-21T20:36:40Z")

</div>

The CMakeLists.txt in the external library creates targets, just like you do, with `add_library`.

Look at the other library’s CMakeLists.txt and find where it adds the library and use that target name to link against.

---

<div class="post-metadata">

### Author: ![fracton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@fracton](https://discourse.cmake.org/u/fracton)
#### Post date: [March 22, 2025, 2:30pm UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/7 "2025-03-22T14:30:15Z")

</div>

I tried this but CMake still doesn’t seem to recognize the library and shows an undefined reference error in my fortran module where I call it.

---

<div class="post-metadata">

### Author: ![LegalizeAdulthood](https://discourse.cmake.org/user_avatar/discourse.cmake.org/legalizeadulthood/32/5282_2.png) [@LegalizeAdulthood](https://discourse.cmake.org/u/LegalizeAdulthood)
#### Post date: [March 24, 2025, 7:45pm UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/8 "2025-03-24T19:45:48Z")

</div>

Post a link to the library you’re trying to use with `add_subdirectory`

---

<div class="post-metadata">

### Author: ![fracton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@fracton](https://discourse.cmake.org/u/fracton)
#### Post date: [March 24, 2025, 8:07pm UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/9 "2025-03-24T20:07:15Z")

</div>

Here’s the link to the library: [GitHub - stenczelt/libmace](https://github.com/stenczelt/libmace)

The interface code in my Fortran project is similar the examples/main.f90 in the above repo.

---

<div class="post-metadata">

### Author: ![LegalizeAdulthood](https://discourse.cmake.org/user_avatar/discourse.cmake.org/legalizeadulthood/32/5282_2.png) [@LegalizeAdulthood](https://discourse.cmake.org/u/LegalizeAdulthood)
#### Post date: [March 24, 2025, 10:27pm UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/10 "2025-03-24T22:27:29Z")

</div>

Looks to me like this is the target you want, since you said you are building Fortran:

> <https://github.com/stenczelt/libmace/blob/87202644edcd846bf97fcec2d28c50eb3e7f54db/CMakeLists.txt#L64>

---

<div class="post-metadata">

### Author: ![fracton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@fracton](https://discourse.cmake.org/u/fracton)
#### Post date: [March 25, 2025, 9:16am UTC](https://discourse.cmake.org/t/adding-an-external-library-to-my-project/13784/11 "2025-03-25T09:16:15Z")

</div>

> [@LegalizeAdulthood](#):
>
> `add_library(mace_fortran INTERFACE)`

I tried this and I still get an undefind reference error. I tried to link the target of my library with the above target and didn’t work.
