Menu

Compilation/linking issue

Rodriguez
2020-08-23
2020-08-27
  • Rodriguez

    Rodriguez - 2020-08-23

    Hello,

    We've been trying to compile smina, and there seems to be a problem in the linking process.

    After compilation, when linking, it fails with an error that indicates that pthreads.h wasn't found.

    The CMakeFiles/CMakeError.log file in the build/ directory shows the following error:

    /cm/shared/apps/gcc/6.3.0/bin/cc  -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_d7504.dir/src.c.o -o cmTC_d7504 
    CMakeFiles/cmTC_d7504.dir/src.c.o: In function `main':
    src.c:(.text+0x2d): undefined reference to `pthread_create'
    src.c:(.text+0x39): undefined reference to `pthread_detach'
    src.c:(.text+0x45): undefined reference to `pthread_cancel'
    src.c:(.text+0x56): undefined reference to `pthread_join'
    src.c:(.text+0x6a): undefined reference to `pthread_atfork'
    collect2: error: ld returned 1 exit status
    gmake[1]: *** [cmTC_d7504] Error 1
    gmake[1]: Leaving directory `/root/smina/smina-code/build/CMakeFiles/CMakeTmp'
    gmake: *** [cmTC_d7504/fast] Error 2
    

    The cmake command being used is as follows:

    # cmake -DCMAKE_INSTALL_PREFIX=/cm/shared/apps/smina/c849cb -DBOOST_ROOT=/cm/shared/apps/boost/gcc/4.9.3/1.60.0 -DCMAKE_MODULE_PATH=/cm/shared/apps/eigen/3.3.7/cmake -DOPENBABEL_DIR=/cm/shared/apps/openbabel/3.1.1 -DCMAKE_THREAD_PREFER_PTHREAD=ON ..
    

    Any assistance with this issue is appreciated.
    Thank you.

     
  • David Koes

    David Koes - 2020-08-24

    Try adding target_link_libraries(smina Threads::Threads) to the end of CMakeLists.txt

     
  • Rodriguez

    Rodriguez - 2020-08-24

    We've tried adding the line, but the error persists.

    Here's the full CMakeError.log output:

    Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output:
    Change Dir: /root/smina/smina-code/build/CMakeFiles/CMakeTmp
    
    Run Build Command(s):/usr/bin/gmake cmTC_f9bc3/fast && /usr/bin/gmake  -f CMakeFiles/cmTC_f9bc3.dir/build.make CMakeFiles/cmTC_f9bc3.dir/build
    gmake[1]: Entering directory `/root/smina/smina-code/build/CMakeFiles/CMakeTmp'
    Building C object CMakeFiles/cmTC_f9bc3.dir/src.c.o
    /cm/shared/apps/gcc/6.3.0/bin/cc   -DCMAKE_HAVE_LIBC_PTHREAD -o CMakeFiles/cmTC_f9bc3.dir/src.c.o -c /root/smina/smina-code/build/CMakeFiles/CMakeTmp/src.c
    Linking C executable cmTC_f9bc3
    /cm/shared/apps/cmake/3.18.1/bin/cmake -E cmake_link_script CMakeFiles/cmTC_f9bc3.dir/link.txt --verbose=1
    /cm/shared/apps/gcc/6.3.0/bin/cc  -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_f9bc3.dir/src.c.o -o cmTC_f9bc3
    CMakeFiles/cmTC_f9bc3.dir/src.c.o: In function `main':
    src.c:(.text+0x2d): undefined reference to `pthread_create'
    src.c:(.text+0x39): undefined reference to `pthread_detach'
    src.c:(.text+0x45): undefined reference to `pthread_cancel'
    src.c:(.text+0x56): undefined reference to `pthread_join'
    src.c:(.text+0x6a): undefined reference to `pthread_atfork'
    collect2: error: ld returned 1 exit status
    gmake[1]: *** [cmTC_f9bc3] Error 1
    gmake[1]: Leaving directory `/root/smina/smina-code/build/CMakeFiles/CMakeTmp'
    gmake: *** [cmTC_f9bc3/fast] Error 2
    
    
    Source file was:
    #include 
    
    void* test_func(void* data)
    {
      return data;
    }
    
    int main(void)
    {
      pthread_t thread;
      pthread_create(&thread, NULL, test_func, NULL);
      pthread_detach(thread);
      pthread_cancel(thread);
      pthread_join(thread, NULL);
      pthread_atfork(NULL, NULL, NULL);
      pthread_exit(NULL);
    
      return 0;
    }
    
    Determining if the function pthread_create exists in the pthreads failed with the following output:
    Change Dir: /root/smina/smina-code/build/CMakeFiles/CMakeTmp
    
    Run Build Command(s):/usr/bin/gmake cmTC_d1cc0/fast && /usr/bin/gmake  -f CMakeFiles/cmTC_d1cc0.dir/build.make CMakeFiles/cmTC_d1cc0.dir/build
    gmake[1]: Entering directory `/root/smina/smina-code/build/CMakeFiles/CMakeTmp'
    Building C object CMakeFiles/cmTC_d1cc0.dir/CheckFunctionExists.c.o
    /cm/shared/apps/gcc/6.3.0/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTC_d1cc0.dir/CheckFunctionExists.c.o -c /cm/shared/apps/cmake/3.18.1/share/cmake-3.18/Modules/CheckFunctionExists.c
    Linking C executable cmTC_d1cc0
    /cm/shared/apps/cmake/3.18.1/bin/cmake -E cmake_link_script CMakeFiles/cmTC_d1cc0.dir/link.txt --verbose=1
    /cm/shared/apps/gcc/6.3.0/bin/cc  -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_d1cc0.dir/CheckFunctionExists.c.o -o cmTC_d1cc0  -lpthreads
    /usr/bin/ld: cannot find -lpthreads
    collect2: error: ld returned 1 exit status
    gmake[1]: *** [cmTC_d1cc0] Error 1
    gmake[1]: Leaving directory `/root/smina/smina-code/build/CMakeFiles/CMakeTmp'
    gmake: *** [cmTC_d1cc0/fast] Error 2
    
     
  • David Koes

    David Koes - 2020-08-24

    You need to figure out how to communicate to your version of cmake that -lpthread is needed at link time. What I sent works for cmake 3.12.4 running on ubuntu 20.04. You may want to try upgrading your cmake.

     
  • Rodriguez

    Rodriguez - 2020-08-24

    We've upgraded cmake to 3.18.1 and the issue persists. We've managed to compile it on Ubuntu as a test, but not on our working environment on CentOS.

    Any other ideas to link pthreads with cmake?

    Thank you.

     
  • David Koes

    David Koes - 2020-08-24

    Try adding target_link_libraries(smina pthread)

     
  • Rodriguez

    Rodriguez - 2020-08-25

    We tried adding the line, but the problem persists.

    As a test, using the code that CMake uses to verify the presence of “pthreads”:

    #include 
    
    void* test_func(void* data)
    {
      return data;
    }
    
    int main(void)
    {
      pthread_t thread;
      pthread_create(&thread, NULL, test_func, NULL);
      pthread_detach(thread);
      pthread_cancel(thread);
      pthread_join(thread, NULL);
      pthread_atfork(NULL, NULL, NULL);
      pthread_exit(NULL);
    
      return 0;
    }
    

    and attempting to compile it, if it’s compiled with “-lpthreads”, as is generated by the smina CMake it outputs an error:

    # gcc -o obj.o src.c -lpthreads
    /usr/bin/ld: cannot find -lpthreads
    collect2: error: ld returned 1 exit status
    

    But it succeeds if "-lpthread” is used instead:

    # gcc -o obj.o src.c -lpthread
    # ls
    obj.o  src.c
    

    Perhaps the problem is that CMake is generating the library’s name incorrectly?

     
  • David Koes

    David Koes - 2020-08-26

    What is the link line that is causing the error (run make -DVERBOSE=1)? Could your pthread library possibly be in a non-standard location?

     
  • Rodriguez

    Rodriguez - 2020-08-27

    The solution was compiling a new version of Boost. It seems to be working well now.

    Thank you for your quick responses and support.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.