C & Mac: trouble finding a header using pkg-config - c

I'm trying to use in one of my projects. I'm working on a Mac, have gotten MacPorts with pkg-config and glib-2.0 packages.
When I try to make a file containing an include to above path, I get the following error (line above it is for clarity that it does actually give me the right dirs):
$ pkg-config --cflags --libs glib-2.0
-I/opt/local/lib/glib-2.0/include -I/opt/local/include -L/opt/local/lib -lglib-2.0 -lintl
$ make
gcc hash-glib.c -c `pkg-config --cflags --libs glib-2.0` -std=c99 -Wall -Wextra -pedantic -O2
hash-glib.c:2:23: error: glib/glib.h: No such file or directory
$

Presumably, you've run:
ls -l /opt/local/lib/glib-2.0/include/glib/glib.h \
/opt/local/include/glib/glib.h
to demonstrate that the header #include "glib/glib.h" actually is present in one of the locations where you've been told by pkg-config that it could be found. If it isn't there, then pkg-config is misleading you, and the compiler is telling you that you've been hoodwinked.
Since the compiler will have done its utmost to find the header, it is a reasonable bet that the file isn't in either of those locations. You are then left with detective work: where is the glib.h header installed?
find /opt/local -type f -name glib.h
If that tells you where it is, you can then work out what pkg-config should be saying. If that fails to find it, widen the search area. If you still can't find it, maybe it isn't installed yet? Or you only installed the glib runtime, not the development package.

Related

Compile hiredis in C on Mac OS X

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

Coding a simple GTK GUI on windows in C with eclipse

My problem seems quite simple: I would like to be able to create a simple program with a simple GUI for windows in C (and if possible on windows).
My current environment is eclipse IDE, msys2 with mingw64 toolchain all on windows 10.
I would like to use pkg-config with eclipse to avoid "hard-coding" all my libraries, and for that I first tried the pkg-config plugin for eclipse (it crashes when I create a new project, null pointer exception). I then tried to insert the pkg-config command inside the call to GCC. For that I had to use powershell because cmd do not allow it.
But even on powershell, i'm not able to use it because the string that pkg-config returns me is considered by gcc as a command.
Here is what eclipse uses to compile:
powershell gcc -O0 -g3 -Wall -c -fmessage-length=0 --std=c99 $(pkg-config-win --cflags --libs gtk+-3.0) -o main.o "..\\main.c"
And here is what it returns me:
gcc.exe: error: unrecognized command line option '-mms-bitfields -pthread -mms-bitfields -IC:/msys64/mingw64/include/gtk-3.0 [...]'
I then tried to remove the --cflags of pkg-config:
Command used by eclipse:
powershell gcc $(pkg-config-win --libs gtk+-3.0) -o Test.exe main.o -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -lwinmm -ldwmapi -lz -lpangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
And here is the result (8 times the same line).
gcc.exe: error: .0: No such file or directory
I tried dozens of different things but none of them works.
So my main questions are:
Is there a simple way to solve this problem?
Do I do the right things (is eclipse made to be able to do such things?)
If not, what should I do? (Change IDE? Change GUI library, ...)
I hope that you will be able to help me, I am very open about the possibilities that you might give. Please excuse me if I was unclear on some points or do not give enough informations, I will reply as soon as possible to your questions :p
So my main questions are:
1.Is there a simple way to solve this problem?
Yes,
While this is an old question, it deserves an answer. The problem as correctly noted in the comments is the output of $(pkg-config-win --cflags --libs gtk+-3.0) is returned as one long string and gcc doesn't recognize it. To solve the problem you can simply assign the output of pkg-config as an array and use the array variable with gcc, e.g.
PS> [string[]] $cflags = $(pkg-config-win --cflags --libs gtk+-3.0)
PS> gcc -O0 -g3 -Wall -c -fmessage-length=0 --std=c99 $cflags -o main.o ..\main.c
gcc will then see each option contained in $cflags as an individual argument (an quoting is automatically handled so embedded '-' and '.' do not cause problems in powershell)

C - Build options for SDLGFX

