# Passing list of libraries to ExternalProject\_Add

**URL:** https://discourse.cmake.org/t/passing-list-of-libraries-to-externalproject-add/13205
**Category:** Usage
**Created:** [December 14, 2024, 8:20pm UTC](https://discourse.cmake.org/t/passing-list-of-libraries-to-externalproject-add/13205 "2024-12-14T20:20:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ramcdona](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ramcdona/32/2439_2.png) [@ramcdona](https://discourse.cmake.org/u/ramcdona)
#### Post date: [December 14, 2024, 8:20pm UTC](https://discourse.cmake.org/t/passing-list-of-libraries-to-externalproject-add/13205/1 "2024-12-14T20:20:36Z")

</div>

I need to use two different compilers to build my entire project. This is because part of the project needs OpenMP support, but the primary compilers for some of the platforms do not have good OpenMP support. To do this, I build that part of the project as an ExternalProject – allowing CMake to set up an entirely separate environment. This has worked fine for a while.

My project has become more complex such that now the sub-project needs to link against some libraries from the main project. Consequently, I need to pass those library targets through.

In my main CMake project context, I:

```auto
ADD_LIBRARY( foo
foo.cpp
)

ADD_LIBRARY( bar
bar.cpp
)

SET( COMMON_LIBRARIES
    foo
    bar
)

ExternalProject_Add( SUBPROJECT
    #.....
    CMAKE_ARGS -DCMAKE_C_COMPILER=${C_OMP_COMPILER}
        -DCMAKE_CXX_COMPILER=${CXX_OMP_COMPILER}
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCOMMON_LIBRARIES=${COMMON_LIBRARIES}
    INSTALL_COMMAND ""
)

```

Then, in my SUBPROJECT context, I:

```auto
MESSAGE( STATUS "COMMON_LIBRARIES = ${COMMON_LIBRARIES}" )

TARGET_LINK_LIBRARIES( omp_subtarget PUBLIC ${COMMON_LIBRARIES} )

```

I presently have two problems…

In my SUBPROJECT context, only the first library in the list comes through. I.e. the MESSAGE statement outputs:

`COMMON_LIBRARIES = foo`

Second, only the library name is passed (not a target), so I either need to pass the full path, or I need to figure out a way to pass a target.

I think I will be able to resolve this with something based on

`get_target_property( full_foo_path foo LOCATION)`

but bonus points if someone can come up with a nice solution that iterates through the entire list of libraries in COMMON\_LIBRARIES, turning them into full absolute paths.

Thanks in advance for any help,

Rob

---

<div class="post-metadata">

### Author: ![ramcdona](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ramcdona/32/2439_2.png) [@ramcdona](https://discourse.cmake.org/u/ramcdona)
#### Post date: [December 15, 2024, 6:20am UTC](https://discourse.cmake.org/t/passing-list-of-libraries-to-externalproject-add/13205/2 "2024-12-15T06:20:50Z")

</div>

OK, I’ve written something that seems to loop over my libraries and build up the full paths as I needed…

```auto
set( lib_string )
foreach( lib ${COMMON_LIBRARIES} )
    if( TARGET ${lib} )
        get_target_property( libpath ${lib} BINARY_DIR )
        list( APPEND lib_string ${libpath}/${lib} )
    else()
        list( APPEND lib_string ${lib} )
    endif()
endforeach()

```

However, overall my solution still suffers the original problem that only the first list entry gets passed to the interior context. I have noticed that CMake does give an error…

```auto
CMake Warning:
  Ignoring extra path from command line:

   "/path/to/second/lib/bar"

```

---

<div class="post-metadata">

### Author: ![ramcdona](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ramcdona/32/2439_2.png) [@ramcdona](https://discourse.cmake.org/u/ramcdona)
#### Post date: [December 15, 2024, 6:24am UTC](https://discourse.cmake.org/t/passing-list-of-libraries-to-externalproject-add/13205/3 "2024-12-15T06:24:15Z")

</div>

I have figured out that the problem is with the ‘;’ list separator.

Somehow the shell interpreter gets involved and the semicolons are replaced with newlines. I’m on MacOS, but also need solutions that work on Windows and Linux.

I can think of a variety of possible ugly solutions – search and replace the semicolons with some other character. Then, in the interior context, switch them back.

However, this seems cumbersome and possibly challenging to make cross platform (different shells may escape characters differently or interpret characters differently.)

What is the canonical CMake way of handling this?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 20, 2026, 2:49am UTC](https://discourse.cmake.org/t/passing-list-of-libraries-to-externalproject-add/13205/4 "2026-02-20T02:49:16Z")

</div>

`ExternalProject_add` suppots the `LIST_SEPARATOR` specification. You can then replace `;` in your CMake code with that separator which will then be replaced with `;` properly. You can see how it is used [here](https://gitlab.kitware.com/paraview/common-superbuild/-/blob/cfaddca7b102d9370dcda5fcb790cd5c5253c01d/projects/matplotlib.cmake#L4) (it is `EP_add` under the hood).
