Skip to content

CMake error on macOS Monterey, library not found for -lpthread #3901

@JohanEngelen

Description

@JohanEngelen

The first time running cmake on macOS Monterey, I see this error:

CMake Error at cmake/Modules/ExtractDMDSystemLinker.cmake:42 (message):
  Failed to link empty D program using
  '/Users/johan/dcompilers/ldc-1.27.1-johan/bin/ldmd2 -wi -link-debuglib':

  ld: library not found for -lpthread

  clang: error: linker command failed with exit code 1 (use -v to see
  invocation)

  Error: /Library/Developer/CommandLineTools/usr/bin/cc failed with status: 1
Call Stack (most recent call first):
  CMakeLists.txt:670 (include)

Simply rerunning cmake works and creates a fine build configuration for LDC (ninja works fine afterwards).

I've traced the problem down to the following:
There are two cc executables on macOS: /usr/bin/cc and /Library/Developer/CommandLineTools/usr/bin/cc. The first one, /usr/bin/cc is usually used by LDC, and correctly finds libpthread, so LDC can link executables without problem. The second one, /Library/Developer/CommandLineTools/usr/bin/cc, is not able to find libpthread and therefore fails linking when invoked by LDC.
In the execute_process enviroment of CMake, the CC environment variable is set to /Library/Developer/CommandLineTools/usr/bin/cc; this is the C compiler detected by CMake (CMake outputs: -- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped). LDC uses the CC variable to find the C compiler, thus the linking problem occurs.
I wonder if this is a problem in CMake, that should detect /usr/bin/cc instead of the other one.
I'm using CMake 3.22.1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions