/usr/bin/ld: attempted static link of dynamic object `/usr/lib64/libm.so'

14,007

/usr/lib64/libm.so is a dynamic library. Since you link explicitly with it, -static doesn't force using the static version (libm.a) You are trying to compile a C++ program so you should use g++. Passing the libstdc++ and libm libraries is not needed then. Also /usr/lib64 should be in your standard link path so is not needed.

So you should use:

g++ \
    -static \
    -g myCode.C \
    -O \
    -o myCode \
    -I. \
    -L. \
    -Wreturn-type \
    -Wswitch \
    -Wcomment \
    -Wformat \
    -Wchar-subscripts \
    -Wparentheses \
    -Wpointer-arith \
    -Wcast-qual \
    -Woverloaded-virtual \
    -Wno-write-strings \
    -Wno-deprecated
Share:
14,007
user3544092
Author by

user3544092

Updated on June 04, 2022

Comments

  • user3544092
    user3544092 almost 2 years

    I'm not experienced at building with gcc at all and now require some help. I've a code that is being built with the following options

    gcc \
        -g myCode.C \
        -O \
        -o myCode \
        -I. \
        -L. \
        -L/usr/lib64 \
        -lstdc++ \
        -Wreturn-type \
        -Wswitch \
        -Wcomment \
        -Wformat \
        -Wchar-subscripts \
        -Wparentheses \
        -Wpointer-arith \
        -Wcast-qual \
        -Woverloaded-virtual \
        -Wno-write-strings /usr/lib64/libm.so \
        -Wno-deprecated
    

    When compiling myCode.C on redhat 6 machine it is not working on older versions of the OS throwing errors

    /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.9' not found
    /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found
    

    To fix this issue, I've tried to add -static build option to make all dynamic linking libraries as static, but have some build error which i dont understand :(

    /usr/bin/ld: attempted static link of dynamic object `/usr/lib64/libm.so'
    collect2: ld returned 1 exit status
    

    How do I make my code to work on older version of redhat rather than only on 6 and newer ?? what build options should I add/remove ?

    • ymonad
      ymonad over 7 years
      remove /usr/lib64/libm.so from -Wno-write-strings /usr/lib64/libm.so and add -lm instead if necessary.
    • user3544092
      user3544092 over 7 years
      @ymonad /usr/bin/ld: cannot find -lm
  • user3544092
    user3544092 over 7 years
    compiled without errors but still have the same issue while running on older OS
  • Arnout
    Arnout over 7 years
    Of course, I forgot to add the -static to the command... Edited.