Compile hiredis in C on Mac OS X

核能气质少年 提交于 2021-01-28 19:55:44

问题


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

回答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



回答2:


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


来源:https://stackoverflow.com/questions/57607796/compile-hiredis-in-c-on-mac-os-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!