Skip to content

Build failure due to missing pthread library reference for unit_tests #3

Open
@apthorpe

Description

@apthorpe

Thank you for releasing this project; it has helped me a lot in getting CMake set up for a new C++ project.

I ran into a problem where the target unit_tests fails to build which I tracked down to a missing reference to the pthread library. The following shows my system information (OS, compiler, and CMake version), the steps to reproduce the error, and a diff showing the one line fix.

I submitted a pull request (pr #2) for this change if you want to incorporate it into the main branch. I tested the fix and it seems to resolve the problem and also resolves Issue #1 (closed for lack of info & followup).

Thanks again,

-- Bob

Version Info

I cloned the current minimal_cmake_example repo for this test. My OS, CMake, and compiler versions are shown below

$ cat /etc/lsb-release
DISTRIB_ID=LinuxMint
DISTRIB_RELEASE=19
DISTRIB_CODENAME=tara
DISTRIB_DESCRIPTION="Linux Mint 19 Tara"

$ cmake --version
cmake version 3.10.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ gcc --version
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Minimum Error Demonstration

I created a _build directory next to the cloned source directory and built the project to trigger the error:

$ mkdir _build && cd _build && cmake ../minimal_cmake_example && make
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.65.1
-- Found the following Boost libraries:
--   filesystem
--   system
-- Found GTest: /usr/local/lib/libgtest.a
-- Configuring done
-- Generating done
-- Build files have been written to: /home/apthorpe/Documents/Projects/minimal_cmake_example/_build
Scanning dependencies of target example
[ 20%] Building CXX object CMakeFiles/example.dir/src/main/main.cpp.o
[ 40%] Linking CXX executable example
[ 40%] Built target example
Scanning dependencies of target unit_tests
[ 60%] Building CXX object CMakeFiles/unit_tests.dir/src/test/test.cpp.o
[ 80%] Building CXX object CMakeFiles/unit_tests.dir/src/main/main.cpp.o
[100%] Linking CXX executable unit_tests
/usr/bin/ld: /usr/local/lib/libgtest.a(gtest-all.cc.o): undefined reference to symbol 'pthread_key_delete@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/unit_tests.dir/build.make:124: recipe for target 'unit_tests' failed
make[2]: *** [unit_tests] Error 1

Fix

Add -lpthread to end of target_link_libraries list in GTest section of CMakeList.txt:

$ diff -u CMakeLists.broken.txt CMakeLists.txt 
--- CMakeLists.broken.txt       2019-01-17 09:16:10.509836205 -0600
+++ CMakeLists.txt      2019-01-17 09:16:17.441894497 -0600
@@ -70,6 +70,7 @@
   target_link_libraries(unit_tests PUBLIC
     ${GTEST_BOTH_LIBRARIES}
     example
+    -lpthread
   )
 
   target_include_directories(unit_tests PUBLIC

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions