Handeling pthread Macro Error at Build Time?

26 views Asked by At

I have been encountering same error for a large period of time now. C++ example below is simplified, as my first encounter with this error was while I was building a newly downloaded library. I don't know how to approach the issue. I have redownloaded g++, gcc, essential build and libc6.

Here a basic C++ code to demonstrate error:

#include <iostream>
#include <pthread.h>

int main() {
    pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;

    pthread_mutex_destroy(&m);

    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Following is CMakeLists.txt file:

project(clion_pthread)

set(CMAKE_CXX_STANDARD 14)

# Find the Threads package (this finds pthreads on UNIX)
find_package(Threads REQUIRED)

# Add your executable
add_executable(clion_pthread main.cpp)

# Link the executable to the Threads library
target_link_libraries(clion_pthread Threads::Threads)

Error

clion_pthread/cmake-build-debug --target clion_pthread -- -j 6
[ 50%] Building CXX object CMakeFiles/clion_pthread.dir/main.cpp.o
In file included from /usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:35,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/gthr.h:148,
                 from /usr/include/c++/9/ext/atomicity.h:35,
                 from /usr/include/c++/9/bits/ios_base.h:39,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from /clion_pthread/main.cpp:1:
clion_pthread/main.cpp: In function ‘int main()’:
clion_pthread/main.cpp:5:25: error: ‘__PTHREAD_SPINS’ was not declared in this scope
    5 |     pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~
make[3]: *** [CMakeFiles/clion_pthread.dir/build.make:76: CMakeFiles/clion_pthread.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/clion_pthread.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/clion_pthread.dir/rule] Error 2
make: *** [Makefile:124: clion_pthread] Error 2

OS

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:    20.04
Codename:   focal

0

There are 0 answers