How are C libraries installed? - c

sorry for stupid question, but I have been writing programs in other languages and now that I am starting to get into C, I would like to know this.
I am on Arch (sorry to be that guy, I think it matters), and pretty new to it, but I need to install library.
Are libraries packages, or should I get them from somewhere like github? What is propper procedure?
Library in question is libgimp/gimp.h
Thanks in advance.

Run:
$sudo pacman -S gimp
And then:
$ ls -l usr/include/gimp-2.0/libgimp/
usr/include/gimp-2.0/libgimp/gimp.h
usr/include/gimp-2.0/libgimp/gimp_pdb.h
usr/include/gimp-2.0/libgimp/gimp_pdb_headers.h
usr/include/gimp-2.0/libgimp/gimpaspectpreview.h
usr/include/gimp-2.0/libgimp/gimpbrush_pdb.h
usr/include/gimp-2.0/libgimp/gimpbrushes.h
usr/include/gimp-2.0/libgimp/gimpbrushes_pdb.h
usr/include/gimp-2.0/libgimp/gimpbrushmenu.h
usr/include/gimp-2.0/libgimp/gimpbrushselect.h
usr/include/gimp-2.0/libgimp/gimpbrushselect_pd
...
You will have to provide the compiler flag
-I/usr/include/gimp-2.0
And the linker flag:
-lgimp-2.0 -lgimpbase-2.0
Note: Always include files this way:
#include <libgimp/file_name.h>
e.g.:
#include <libgimp/gimp.h>
Update: As requested by another user, I am attaching the instructions in terms of pkg-config (not a big user myself but it's a great tool):
$ pkg-config --cflags gimp-2.0
-I/usr/include/gimp-2.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libpng16 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/cairo -I/usr/include/lzo -I/usr/include/freetype2 -I/usr/include/harfbuzz -I/usr/include/pixman-1 -I/usr/include/gegl-0.4 -I/usr/include/gio-unix-2.0 -I/usr/include/json-glib-1.0 -pthread -I/usr/include/babl-0.1
$ pkg-config --libs gimp-2.0
-lgimp-2.0 -lgimpmath-2.0 -lgimpconfig-2.0 -lgimpcolor-2.0 -lgimpbase-2.0 -lgdk_pixbuf-2.0 -lcairo -lgegl-0.4 -lgegl-npd-0.4 -lm -Wl,--export-dynamic -lgmodule-2.0 -pthread -lglib-2.0 -ljson-glib-1.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lbabl-0.1
So you may just pass that to your compiler:
GIMP_INCLUDES=$(pkg-config --cflags gimp-2.0)
GIMP_LIBS=$(pkg-config --libs gimp-2.0)
g++ myfile.cpp $GIMP_INCLUDES myfile.o
g++ myfile.o $GIMP_LIBS -o main

Related

linking error. Perhaps a problem with ordering?

I'm building a program which uses GTK+3 and pango. Most of it compiles fine apart from the last bit which builds the executable. The final command is:
gcc -o z80sim main.c -Wall -Iz80 -Igui obj/disas.o obj/iosim.o obj/sim0.o obj/sim1.o obj/sim2.o obj/sim3.o obj/sim4.o obj/sim5.o obj/sim6.o obj/sim7.o obj/simctl.o obj/simfun.o obj/simglb.o obj/simint.o obj/callbacks.o obj/code.o obj/guiglb.o obj/memory.o obj/flags.o obj/log.o obj/ports.o obj/registers.o `pkg-config --cflags --libs gtk+-3.0`
But I get this error message:
obj/sim1.o: In function `cpu':
sim1.c:(.text+0x2cb): undefined reference to `check_gui_break'
obj/callbacks.o: In function `on_open1_activate':
callbacks.c:(.text+0x20): undefined reference to `Get_File'
:
:
Where check_gui_break is defined and called in sim1.c etc.
This has to be something to do with the ordering of object files and libraries on the ld call, but I have tried putting the pkg-config part before and after the individual .o files and it still gives the same errors.
I've read the very informative https://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking but, although I know a lot more now, I still can't crack this one.
BTW.
pkg-config --cflags --libs gtk+-3.0
gives
-pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgtk-3 -lgdk-3 -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
Please will some kind soul take pity on me and enlighten me?
It appears that most of the errors I've encountered have been a result of the transition from Glade2 to Glade3. In 2, Glade generates some C code which is what's missing from my code. Back to the reference manual to see how to modernise the code.

How to use GTK C library?

I'm new at C and started to learn how to create GUI.
For some reason the only way program run is
gcc simple.c -o simple -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0 -pthread -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/pixman-1 -I/usr/include/libpng12
If i just run gcc simple.cit gives me
In file included from /usr/include/glib-2.0/glib/galloca.h:32:0,
from /usr/include/glib-2.0/glib.h:30,
from /usr/include/glib-2.0/gobject/gbinding.h:28,
from /usr/include/glib-2.0/glib-object.h:23,
from /usr/include/glib-2.0/gio/gioenums.h:28,
from /usr/include/glib-2.0/gio/giotypes.h:28,
from /usr/include/glib-2.0/gio/gio.h:26,
from /usr/include/gtk-2.0/gdk/gdkapplaunchcontext.h:30,
from /usr/include/gtk-2.0/gdk/gdk.h:32,
from /usr/include/gtk-2.0/gtk/gtk.h:32,
from graphical.c:1:/usr/include/glib-2.0/glib/gtypes.h:32:24: fatal error: glibconfig.h: No such file or catalog compilation terminated.
If I do other 'traditional' ways to include lib:
gcc `pkg-config gtk+-2.0 --cflags` example.c -o example `pkg-config gtk+-2.0 --libs`
It gives me:
Package gtk+-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-2.0.pc' to the PKG_CONFIG_PATH environment variable
No package 'gtk+-2.0' found
graphical.c:1:21: fatal error: gtk/gtk.h: No such file or catalog
What is the right way to include GTK?
I just figured out what was the problem.
I had linuxbrew installed and for some reason pkg-config took path from one of it's repos even if I force it to export.
I completely removed linuxbrew and now it's finally working!

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)

