8

I am including an external project with ExternalProject_Add. What I want is to be able to do

cmake -DCMAKE_CXX_COMPILER=<some compiler> <assume correct path>

for the top-level project so that my chosen compiler propagates to the externally included projects. I expect something that I can put in the ExternalProject_Add command:

ExternalProject_Add (

  some_external_project

  PREFIX ...            # Assume this works.
  GIT_REPOSITORY ...    # Assume this works too.

  # What should I write here to tell it to use the ${CMAKE_CXX_COMPILER}
  # of the top-level project ?
)
dow
  • 513
  • 2
  • 5
  • 16
  • 1
    Possible duplicate of [How can CMake arguments be forwarded to ExternalProject](http://stackoverflow.com/questions/12021448/how-can-cmake-arguments-be-forwarded-to-externalproject) – Tsyvarev Jan 28 '17 at 23:01
  • @Tsyvarev Thanks. I have found a simple workaround that worked for me, so I will post it for quick reference. – dow Jan 28 '17 at 23:36

1 Answers1

20

The following worked for me in the top-level project:

ExternalProject_Add (

  some_external_project

  PREFIX ...          # Assume this works.
  GIT_REPOSITORY ...  # Assume this works too.

  # This did the trick for me.
  CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
)
dow
  • 513
  • 2
  • 5
  • 16
  • 5
    Why is this getting down-voted? Seems like a valid approach. – Torbjörn Jan 29 '17 at 20:53
  • I like the simplicity of this approach. Also note, this approach can do more than carrying over compiler options; it can carry over any flags in fact. – Samuel Li May 15 '21 at 06:33
  • 1
    This worked well for me. in my CMakeList.txt, I use ExternalProject_Add that surprisingly uses the default compiler (clang in my case because I develop on Mac with Xcode installed) rather than the compiler specified when running cmake (GNU gcc in my case). Compiling source with 2 different compilers create unresolved symbols. These 2 lines force cmake to use the specified compiler rather than the default one and it works like a charm. – Boris Jul 21 '21 at 07:20