1

I have the redis server installed, and can use it from the command line. Now, I am wanting to write a client program using hiredis. To begin with, I tried to compile example.c which is present in the hiredis directory:

vishal@expmach:~/redis-2.6.14/deps/hiredis$ ls

adapters  async.h       COPYING  dict.h        *example.c*        example-libevent.c      
hiredis.c  Makefile  net.h      sds.c  test.c async.c   CHANGELOG.md  dict.c   example-
ae.c  example-libev.c  fmacros.h           hiredis.h  net.c     README.md  sds.h

Here are the commands:

vishal@expmach:~/redis-2.6.14/deps/hiredis$ gcc -c -I hiredis example.c
vishal@expmach:~/redis-2.6.14/deps/hiredis$ gcc -o example -I hiredis -L hiredis -lhiredis -lm

/usr/bin/ld: cannot find -lhiredis collect2: ld returned 1 exit status

I am not sure how to go about fixing this. Please help.

user1274878
  • 1,275
  • 4
  • 25
  • 56
  • Try this : `sudo echo "/usr/local/lib" > sudo /etc/ld.so.conf.d/local.conf` & `sudo ldconfig`. Then compile the example.c like this : `gcc example.c -o example -l hiredis -I /usr/local/include/hiredis/` – y_159 Nov 10 '20 at 16:58

2 Answers2

1
gcc -o example example.c -lhiredis $(pkg-config --cflags --libs glib-2.0)
4b0
  • 21,981
  • 30
  • 95
  • 142
0

why don't you juste juste the provided Makefile?

make

./hiredis-example

Community
  • 1
  • 1
user1151446
  • 1,845
  • 3
  • 15
  • 22
  • Thanks! My plan is to use hiredis client library in some existing code. So I need to know how to compile it using gcc. – user1274878 Sep 13 '13 at 14:47
  • I am still learning how to understand Makefiles. Can you please tell me how I could go about using gcc to begin with. – user1274878 Sep 13 '13 at 15:16
  • you need to build the lib first and then gcc -o hiredis-example example.o libhiredis.a will work. If you type make, you will see all the commands issued to build the example – user1151446 Sep 13 '13 at 15:26
  • This works if I have example.c and libhiredis.a in the same directory. But when I have example.c in a different directory, then I get and error saying "gcc: libhiredis.a: No such file or directory" – user1274878 Sep 13 '13 at 16:53
  • just look at any gcc tutorial, and you will get it very quickly. Good luck! – user1151446 Sep 16 '13 at 07:04