Get full compilation command in CMake

If you execute vim --version, it will show some information about how it was compiled. On my system, it looks like this:
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK -pthread -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/libdrm -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -D_FORTIFY_SOURCE=2 -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong --param=ssp-buffer-size=4 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc -L. -Wl,-O1,--sort-common,--as-needed,-z,relro -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-E -Wl,-rpath,/usr/lib/perl5/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro -L/usr/local/lib -Wl,--as-needed -o vim -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lfontconfig -lfreetype -lSM -lICE -lXt -lX11 -lXdmcp -lSM -lICE -lm -lncurses -lelf -lnsl -lacl -lattr -lgpm -ldl -L/usr/lib -llua -Wl,-E -Wl,-rpath,/usr/lib/perl5/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro -fstack-protector -L/usr/local/lib -L/usr/lib/perl5/core_perl/CORE -lperl -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc -L/usr/lib/python2.7/config -lpython2.7 -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -lruby -lpthread -lgmp -ldl -lcrypt -lm -L/usr/lib
```
I want to add similar information to the version output of an application that is built using CMake.
I know that there are variables like CMAKE_C_FLAGS, but it depends on other variables if these are actually used. For example, CMAKE_C_FLAGS_RELEASE is used if CMAKE_BUILD_TYPE=Release is specified. Thus it might be error-prone to try to piece this information together from individual CMake variables, and in the end you can't be sure that this is really what CMake uses to compile your sources.
Is there a way to get the actual command CMake uses to compile a source file, similar to the output from vim --version above?
moreover, CMAKE_C_FLAGS could vary for different source files, directories and targets… so your output can be far from accurate…
and using variables like CMAKE_BUILD_TYPE is pretty stable actually and may give you what you want:
string(TOUPPER "${CMAKE_BUILD_TYPE}" _type_upcase)
# Yeah! CMake can do double expand!
set(_cflags "${CMAKE_C_FLAGS_${_type_upcase}}")
little problem is: make sure CMAKE_BUILD_TYPE is set and valid before get the value!
advanced way:
one may use CMAKE_<lang>_COMPILE_OBJECT variable to get a "compile template". then use directory properties to collect COMPILE_DEFINITIONS and replace a pattern <DEFINES> in the template. then collect INCLUDE_DIRECTORIES and other options depending on build type.
You may look here for inspiration.

Header files in subdirectories (e.g. gtk/gtk.h vs gtk-2.0/gtk/gtk.h)

I'm trying to build a hello world using GTK, which includes the line:
#include <gtk/gtk.h>
as you would expect.
The Makefile supplied has the line:
GTK_INCLUDE = -I/usr/local/include
so it would expect to find gtk.h in /usr/local/include/gtk/gtk.h. However on my system, it is located in /usr/local/include/gtk-2.0/gtk/gtk.h, ie within a version'ed subdirectory.
Obviously in this case I can add -I/usr/local/include/gtk-2.0 to the Makefile, but the same problem crops up with gtk.h's dependencies and so on.
Is there a good way of dealing with this? Could configure be used to locate where the header files are and add the appropriate include directories? I know next to nothing about configure, but it seems to find out things about the system at build time, which is what I am after.
Is this a common occurence or do I have some freak directory structure which is the real problem?
Thanks for any pointers!
You need to use pkg-config to get the include paths:
$ pkg-config --cflags gtk+-2.0
-I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12
You must also use it to get the libraries:
$ pkg-config --libs gtk+-2.0
-lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lgio-2.0 -lcairo -lpango-1.0 -lfreetype -lz -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lglib-2.0
(The output of these commands will vary depending on your distribution, and will always be the correct ones for your distribution.)
Probably, you must create a symbolic link like:
ln -s /usr/local/include/gtk /usr/local/include/gtk-2.0
but you can first try to reinstall the GTK package.
I haven't used gtk in a long while, but the way this is normally handled in Linux is that there is a script called packagename-config (in this case, probably gtk-config) that comes with the development headers, which your makefile is supposed to call in order to get the proper include paths and linker flags for the package, using --cflags and --libs respectively.
So try something like
GTK_INCLUDE=`gtk-config --cflags`
(note the use of backticks, not apostrophes)
And you probably also want to add the output of gtk-config --libs to your LDFLAGS in order to make sure you are linking against all the right stuff.

Resources