How to use GMP with Cmake on CLion?

Answered

I did compile GMP in static and dynamic versions using this instruction: http://ethicorum.ru/?p=209&i=1&lang=ru. I got this files:

D:\Programs\ProgrammingEnvironment\Librares\gmp-6.1.1

├───bin
│ libgmp-10.dll
│ libgmpxx-4.dll

├───include
│ gmp.h
│ gmpxx.h

├───lib
│ libgmp.a
│ libgmp.dll.a
│ libgmp.la
│ libgmpxx.a
│ libgmpxx.dll.a
│ libgmpxx.la

└───share
└───info
dir
gmp.info
gmp.info-1
gmp.info-2

I try many times to use GMP but got "undefined reference", "cannot find -lgmp" errors. 

Please help me to understand how to build project. My latest attempt:

cmake_minimum_required(VERSION 3.13)
project(Test_CLion)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++17 -pedantic -Wall -Wextra -Wconversion")

set(STATIC_BUILD FALSE)
if (CMAKE_BUILD_TYPE MATCHES Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
set(STATIC_BUILD TRUE)
endif ()

if (STATIC_BUILD)
set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc")
endif ()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Executable/bin)
set(ENV_ROOT "D:/Programs/ProgrammingEnvironment")


add_executable(${PROJECT_NAME} main.cpp)

set(GMP_DIR "${ENV_ROOT}/Librares/gmp-6.1.1")
include_directories(${PROJECT_NAME} ${GMP_DIR}/include)
find_library(GMP_LIBRARY NAMES gmp libgmp PATHS "${GMP_DIR}")
target_link_libraries(${PROJECT_NAME} gmp libgmp)

And I got "cannot find -lgmp"

0
6 comments

Hi! 

Have you tried specifying the full path to the library? Like

target_link_libraries(${PROJECT_NAME} "D:/Programs/ProgrammingEnvironment/Librares/gmp-6.1.1/lib/libgmp.a")
1
Avatar
Permanently deleted user

Thanks! It work's! But I can use only dynamic linked GMP. How to make it static? I tryed this:

set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgmpxx")

But I got 

g++.exe: error: unrecognized command line option '-static-libgmpxx'; did you mean '-static-libmpx'?

And then

g++.exe: error: unrecognized command line option '-static-libmpx'

Full CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)
project(Test_CLion)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++17 -pedantic -Wall -Wextra -Wconversion")

set(STATIC_BUILD TRUE)
if (CMAKE_BUILD_TYPE MATCHES Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
set(STATIC_BUILD TRUE)
endif ()

if (STATIC_BUILD)
set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgmpxx")
endif ()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Executable/bin)
set(ENV_ROOT "D:/Programs/ProgrammingEnvironment")


add_executable(${PROJECT_NAME} main.cpp)

set(GMP_DIR "${ENV_ROOT}/Librares/gmp-6.1.1")
include_directories(${PROJECT_NAME} ${GMP_DIR}/include)
target_link_libraries(${PROJECT_NAME}
"${GMP_DIR}/lib/libgmp.dll.a"
"${GMP_DIR}/lib/libgmp.a"
"${GMP_DIR}/lib/libgmpxx.a"
"${GMP_DIR}/lib/libgmpxx.dll.a"
)
0

libgmp.a and libgmpxx.a are static libraries. If you remove -static-libgmpxx and do

target_link_libraries(${PROJECT_NAME}
"${GMP_DIR}/lib/libgmp.a"
"${GMP_DIR}/lib/libgmpxx.a"
)

does it work?

And using

set(CMAKE_EXE_LINKER_FLAGS "-static")

will include the static libraries in the executable.

0
Avatar
Permanently deleted user

No, It did't work's. I do this: 

cmake_minimum_required(VERSION 3.13)
project(Test_CLion)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++17 -pedantic -Wall -Wextra -Wconversion")

set(STATIC_BUILD TRUE)
if (CMAKE_BUILD_TYPE MATCHES Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
set(STATIC_BUILD TRUE)
endif ()

if (STATIC_BUILD)
set(CMAKE_EXE_LINKER_FLAGS "-static")
endif ()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Executable/bin)
set(ENV_ROOT "D:/Programs/ProgrammingEnvironment")


