Getting started with wpa_supplicant using C
<p>Before people jump to conclusion saying this is a duplicate post, I'd like to point out that I have gone through the other posts on this topic but really haven't found a solution. </p>

<hr>

<h2>What I need</h2>

<p>My goal is to access the wpa_supplicant from a C program to do the following:</p>

<ol>
<li>See active connections </li>
<li>Detect when interfaces goes down</li>
<li>Connect to AP/Setup an AP and so on</li>
</ol>

<h2>What I've found out</h2>

<ul>
<li>I don't need DBus if I need to write a C program to communicate with wpa_supplicant</li>
<li>I can make use of the functions in <code>wpa_ctrl.h</code> by just including these files in my project directory</li>
<li>Here are some links I found related to this question. <a href="https://stackoverflow.com/questions/8521661/d-bus-tutorial-in-c-to-communicate-with-wpa-supplicant?rq=1">1</a>, <a href="https://stackoverflow.com/questions/19822882/including-wpa-ctrl-h-in-a-c-code">2</a>, <a href="http://lists.shmoo.com/pipermail/hostap/2013-May/027832.html" rel="nofollow noreferrer">3</a></li>
<li>I've also gone through the <a href="http://w1.fi/wpa_supplicant/devel/hostapd_ctrl_iface_page.html" rel="nofollow noreferrer">official documentation</a> which talks about external programs using <code>wpa_ctrl.c</code> </li>
</ul>

<h2>Why the above does not actually solve the problem</h2>

<ul>
<li>Most of the posts I found on SO and other related websites regarding this issue point to resources like official documentation which is good <em>but does not solve the problem</em></li>
<li>In a lot of these posts, people have given up pursuing this or have worked out a solution but haven't posted it online.</li>
<li>For a novice in this topic, it'll be helpful if one can post a working example -- the 'hello world' of wpa_supplicant. </li>
</ul>

<h2>What I've done so far</h2>

<ul>
<li><p>From <a href="https://w1.fi/releases/" rel="nofollow noreferrer">this</a> link, I copied  <code>wpa_supplicant-2.5/src/common/wpa_ctrl.h</code>  into  <code>wpa_supplicant-2.5/src/utils</code> directory (since <code>common.h</code> had many dependencies). I then wrote a simple C program <code>hostapd_cli.c</code> in the same directory which is shown below. <strong>I get an undefined reference to 'wpa_ctrl_open' error</strong></p>

<pre><code>#include "includes.h"
#include &lt;dirent.h&gt;
#include "wpa_ctrl.h"
#include "common.h"
static struct wpa_ctrl *ctrl_conn;
static int hostapd_cli_quit = 0;
static int hostapd_cli_attached = 0;
static const char *ctrl_iface_dir = "/var/run/wpa_supplicant";
static char *ctrl_ifname = NULL;
static int ping_interval = 5;

int main()
{
    ctrl_conn = wpa_ctrl_open(ctrl_iface_dir);
    if (!ctrl_conn){
        printf("Could not get ctrl interface!\n");
        return -1;
    }
    return 0;
}
</code></pre></li>
</ul>

<hr>

<h2>Makefile</h2>

<pre class="lang-make prettyprint-override"><code>C=gcc
CFLAGS=-lpthread
DEPS = includes.h wpa_ctrl.h common.h
OBJ = wpa_ctrl.o hostapd_cli.o

%.o: %.c $(DEPS)
$(CC) -c -o $@ $&lt; $(CFLAGS)

main: $(OBJ)
gcc -o $@ $^ $(CFLAGS)

.PHONY: clean

clean:
rm -f *.o *~ core $(INCDIR)/*~ 
</code></pre>

<h2>Build log</h2>

<pre class="lang-none prettyprint-override"><code> gcc -o main wpa_ctrl.o hostapd_cli.o -lpthread
 hostapd_cli.o: In function `main':
 hostapd_cli.c:(.text+0xf): undefined reference to `wpa_ctrl_open'
 collect2: error: ld returned 1 exit status
 Makefile:10: recipe for target 'main' failed
 make: *** [main] Error 1
</code></pre>

<hr>

<p>There's not much material on how to use these files and integrate it to an external project or how to compile it and I'm kinda clueless. Any help on how to proceed will be really appreciated. </p>

<p>Edit 1: Corrected typo and added build log </p>