I'm not sure if the correct term is "build options". Here's the code that doesn't work :
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
polygonRGBA(renderer, x, y, 6, 255, 0, 0, 155); // this part
I've used this to compile it :
gcc -o test main.c `sdl2-config --cflags --libs̀
polygonRGBA() is a function from SDLGFX. It seems I've not added the correct options to build it (except this part, SDL2's compilation works). The given error is :
undefined reference to 'polygonRGBA'
What is the correct option to add ?
Generally, you can use pkg-config --list-all and grep for the name of the package you want to add compile or link flags for. But I was a little surprised to see that there is no pkg-config option for SDLGFX even though I have the -dev package installed on my (Ubuntu) system. Nor does sdl-config include the libraries for it (not that it necessarily should).
So try adding -lSDL2_gfx to your invocation,
gcc -o test main.c `sdl2-config --cflags --libs` -lSDL2_gfx
When pkg-config fails, you can make a good guess by searching for the library by name,
$ find /usr/lib | grep gfx
....
/usr/lib/x86_64-linux-gnu/libSDL2_gfx.so
...
and using -l with the part between lib and .so: libSDL2_gfx.so

glib.h and gtk.h not found

Hi everyone I have a program with the following includes:
gtk/gtk.h
glib.h
I have used the commands:
sudo apt-get install libgtk2.0-dev glib
sudo apt-get install glade
But I am still getting the error that glib was not found and gtk/gtk.h was not found. It's the first time I am using gtk and I have no idea how it works or how to install it.
The command you're supposed to use (in more recent releases of linux/gtk) is pkg-config, not gtk-config. gtk-config is intended for pre 2.0 gtk development.
Consider the file you're compiling is called foo.c, to compile it under gtk-2.0, you would use, from the command line the command:
gcc `pkg-config --cflags glib-2.0 gtk+-2.0` foo.c -o foo `pkg-config --libs glib-2.0 gtk+-2.0`
This should compile, and give you a file foo, that can be executed.
but really, use a makefile, as this stuff is a pain to keep typing. I would write out a sample makefile, but there are rules that need to be followed in the formatting of them that makes it difficult to type in the editor window.
# Sample Makefile
CFLAGS := $(shell pkg-config --cflags glib-2.0 gtk+-2.0)
LDFLAGS := $(shell pkg-config --libs glib-2.0 gtk+-2.0)
foo: foo.c
<TAB HERE NOT SPACES>$(CC) $(CFLAGS) $< -o $# $(LDFLAGS)
This defines a simple rule saying to make foo, it depends on foo.c, so of foo.c is newer than foo, it will be rebuilt. Where I write 'TAB HERE NOT SPACES' it must be a tab character, and cannot be a set of space characters.
type "locate glib.h" to determine file's location (assuming a contemporary linux distribution - your post doesn't provide much information).
Then ensure the path to glib.h is properly specified in your Makefile. (You do have a Makefile, don't you?) Perform the same steps for gtk.h.
Please read the official documentation. It explains how to compile GTK applications.
Basically to compile a hello.c file to generate a hello program, you'll type:
gcc `pkg-config --cflags --libs gtk+-2.0` hello.c -o hello

Why can't I build a "hello world" for glib?

So here's the world's simplest glib program:
#include <glib.h>
I try to compile it with gcc test.c and I get:
test.c:1:18: error: glib.h: No such file or directory
So I make sure that I have the right packages:
# dpkg -l | grep libglib
ii libglib-perl 1:1.183-1 Perl interface to the GLib and GObject libra
ii libglib1.2-dev 1.2.10-19build1 The GLib library of C routines (development)
ii libglib1.2ldbl 1.2.10-19build1 The GLib library of C routines
ii libglib2.0-0 2.20.1-0ubuntu2 The GLib library of C routines
ii libglib2.0-cil 2.12.1-1ubuntu2 CLI binding for the GLib utility library 2.1
ii libglib2.0-data 2.18.2-0ubuntu2 Common files for GLib library
ii libglib2.0-dev 2.20.1-0ubuntu2 Development files for the GLib library
ii libglibmm-2.4-1c2a 2.18.1-1 C++ wrapper for the GLib toolkit (shared lib
Then I search for any "glib.h" anywhere under /usr/include. I get two, /usr/include/glib-1.2/glib.h and /usr/include/glib-2.0/glib.h. So I try:
$ gcc -I/usr/include/glib-2.0 -Wall test.c
In file included from /usr/include/glib-2.0/glib/galloca.h:34,
from /usr/include/glib-2.0/glib.h:32,
from test.c:2:
/usr/include/glib-2.0/glib/gtypes.h:34:24: error: glibconfig.h: No such file or directory
(about 10,000 more errors snipped)
I don't seem to have a glibconfig.h anywhere on my computer.
What do I do now?
glib tends to hide itself... Your include statement doesn't work because GCC doesn't automatically search subdirectories, and so cannot see the glib.h in glib-1.2 or glib-2.0.
Read the Compiling GLib Applications page in the GLIB manuals... you use commands like pkg-config --cflags glib-2.0 to get the right flags for GCC.
The canonical way to do what you are trying is
% gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
Note the back-ticks, which tell the shell to run the pkg-config command "in-place".
> > The canonical way to do what you are trying is
> % gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
Sorry, but no. That is a common misconception, that just happens to work in most cases on ELF-based systems, Linux in particular. The canonical way is to pass in the cflags and libraries separately, in the correct and traditional locations on the command line, like this:
gcc -Wall -o test `pkg-config --cflags glib-2.0` test.c `pkg-config --libs glib-2.0`
It is a pity that pkg-config accepts both the --cflags and --libs options at the same time, as it means this incorrect meme will never die, and people used to it on Linux will continue to be baffled when they then try the same on other platforms.
As #chris said use pkg-config.
glibconfig.h is missing
it’s because this file is not in the /usr/include/glib-2.0, but in /usr/lib/glib-2.0. So you have to include also this /usr/lib path or copy the file to the /include/glib-2.0
I am using glib.h as well-
Do this to compile all your glib.h programs :)
gcc `pkg-config --cflags --libs glib-2.0` filename.c
Make sure to surround pkg-config --cflags --libs glib-2.0 with back-quotes
which you find under tilde (left most key on the querty keyboard).
Thank me later .. :P
apt-get build-dep is your friend -- nobody can remember all the packages you need to build something.

Resources