# Undefined third-party library symbols when building dylib on MacOS

**URL:** <https://discourse.cmake.org/t/undefined-third-party-library-symbols-when-building-dylib-on-macos/2671>\
**Category:** Code\
**Tags:** os:macos\
**Created:** [February 2, 2021, 1:05am UTC](https://discourse.cmake.org/t/undefined-third-party-library-symbols-when-building-dylib-on-macos/2671 "2021-02-02T01:05:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomoreilly](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomoreilly](https://discourse.cmake.org/u/tomoreilly)\
**Post date:** [February 2, 2021, 1:05am UTC](https://discourse.cmake.org/t/undefined-third-party-library-symbols-when-building-dylib-on-macos/2671/1 "2021-02-02T01:05:56Z")

</div>

The following command generates a Makefile on linux that properly builds a .so library:

```
# Shared library
add_library(mbauxShared
            SHARED
            mb_cheb.c mb_delaun.c mb_intersectgrid.c mb_readwritegrd.c 
            mb_surface.c mb_track.c mb_truecont.c mb_zgrid.c)

```

However the same command generates a Makefile on MacOS that results in many undefined symbol errors, for symbols defined in third-party libraries. What are the proper commands that prevent this problem?

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [February 2, 2021, 7:49am UTC](https://discourse.cmake.org/t/undefined-third-party-library-symbols-when-building-dylib-on-macos/2671/2 "2021-02-02T07:49:44Z")

</div>

> [@tomoreilly](#):
>
> many undefined symbol errors

What are the exact errors?

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [February 2, 2021, 8:01am UTC](https://discourse.cmake.org/t/undefined-third-party-library-symbols-when-building-dylib-on-macos/2671/3 "2021-02-02T08:01:10Z")

</div>

> [@tomoreilly](#):
>
> However the same command generates a Makefile on MacOS that results in many undefined symbol errors, for symbols defined in third-party libraries.

If your library requires external (third-part) libraries to link, there should probably be a [`target_link_libraries(mbauxShared ...)`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) command somewhere to link the libraries in.

---

<div class="post-metadata">

**Author:** ![tomoreilly](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomoreilly](https://discourse.cmake.org/u/tomoreilly)\
**Post date:** [February 2, 2021, 4:10pm UTC](https://discourse.cmake.org/t/undefined-third-party-library-symbols-when-building-dylib-on-macos/2671/4 "2021-02-02T16:10:19Z")

</div>

The issue - and what looks to be a good solution - [is described here](https://stackoverflow.com/questions/54724207/linker-issues-with-shared-library-on-macos-using-cmake?rq=1).

---

<div class="post-metadata">

**Author:** ![Juan\_Sanchez](https://discourse.cmake.org/user_avatar/discourse.cmake.org/juan_sanchez/32/744_2.png) [@Juan\_Sanchez](https://discourse.cmake.org/u/Juan_Sanchez)\
**Post date:** [February 3, 2021, 11:00pm UTC](https://discourse.cmake.org/t/undefined-third-party-library-symbols-when-building-dylib-on-macos/2671/5 "2021-02-03T23:00:20Z")

</div>

I use the following linker option on macOS when I know that the symbols will be satisfied at runtime:

```
-undefined dynamic_lookup

```

This makes it possible in my project to not link against a specific version of the Python dll, since it is the Python dll loading my library anyway.

This flag depends on whether or not you are using Linux style linking:

```
-flat_namespace

```

To make this macOS specific:

```
IF (APPLE)
SET_TARGET_PROPERTIES(devsim_py3 PROPERTIES LINK_FLAGS
"-undefined dynamic_lookup -flat_namespace")
ENDIF (APPLE)
```
