grigor
grigor

Reputation: 1624

cmake doesn't set MPI_C_LIBRARIES unless it's run twice

I'm trying to use cmake to compile C++ code with MPI. In CMakeLists.txt I have:

find_package(MPI)

and later

if(MPI_FOUND)
    MESSAGE("{MPI_C_LIBRARIES}")
    target_link_libraries(test ${MPI_C_LIBRARIES})
endif(MPI_FOUND)

I have OpenMPI installed. The first time I run cmake I get:

-- Found MPI_CXX: /usr/local/lib/libmpi_cxx.dylib;/usr/local/lib/libmpi.dylib

and an empty line where it should print MPI_C_LIBRARIES. If I try to do make it doesn't link into the MPI libraries and gives an error. However, if I run cmake a second time I get the message correctly:

/usr/local/lib/libmpi_cxx.dylib;/usr/local/lib/libmpi.dylib

and it links to the libraries just fine. So in summary, I have to run cmake twice to make sure MPI_C_LIBRARIES are set. Does anyone know if this is normal or if I'm doing something wrong?

Upvotes: 4

Views: 1853

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54589

Most likely, you are doing something wrong.

The find_package call is supposed to fill the respective variables in the cache. As soon as the find_package call returns, you should be able to use the results.

As to what might have gone wrong:

  • Verify that you indeed execute the find_package call before you try to use the results. This should be straightforward for a simple CMake file, but can be quite challenging for more complex build scripts. Generous use of message commands should help debugging this.
  • Once you are sure that the find_package call happens in the right place, you need to identify what causes it to succeed on the second run, while it failed on the first. Most likely, you set a cached variable somewhere in your CMake script after calling find_package. On the second run, the cached value is available to the find script and that allows it to complete its job. Check the find script's source to identify the places where it depends on the value of global and/or cached variables.

Upvotes: 3

Related Questions