1

I got following error

'libwebsockets.h' file not found

but I have installed libwebsockets with the command

brew install libwebsockets

How can I solve this error? I want to implement a websocketserver and i need this. If my code is only has this following line

#include <libwebsockets.h>

it gave me an error. I tried to compile it with gcc foo.c

1 Answers1

4

You need to pass the appropriate compile and link flags to the compiler for it to find the headers and libraries.

This is usually done with the program "pkg-config". To get the compile flags, run:

pkg-config libwebsockets --cflags

To get the link flags:

pkg-config libwebsockets --libs

If you compile and link in the same step, you need to pass the flags that are output by both of the above commands to the compiler. If you have separate compilation and link commands, you pass the "--cflags" output during compiling, and the "--libs" output during linking.

In your case, you can compile with:

gcc $(pkg-config libwebsockets --cflags) foo.c $(pkg-config libwebsockets --libs)

The $() syntax takes the output of the command you put between ( and ) and puts it into yours, as if you had typed it in.

You should probably write a small build script to do that for you. For example in a file named "build" in the same directory as "foo.c":

#! /bin/sh
gcc $(pkg-config libwebsockets --cflags) foo.c $(pkg-config libwebsockets --libs)

Make the script executable:

chmod +x build

And then just call it:

./build
Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • @Nicos .When I run the first statement I get Package libwebsockets was not found in the pkg-config search path. Perhaps you should add the directory containing `libwebsockets.pc' to the PKG_CONFIG_PATH environment variable No package 'libwebsockets' found. On the second statement i get this: Unknown option --lflags – Okan Albayrak Mar 21 '16 at 13:10
  • @OkanAlbayrak Try to find where the .pc file is located. In your homebrew install root, search for "*websockets*.pc". For example `find /path/to/homebrew/root -name "*websockets*.pc"`. Did it find anything? – Nikos C. Mar 21 '16 at 13:39
  • Ok, I had found it – Okan Albayrak Mar 21 '16 at 13:54
  • I was in the file libwebsockets.pk and ran the first statement. It gave me no Error. But when I had to try to compile my c code it gave me an error – Okan Albayrak Mar 21 '16 at 14:07
  • @OkanAlbayrak Oops, sorry. It's not `--lflags`, it's `--libs`. I'm updating my answer. – Nikos C. Mar 21 '16 at 14:14
  • it was on the path /usr/local/Cellar/libwebsockets/1.4/lib/pkgconfig/libwebsockets.pc, where I had ran these 2 commands. But when i ran the c code with #include . it doenst find this header – Okan Albayrak Mar 21 '16 at 14:23
  • @OkanAlbayrak What is the output of pkg-config? Did you pass the flags to gcc? – Nikos C. Mar 21 '16 at 14:51
  • @Nicos For **pkg-config libwebsockets.pc --cflags** I got **-I/usr/local/Cellar/libwebsockets/1.4/include** and for **pkg-config libwebsockets.pc --libs** i got **-L/usr/local/Cellar/libwebsockets/1.4/lib -lwebsockets** – Okan Albayrak Mar 21 '16 at 14:58
  • @Nicos How I can pass the flags to gcc? – Okan Albayrak Mar 21 '16 at 15:01
  • @OkanAlbayrak I wrote that in my answer, using the `$()` syntax to run pkg-config and pass the output to gcc as flags. – Nikos C. Mar 21 '16 at 15:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106932/discussion-between-okan-albayrak-and-nikos-c). – Okan Albayrak Mar 21 '16 at 17:24