0

I am trying to configure GMP on CLion (Apple Silicon), for use in my C++ programmes, and have the following CMakeLists.txt:

    project(cs50)

    cmake_minimum_required(VERSION 2.8)
    set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
    find_package(GMP REQUIRED)
    add_executable(Turrial main.cpp)
    target_link_libraries(Turrial gmp gmpxx)

It seems like I need a FindGMP.cmake file too, as the error when I load the CMake changes is:

CMake Error at CMakeLists.txt:5 (find_package):
  By not providing "FindGMP.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "GMP", but
  CMake did not find one.

  Could not find a package configuration file provided by "GMP" with any of
  the following names:

    GMPConfig.cmake
    gmp-config.cmake

  Add the installation prefix of "GMP" to CMAKE_PREFIX_PATH or set "GMP_DIR"
  to a directory containing one of the above files.  If "GMP" provides a
  separate development package or SDK, be sure it has been installed.


-- Configuring incomplete, errors occurred!
See also "/Users/mikaelbashir/CLionProjects/cs50/cmake-build-debug/CMakeFiles/CMakeOutput.log".
See also "/Users/mikaelbashir/CLionProjects/cs50/cmake-build-debug/CMakeFiles/CMakeError.log".

[Failed to reload]

I've looked at many sources online, and the closest thing to what I (think I) need is for C programmes at [https://stackoverflow.com/questions/29307862/error-linking-gmp-library/29309437#29309437], so I don't know if the FindGMP.cmake file needs to be modified or not, or how to tell CmakeLists.txt where to find it. I installed GMP using homebrew, and the related files I have are:

    /opt/homebrew/Cellar/gmp/6.2.1_1/include/ (2 files)
    /opt/homebrew/Cellar/gmp/6.2.1_1/lib/libgmp.10.dylib
    /opt/homebrew/Cellar/gmp/6.2.1_1/lib/libgmpxx.4.dylib
    /opt/homebrew/Cellar/gmp/6.2.1_1/lib/pkgconfig/ (2 files)
    /opt/homebrew/Cellar/gmp/6.2.1_1/lib/ (4 other files)
    /opt/homebrew/Cellar/gmp/6.2.1_1/share/info/ (3 files)

My question is, how can I fix this error so that GMP is correctly configured in my project. Please be as specific as possible, as I am relatively bad at programming and new to C++.

I tried many different CMakeLists.txt files but none seem to be working. The one I mentioned at least allows CLion to find <gmpxx.h>, but still I am struggling to get CLion to configure GMP.

  • 1
    As you can see from the error message, using `find_package` requires a file `GMPConfig.cmake` or `gmp-config.cmake` to be existed in your GMP installation. Since you have no such file, you cannot use `find_package` approach for GMP. But since GMP installation has pkgconfig files, you may use `pkg-config` approach for locate this package. See [that question](https://stackoverflow.com/questions/29191855/what-is-the-proper-way-to-use-pkg-config-from-cmake) for more details about that approach. – Tsyvarev Aug 24 '23 at 22:20

1 Answers1

0

That pkgconfig directory contains pkg-config files, which contain information for the compiler.

In CMake you can use the FindPkgConfig module to interpret these files for you and create a CMake-native target you can link to:

find_package(PkgConfig REQUIRED)
pkg_check_modules(GMPXX REQUIRED IMPORTED_TARGET gmpxx)
target_link_libraries(Turrial PkgConfig::GMPXX)
Botje
  • 26,269
  • 3
  • 31
  • 41
  • After changing the CMakeLists.txt to: "project(cs50) cmake_minimum_required(VERSION 2.8) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}") find_package(PkgConfig REQUIRED) pkg_check_modules(GMPXX REQUIRED IMPORTED_TARGET gmpxx) add_executable(Turrial main.cpp) target_link_libraries(Turrial PkgConfig::GMPXX) ", I get an error saying: "Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)" – Mikael Bashir Aug 25 '23 at 06:44
  • Then you need to `brew install pkg-config`. – Botje Aug 25 '23 at 07:15
  • I forgot to mention that because it is almost always installed as a dependency of some other brew package. – Botje Aug 25 '23 at 07:54
  • now I get: "CMake Error at CMakeLists.txt:7 (target_link_libraries): Cannot specify link libraries for target "Turrial" which is not built by this project." I'm not sure what is meant to go in place of "Turrial", as I copy and pasted this from another question. – Mikael Bashir Aug 25 '23 at 08:05
  • The body of your question defines the "Turrial" target by calling `add_executable(Turrial ...)`. I just copied the target from there. If you changed the name of the target in the mean time, you should also change the argument to `target_link_libraries`. Also make sure that `target_link_libraries(...)` comes *after* the `add_executable` statement. – Botje Aug 25 '23 at 08:07
  • after adding: "add_executable(Turrial main.cpp)", I get an error saying "Undefined symbols for architecture x86_64: ___gmpz_init, referenced from: _main in main.cpp.o", and "building for macOS-x86_64 but attempting to link with file built for macOS-arm64" (and other things). This is when I try to build my C++ programme, not when loading CMake changes. – Mikael Bashir Aug 25 '23 at 08:15
  • Then you're building for x86, not apple silicon. Time to figure out why your compiler is trying to build for an architecture not native to your computer. – Botje Aug 25 '23 at 08:19
  • I'll try everything again with CLion for silicon – Mikael Bashir Aug 25 '23 at 08:21
  • CLion is just an IDE. Presumably you can configure the target architecture in your project or run configuration. See the documentation for [configuring toolchains in CLion](https://www.jetbrains.com/help/clion/how-to-create-toolchain-in-clion.html) – Botje Aug 25 '23 at 08:21