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

FreeBSD ld: error: unable to find library -lpthreads #158

Open
Mellowchan opened this issue Apr 9, 2022 · 4 comments
Open

FreeBSD ld: error: unable to find library -lpthreads #158

Mellowchan opened this issue Apr 9, 2022 · 4 comments

Comments

@Mellowchan
Copy link

Hello,

i'm trying to port newest telegram libpurple to freebsd but i'm currently having problems with pthreads.h library.

the library exists on the system

find -d /usr/include | grep pthread.h
/usr/include/pthread.h

here is my CMakeError.log

Determining if the strtod_l exist failed with the following output:
Change Dir: /home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/local/bin/gmake -f Makefile cmTC_98e86/fast && /usr/local/bin/gmake  -f CMakeFiles/cmTC_98e86.dir/build.make CMakeFiles/cmTC_98e86.dir/build
gmake[1]: Entering directory '/usr/home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_98e86.dir/CheckSymbolExists.cxx.o
/usr/bin/c++   -std=gnu++11 -MD -MT CMakeFiles/cmTC_98e86.dir/CheckSymbolExists.cxx.o -MF CMakeFiles/cmTC_98e86.dir/CheckSymbolExists.cxx.o.d -o CMakeFiles/cmTC_98e86.dir/CheckSymbolExists.cxx.o -c /home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp/CheckSymbolExists.cxx
/home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp/CheckSymbolExists.cxx:8:19: error: use of undeclared identifier 'strtod_l'
  return ((int*)(&strtod_l))[argc];
                  ^
1 error generated.
gmake[1]: *** [CMakeFiles/cmTC_98e86.dir/build.make:79: CMakeFiles/cmTC_98e86.dir/CheckSymbolExists.cxx.o] Error 1
gmake[1]: Leaving directory '/usr/home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:127: cmTC_98e86/fast] Error 2


File /home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp/CheckSymbolExists.cxx:
/* */
#include <stdlib.h>

int main(int argc, char** argv)
{
  (void)argv;
#ifndef strtod_l
  return ((int*)(&strtod_l))[argc];
#else
  (void)argc;
  return 0;
#endif
}
Checking whether the ASM compiler is GNU using "--version" did not match "(GNU assembler)|(GCC)|(Free Software Foundation)":
FreeBSD clang version 11.0.1 (git@github.com:llvm/llvm-project.git llvmorg-11.0.1-0-g43ff75f2c3fe)
Target: x86_64-unknown-freebsd13.0
Thread model: posix
InstalledDir: /usr/bin
Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output:
Change Dir: /home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/local/bin/gmake -f Makefile cmTC_d02dc/fast && /usr/local/bin/gmake  -f CMakeFiles/cmTC_d02dc.dir/build.make CMakeFiles/cmTC_d02dc.dir/build
gmake[1]: Entering directory '/usr/home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_d02dc.dir/src.c.o
/usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD   -MD -MT CMakeFiles/cmTC_d02dc.dir/src.c.o -MF CMakeFiles/cmTC_d02dc.dir/src.c.o.d -o CMakeFiles/cmTC_d02dc.dir/src.c.o -c /home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_d02dc
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_d02dc.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/cmTC_d02dc.dir/src.c.o -o cmTC_d02dc 
ld: error: undefined symbol: pthread_create
>>> referenced by src.c
>>>               CMakeFiles/cmTC_d02dc.dir/src.c.o:(main)
cc: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[1]: *** [CMakeFiles/cmTC_d02dc.dir/build.make:100: cmTC_d02dc] Error 1
gmake[1]: Leaving directory '/usr/home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:127: cmTC_d02dc/fast] Error 2


Source file was:
#include <pthread.h>

static 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: /home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/local/bin/gmake -f Makefile cmTC_dbb4b/fast && /usr/local/bin/gmake  -f CMakeFiles/cmTC_dbb4b.dir/build.make CMakeFiles/cmTC_dbb4b.dir/build
gmake[1]: Entering directory '/usr/home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_dbb4b.dir/CheckFunctionExists.c.o
/usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create -MD -MT CMakeFiles/cmTC_dbb4b.dir/CheckFunctionExists.c.o -MF CMakeFiles/cmTC_dbb4b.dir/CheckFunctionExists.c.o.d -o CMakeFiles/cmTC_dbb4b.dir/CheckFunctionExists.c.o -c /usr/local/share/cmake/Modules/CheckFunctionExists.c
Linking C executable cmTC_dbb4b
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_dbb4b.dir/link.txt --verbose=1
/usr/bin/cc  -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_dbb4b.dir/CheckFunctionExists.c.o -o cmTC_dbb4b  -lpthreads 
ld: error: unable to find library -lpthreads
cc: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[1]: *** [CMakeFiles/cmTC_dbb4b.dir/build.make:100: cmTC_dbb4b] Error 1
gmake[1]: Leaving directory '/usr/home/tadmin/tdlib-purple/build/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:127: cmTC_dbb4b/fast] Error 2
@BenWiederhake
Copy link
Contributor

/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_dbb4b.dir/CheckFunctionExists.c.o -o cmTC_dbb4b -lpthreads

Huh, that's weird. The library is called pthread, and I found some discussion about how FreeBSD apparently wants you to invoke the compiler with -pthread instead of explicitly linking to -lpthread. I was hoping that cmake automagically takes care of things like these, but apparently not.

Wildly guess that these people happen to solve the same issue, I would hope that the following would fix the issue:

if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
    target_link_libraries (libhighs "pthread")
endif()

I don't have a FreeBSD installation available, and I can't easily test it. Also, I have no experience with any BSD, I'm sorry if you have to test around with this a bit.

I would assume that it goes into line 38, right before link_directories(${Purple_LIBRARY_DIRS}) (before it, not replacing it).

Let me know if that works. If it does, please make a PR! :)

@Mellowchan
Copy link
Author

Thank you for your quick answer.
I've tried adding those lines although libhighs is not the library we are compiling now.
I've tried to change it to tdlib-purple , telegram-tdlib and telegram-purple but sadly neither of those works for me.

It might be probably trivial but the current error with those lines is:

CMake Error at CMakeLists.txt:40 (target_link_libraries):
  Cannot specify link libraries for target "tdlib-purple" which is not
  built by this project.

@BenWiederhake
Copy link
Contributor

You're right, I forgot to replace libhighs with telegram-tdlib, sorry ^^

Hmm, then apparently cmake runs into this issue before even trying to link the plugin.

I'm not really sure what the "correct" solution is, or how cmake wants to be treated. As a workaround, I think the following might work, although it feels a bit brutish:

add_link_options("-pthread")

Again, you might have to try around a bit, I can't try this out on FreeBSD. The idea behind this is to inject a linker option, and thus make the test believe that pthread is available without even linking against any library. Your logs indicate that cmake considers this possible, and checks this first.

@Mellowchan
Copy link
Author

Mellowchan commented Apr 10, 2022

So update.
I've tried adding options / setting global variables from following theads
https://stackoverflow.com/questions/28029195/how-to-let-cmake-use-pthread-instead-of-lpthread
https://stackoverflow.com/questions/1620918/cmake-and-libpthread
https://stackoverflow.com/questions/54189656/what-is-the-proper-use-of-cmake-findthreads-with-modern-c

But no luck and the problem still persist.

If i understand correctly. On linux to use -lpthreads you need to install libboost and there should be probably default pthread.h too.
May I ask you please if you could try to compile this library (i assume you're on linux) with -pthreads ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants