title: undefined reference to `sysctlbyname': Cannot use sysctlbyname in C?
body: <p>I tried to compile a code using the function 'sysctlbyname' and it flashed a list of errors while compiling</p>
  
  <pre><code>/tmp/ccq6bQz6.o: In function `main':
  haha.c:(.text+0x43): undefined reference to `sysctlbyname'
  haha.c:(.text+0x7a): undefined reference to `sysctlbyname'
  haha.c:(.text+0xae): undefined reference to `sysctlbyname'
  haha.c:(.text+0xe5): undefined reference to `sysctlbyname'
  haha.c:(.text+0x120): undefined reference to `sysctlbyname'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I couldn't find out what caused it but it seems like it is something related to the <code>sys/sysctl.h</code> library  </p>
  
  <p>The code I was working with is given below:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/sysctl.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  
  int main()
  {
   char *p = NULL;
   size_t len;
   sysctlbyname("hw.model", NULL, &amp;len, NULL, 0);
   p = malloc(len);
   sysctlbyname("hw.model", p, &amp;len, NULL, 0);
   printf("%s\n", p);
   /* CTL_MACHDEP variables are architecture dependent so doesn't work
   for every one */
   sysctlbyname("machdep.cpu.brand_string", NULL, &amp;len, NULL, 0);
   p = malloc(len);
   sysctlbyname("machdep.cpu.brand_string", p, &amp;len, NULL, 0);
   printf("%s\n", p);
   int64_t mem;
   len = sizeof(mem);
   sysctlbyname("hw.memsize", &amp;mem, &amp;len, NULL, 0);
   printf("System Memory : %lld\n", mem);
   return (0);
  }
  </code></pre>
  
  <p>and the command used to compile was <code>gcc haha.c -o haha</code><br>
  What is causing the problem and how do I solve it?</p>