Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

394
Views
In C, what constitutes an "undefined reference" to a symbol?

I was reading the GNU documentation on the GCC linker's --wrap option, which can be used to mock functions for testing.

https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_3.html says the following:

--wrap symbol

Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to __wrap_symbol. Any undefined reference to __real_symbol will be resolved to symbol. This can be used to provide a wrapper for a system function. The wrapper function should be called __wrap_symbol. If it wishes to call the system function, it should call __real_symbol.

I've noticed that it says Any undefined reference to symbol will be resolved to __wrap_symbol, but it doesn't say what exactly an undefined reference to symbol means. Does anyone know specifically what is considered undefined?

I'm confused because I created an example program that successfully mocks the read function (it's compiled with gcc test.c -o test -Wl,--wrap=read to enable mocking). How come the linker considers this symbol undefined? I included unistd.h, so shouldn't that make read defined? I thought the documentation said --wrap only works on undefined symbols?

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define DUMMY_STR "I don't care because this function is mocked."
#define DUMMY_LEN (strlen(DUMMY_STR))

ssize_t __wrap_read(int fd, void *buf, size_t count)
{
   sprintf(buf, DUMMY_STR);
   return DUMMY_LEN;
}

int main()
{
   char buf[128] = {'\0'};
   puts("Press anything, then ENTER, to continue");
   read(1, buf, sizeof(buf) - 1);
   puts(buf);
   return 0;
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Including the header file just declares the symbols, it does not define them -- they'll be undefined in your test.c compilation unit because it does not ever define them.

The read symbol is in fact defined in libc.so -- the C standard library -- but when linking your program it first sees your compilation unit (test.o) at which point read is still undefined. So the references to read are instead mapped to __wrap_read.

definitions and declarations are two different things. Related, but different.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!