undefined reference to C/C++ Origin: Application & API Last updated 14 Oct 2026

C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status

The linker can’t find the definition of a function or variable that was declared and used.

Exact message you may see undefined reference told returned 1 exit statuscollect2: error: ld returned 1 exit statusLNK2019 unresolved external symbol
Seen on: C/C++

Meaning

The code compiled, but the object file or library containing the implementation wasn’t linked: a missing .cpp in the build, a missing -l library (or wrong order), C vs C++ name mangling, or a template defined only in a .cpp.

Common causes

  • Source file defining the function not compiled/linked
  • Library not linked or listed before the objects (-lfoo order)
  • C function used from C++ without extern "C"
  • Declaration and definition signatures differ

⚡ Quick fix

  1. Add the .cpp/.c to the build (CMake target_sources)
  2. Link the library after your objects (-lm, -lpthread, target_link_libraries)
  3. Wrap C headers in extern "C"

Detailed fix by platform

CMake

  1. cpp
    add_executable(app main.cpp utils.cpp)
    target_link_libraries(app PRIVATE m pthread)

How to diagnose

  1. Symbol — Which name? (c++filt to demangle)
  2. Build — All sources compiled?
  3. Libraries — Linked and order

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

built for iOS, linking for simulator — Building for iOS Simulator, but linking in object file built for iOS, for architecture arm64 (iOS)built for iOS, linkin…COPY failed — COPY failed: file not found in build context / "/app/x": not found (Docker)COPY failedCould not find a package configuration file — CMake Error: Could not find a package configuration file provided by "Foo" (requested version 1.2) (C/C++)Could not find a pack…did not complete successfully — failed to solve: process "/bin/sh -c ..." did not complete successfully: exit code: 1 (Docker)did not complete succ…double free or corruption — C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped) (C/C++)double free or corrup…ELIFECYCLE — npm ERR! code ELIFECYCLE / Command failed with exit code 1 (npm)ELIFECYCLEERR_OSSL_EVP_UNSUPPORTED — Error: error:0308010C:digital envelope routines::unsupported (Node.js)ERR_OSSL_EVP_UNSUPPOR…Failed building wheel — error: subprocess-exited-with-error / Failed building wheel for x (Python)Failed building wheelundefined…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.