add_executable(${PROJECT_NAME} main.cpp)

set(GMP_DIR "${ENV_ROOT}/Librares/gmp-6.1.1")
include_directories(${PROJECT_NAME} ${GMP_DIR}/include)
target_link_libraries(${PROJECT_NAME}
"${GMP_DIR}/lib/libgmp.a"
"${GMP_DIR}/lib/libgmpxx.a"
)

And got this: 


CMakeFiles\Test_CLion.dir/objects.a(main.cpp.obj): In function `ZN17__gmp_binary_plus4evalEP12__mpz_structPKS0_S3_':
D:/Programs/ProgrammingEnvironment/Librares/gmp-6.1.1/include/gmpxx.h:185: undefined reference to `_imp____gmpz_add'
CMakeFiles\Test_CLion.dir/objects.a(main.cpp.obj): In function `ZN10__gmp_exprIA1_12__mpz_structS1_E9assign_siEl':
D:/Programs/ProgrammingEnvironment/Librares/gmp-6.1.1/include/gmpxx.h:1453: undefined reference to `_imp____gmpz_set_si'
CMakeFiles\Test_CLion.dir/objects.a(main.cpp.obj): In function `ZN10__gmp_exprIA1_12__mpz_structS1_EC1Ev':
D:/Programs/ProgrammingEnvironment/Librares/gmp-6.1.1/include/gmpxx.h:1488: undefined reference to `_imp____gmpz_init'
CMakeFiles\Test_CLion.dir/objects.a(main.cpp.obj): In function `ZN10__gmp_exprIA1_12__mpz_structS1_ED1Ev':
D:/Programs/ProgrammingEnvironment/Librares/gmp-6.1.1/include/gmpxx.h:1523: undefined reference to `_imp____gmpz_clear'
CMakeFiles\Test_CLion.dir/objects.a(main.cpp.obj): In function `ZN10__gmp_exprIA1_12__mpz_structS1_EaSEPKc':
D:/Programs/ProgrammingEnvironment/Librares/gmp-6.1.1/include/gmpxx.h:1542: undefined reference to `_imp____gmpz_set_str'
CMakeFiles\Test_CLion.dir/objects.a(main.cpp.obj): In function `ZlsIA1_12__mpz_structS1_ERSoS2_RK10__gmp_exprIT_T0_E':
D:/Programs/ProgrammingEnvironment/Librares/gmp-6.1.1/include/gmpxx.h:2033: undefined reference to `_imp___ZlsRSoPK12__mpz_struct'
CMakeFiles\Test_CLion.dir/objects.a(main.cpp.obj): In function `ZlsIA1_12__mpz_struct16__gmp_unary_exprI10__gmp_exprIS1_S1_E18__gmp_abs_functionEERSoS7_RKS3_IT_T0_E':
D:/Programs/ProgrammingEnvironment/Librares/gmp-6.1.1/include/gmpxx.h:2033: undefined reference to `_imp___ZlsRSoPK12__mpz_struct'
CMakeFiles\Test_CLion.dir/objects.a(main.cpp.obj): In function `ZrsIA1_12__mpz_structERSiS2_R10__gmp_exprIT_S4_E':
D:/Programs/ProgrammingEnvironment/Librares/gmp-6.1.1/include/gmpxx.h:2039: undefined reference to `_imp___ZrsRSiP12__mpz_struct'
CMakeFiles\Test_CLion.dir/objects.a(main.cpp.obj): In function `ZN10__gmp_exprIA1_12__mpz_structS1_EC1I16__gmp_unary_exprIS2_18__gmp_abs_functionEEERKS_IS1_T_E':
D:/Programs/ProgrammingEnvironment/Librares/gmp-6.1.1/include/gmpxx.h:1497: undefined reference to `_imp____gmpz_init'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [CMakeFiles\Test_CLion.dir\build.make:88: ../Executable/bin/Test_CLion.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:72: CMakeFiles/Test_CLion.dir/all] Error 2
mingw32-make.exe: *** [Makefile:83: all] Error 2
0

What toolchain do yo use in CLion? What toolchain did you use for building the GMP libraries?

0

Please sign in to leave a comment.