1

I'm trying to build https://github.com/iovisor/bpftrace.git on Ubuntu 18.04. I built and installed its dependency bcc by hand (the bpftrace repo build docs suggest that because the packaged version on ubuntu lacks required header files).

I found that cmake would complain for me as follows:

-- Looking for bcc_prog_load - not found

After some CMakeLists.txt edits to ensure suitable -I flags are passed, and looking in CMakeFiles/CMakeError.log, I see that the build fails like this:

/usr/bin/cc  -I/home/me/dev/bcc/src/cc -I/home/me/dev/bcc/src/cc/libbpf/src   -o CMakeFiles/cmTC_653d8.dir/CheckSymbolExists.c.o   -c /home/me/dev/bpftrace/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
Linking C executable cmTC_653d8
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_653d8.dir/link.txt --verbose=1
/usr/bin/cc      -rdynamic CMakeFiles/cmTC_653d8.dir/CheckSymbolExists.c.o  -o cmTC_653d8 -lbcc 
CMakeFiles/cmTC_653d8.dir/CheckSymbolExists.c.o: In function `main':
CheckSymbolExists.c:(.text+0x1b): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

If I run with --debug-trycompile and run the failing commands by hand, I see that passing the -pthread command line option to gcc fixes that error. From this answer I glean that something like the following is supposed to fix that:

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(my_app Threads::Threads)

However, the thing that is failing here is not some target I have added, but merely a check for the presence of a symbol:

check_symbol_exists(bcc_prog_load "libbpf.h" HAVE_BCC_PROG_LOAD)

I therefore don't know how to specify a target for target_link_libraries. I tried replacing the target_link_libraries with link_libraries (dropping the first argument my_app), but that does not cause CMake to pass the -pthread argument to gcc when testing for the presence of the symbol.

What can I do to make the check_symbol_exists work?

Croad Langshan
  • 2,646
  • 3
  • 24
  • 37
  • You may set variable `CMAKE_REQUIRED_LIBRARIES` with additional libraries for link when use `check_symbol_exists`. See more in the documentation: https://cmake.org/cmake/help/latest/module/CheckSymbolExists.html. – Tsyvarev Mar 31 '19 at 22:32
  • @Tsyvarev if I add pthread to that list, it does get past this, thanks! Now I immediately hit another problem that it's not passing `-L` with the appropriate directory :-( and nothing I try makes it pass that (not `link_directories("/absolute/path/to/lib")`, nor `set(CMAKE_REQUIRED_LINK_OPTIONS "-L/absolute/path/to/lib")`) – Croad Langshan Mar 31 '19 at 23:11
  • Why do you need `-L` option for `check_symbol_exists`? Or do you have `pthreads` installed locally? – Tsyvarev Apr 01 '19 at 05:42
  • That -L is for the library whose symbol I'm checking exists (bcc), which yes, is installed locally @Tsyvarev – Croad Langshan Apr 01 '19 at 07:35
  • Hm, passing `-L` option via `CMAKE_REQUIRED_LINK_OPTIONS` variable should work. As last resort, in the `CMAKE_REQUIRED_LIBRARIES` variable you may use **absolute path** to your library instead of library's name. – Tsyvarev Apr 01 '19 at 15:13

0 Answers0