Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MPIR:x64-linux] build failure (static linking) #13827

Closed
amakhno opened this issue Sep 30, 2020 · 12 comments · Fixed by #25209
Closed

[MPIR:x64-linux] build failure (static linking) #13827

amakhno opened this issue Sep 30, 2020 · 12 comments · Fixed by #25209
Assignees
Labels
depends:upstream-changes Waiting on a change to the upstream project

Comments

@amakhno
Copy link

amakhno commented Sep 30, 2020

Host Environment

  • OS: Linux (Ubuntu 20)
  • Compiler: g++ 9.3

To Reproduce
Steps to reproduce the behavior:

./vcpkg install cgal eigen3
git clone https://github.com/amakhno/vcpkg-gmp-bug-example.git
cd vcpkg-gmp-bug-example
cmake . && make

Failure logs
vcpkg install works perfectly but I have an error during the project build (linking)

-- Using header-only CGAL
-- Targetting Unix Makefiles
-- Using /usr/bin/c++ compiler.
-- Boost include dirs: /home/noir/projects/vcpkg/installed/x64-linux/include
-- Boost libraries:    
-- Using gcc version 4 or later. Adding -frounding-math
CMake Warning at /home/noir/projects/vcpkg/installed/x64-linux/share/cgal/CGAL_enable_end_of_configuration_hook.cmake:99 (message):
  =======================================================================

  CGAL performance notice:

  The variable CMAKE_BUILD_TYPE is set to "".  For performance reasons, you
  should set CMAKE_BUILD_TYPE to "Release".

  Set CGAL_DO_NOT_WARN_ABOUT_CMAKE_BUILD_TYPE to TRUE if you want to disable
  this warning.

  =======================================================================
Call Stack (most recent call first):
  CMakeLists.txt:9999 (CGAL_run_at_the_end_of_configuration)


-- Configuring done
-- Generating done
-- Build files have been written to: /home/noir/projects/vcpkg-bug-example
[ 50%] Linking CXX shared library libexample.so
/usr/bin/ld: /home/noir/projects/vcpkg/installed/x64-linux/debug/lib/libgmp.a(modexact_1c_odd.o): relocation R_X86_64_PC32 against symbol `__gmp_modlimb_invert_table' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/example.dir/build.make:86: libexample.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/example.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

Additional context
There is no such a problem when you try to buld it with a dynamic library or a console application.
I've tried to reinstall vcpkg completely but it didn't help.
The same project works like a charm with :windows-x64-static on my Windows machine.

As I googled, it's likely that linux default triplet is x64-linux which is static, but I couln't find any reasons why it doesn't work

The problem is probably in MPIR but I'm not sure

@PhoebeHui PhoebeHui added the category:question This issue is a question label Oct 9, 2020
@PhoebeHui
Copy link
Contributor

@amakhno, thanks for reporting this issue!

The x64-linux triplet default is static. Relate to upstream issue wbhart/mpir#286
See https://stackoverflow.com/a/19768349 for more information for this issue. Add the '-fPIC' doesn't work,
the mpir already built with -fPIC.

Failure
/usr/bin/ld: /home/noir/projects/vcpkg/installed/x64-linux/debug/lib/libgmp.a(modexact_1c_odd.o): relocation R_X86_64_PC32 against symbol `__gmp_modlimb_invert_table' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value

@amakhno
Copy link
Author

amakhno commented Oct 14, 2020

@PhoebeHui ,
Thank you for your reply!

Do have any ideas on how to deal with this problem?
I tried to look inside the .a file and there are dozens of links but it still doesn't work.

If I change the MPIR portfile.cmake to: (add -fPIC)

string(APPEND VCPKG_CXX_FLAGS " -Wno-implicit-function-declaration -fPIC")

It changes nothing

So I also changed the dependencies of MPFR and CGAL to GMP but there is no real difference in the result

