1

I'm trying to compile a client using hiredis in C on Mac OS X.

I've installed hiredis with:

brew install hiredis

But still get the error:

fatal error: 'hiredis.h' file not found

My hiredis.h is however in:

/usr/local/include/hiredis/hiredis.c

How do I tell the compiler this?

I'm compiling with:

gcc test.c -o test
Alfred Balle
  • 1,135
  • 4
  • 16
  • 32
  • I recommend you take some time to read [the GCC documentation](https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/), and in general search for options or flags to add non-standard directories to be searched for header files. – Some programmer dude Aug 22 '19 at 10:59
  • (a) Your question states that “hiredis.h” is in “/usr/local/include/hiredis/hiredis.c”, but the latter ends in “.c” whereas the former ends in “.h”. (b) Your question does not show the source code that generates the error message. If it says `#include `, then the solution may be to change it to `#include `. – Eric Postpischil Aug 22 '19 at 11:00
  • Thank you, seems to work `gcc redistest.c -o redistest -I /usr/local/include/hiredis/ -lhiredis` – Alfred Balle Aug 22 '19 at 11:01
  • @Someprogrammerdude: `/usr/local/include` is generally one of the standard directories searched for headers, and it is more commonly intended one use `#include ` to access headers organized by subdirectory than that one add subdirectory paths to the search list. – Eric Postpischil Aug 22 '19 at 11:02
  • @EricPostpischil I've been using quite a few Linux systems (and some SunOS systems way before that) where it wasn't. macOS might be different? – Some programmer dude Aug 22 '19 at 11:06
  • 1
    @Someprogrammerdude: Even so, one would want to add just one path, /usr/local/include, to the search list and use `#include ` in the source rather than add dozens of different subdirectories to the search path. – Eric Postpischil Aug 22 '19 at 11:10
  • @EricPostpischil That's true. – Some programmer dude Aug 22 '19 at 11:11

2 Answers2

1

In your question you said hiredis.h is in /usr/local/include/hiredis/hiredis.c, which doesn't really make any sense.

Assuming you meant that your hiredis.h is in /usr/local/include/hiredis. You can do like:

gcc test.c -I/usr/local/include/hiredis -o test

Read about -I in this SO post.

UPDATE:

As mentioned by @EricPostpischil in comments, its a better idea to just include like:

#include < hiredis/hiredis.h>

I am still not sure if /usr/local/include is in default include path. If it is, well no need to do anything, just compile like:

gcc test.c -o test

and if it isn't,

gcc test.c -I/usr/local/include -o test
Mihir Luthra
  • 6,059
  • 3
  • 14
  • 39
0

If you have installed hiredis with homebrew, you can see what's in the package like this:

brew ls --verbose hiredis
/usr/local/Cellar/hiredis/0.14.0/INSTALL_RECEIPT.json
/usr/local/Cellar/hiredis/0.14.0/CHANGELOG.md
/usr/local/Cellar/hiredis/0.14.0/.brew/hiredis.rb
...
...
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.dylib
/usr/local/Cellar/hiredis/0.14.0/lib/pkgconfig/hiredis.pc    <--- PKG-CONFIG
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.a
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.0.14.dylib
...
...

And, as you can see, it gives you a pkg-config file with all the settings in it that you need. So, you might as well install pkg-config and do it properly!

brew install pkg-config

Now, if you want to know the C compiler flags for hiredis, you do:

pkg-config --cflags hiredis
-D_FILE_OFFSET_BITS=64 -I/usr/local/Cellar/hiredis/0.14.0/include/hiredis

And if you want to know the linker settings, you do:

pkg-config --libs hiredis
-L/usr/local/Cellar/hiredis/0.14.0/lib -lhiredis

And so, your compile-link command becomes very simple and updates itself when you update the packages:

gcc-9 $(pkg-config --cflags --libs hiredis) -o program program.c
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432