/bin/ld: /home/noir/projects/vcpkg/installed/x64-linux/debug/lib/libgmp.a(bdiv_q_1.o): relocation R_X86_64_PC32 against symbol `__gmp_binvert_limb_table' can not be used when making a shared object; recompile with -fPIC
/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/example.dir/build.make:86: libexample.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/example.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

@PhoebeHui PhoebeHui added depends:upstream-changes Waiting on a change to the upstream project and removed category:question This issue is a question labels Oct 19, 2020
@BillyONeal
Copy link
Member

@amakhno Could this be coming from the gmp port rather than the mpir port?

/home/noir/projects/vcpkg/installed/x64-linux/debug/lib/libgmp.a

I know mpir provides some gmp headers but I think it uses different lib names?

@amakhno
Copy link
Author

amakhno commented Oct 20, 2020

@BillyONeal

it uses different lib names

No, when you build mpir it also creates libgmp* libraries (for compatibility, probably)

I've found a stupid workaround for using the system GMP from libgmp3-dev. After installation of all dependencies I manually (bash script) remove all gmp/mpir files from vcpkg/installed directory.

With CMake files generation I have CGAL static references to VCPKG and a dynamic reference to GMP in PATH

There are also dozens of the same GMP bug reports (__gmp_binvert_limb_table) online, but the only workaround I've found is using dynamic linking. This means that I cannot build CGAL even with GMP using vcpkg easily.

@BillyONeal
Copy link
Member

BillyONeal commented Oct 21, 2020

hmmm interesting:

No, when you build mpir it also creates libgmp* libraries (for compatibility, probably)

billy@bion-ubuntu-18-04:~/vcpkg$ ./vcpkg list
mpir:x64-linux                                     3.0.0#10         Multiple Precision Integers and Rationals
billy@bion-ubuntu-18-04:~/vcpkg$ ls installed/x64-linux/lib
libgmp.a  libgmp.la  libgmpxx.a  libgmpxx.la  libmpir.a  libmpir.la  libmpirxx.a  libmpirxx.la
billy@bion-ubuntu-18-04:~/vcpkg$

Strange that they don't do that for Windows builds ¯\_(ツ)_/¯

PS C:\Dev\vcpkg> .\vcpkg.exe list
mpir:x86-windows                                   3.0.0#10         Multiple Precision Integers and Rationals
PS C:\Dev\vcpkg> dir .\installed\x86-windows\lib


    Directory: C:\Dev\vcpkg\installed\x86-windows\lib

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          10/19/2020  5:42 PM         127490 mpir.lib

PS C:\Dev\vcpkg>

As you filed a defect report upstream let's see what they do with it.

@reanimatedmanx
Copy link

Any info on when this will be resolved?

@BillyONeal
Copy link
Member

@one-thunder It's reported upstream as wbhart/mpir#286

@lrineau
Copy link
Contributor

lrineau commented Jun 8, 2022

  • With the triplet x64-linux, all the libraries are static in vcpkg.
  • cgal actually depends on gmp and not mpir.

I was able to reproduce your issue, with x64-linux, and then I found a fix with this patch:

diff --git a/ports/gmp/portfile.cmake b/ports/gmp/portfile.cmake
index b4acb3047..2138744fc 100644
--- a/ports/gmp/portfile.cmake
+++ b/ports/gmp/portfile.cmake
@@ -49,6 +49,7 @@ vcpkg_configure_make(
     OPTIONS
         ${OPTIONS}
         --enable-cxx
+        --with-pic^M
 )
 
 set(tool_names bases fac fib jacobitab psqr trialdivtab)
diff --git a/ports/gmp/vcpkg.json b/ports/gmp/vcpkg.json
index 8393feed1..12a6e0d43 100644
--- a/ports/gmp/vcpkg.json
+++ b/ports/gmp/vcpkg.json
@@ -1,7 +1,7 @@
 {
   "name": "gmp",
   "version": "6.2.1",
-  "port-version": 9,
+  "port-version": 10,
   "description": "The GNU Multiple Precision Arithmetic Library",
   "homepage": "https://gmplib.org",
   "supports": "!(windows & (arm | arm64))",

@PhoebeHui Do you think that patch would be acceptable?

@BillyONeal
Copy link
Member

I don't believe that is correct: switches like -fPIC should be coming from the toolchain

@BillyONeal
Copy link
Member

(Also, if it's anything related to gmp, it is not this bug.)

@lrineau
Copy link
Contributor

lrineau commented Jun 9, 2022

I don't believe that is correct: switches like -fPIC should be coming from the toolchain

You are right. And actually -fPIC is already set by the toolchain. That is -DPIC that was not. See my PR #25158.

@lrineau
Copy link
Contributor

lrineau commented Jun 13, 2022

I have tried to add -DPIC to the toolchain, but there was complications. See #25158 (comment).

I have created a new pull-request #25209 that adds --with-pic to the ./configure steps of GMP and MPIR.

I have verified that this issue #13827 is fixed, on Linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
depends:upstream-changes Waiting on a change to the upstream project
Projects
None yet
5 participants