How to use compile GTK in C on Mac OS - c

I'm getting started in c and would like to create GUI, so I tied using GTK.
I followed the tutorial they gave on https://www.gtk.org/docs/getting-started/hello-world/ , but when I get to compiling, I get this error:
--libs gtk4`
-bash: pkg-config: command not found
hello-world-gtk.c:2:10: fatal error: 'gtk/gtk.h' file not found
#include <gtk/gtk.h>
how do I fix this!!! I downloaded it and everything, but it still doesn't work!
BTW the command used to run is: gcc main.c -o p1 pkg-config --cflags --libs gtk+-2.0
not sure if this has anything to do with it.
the main.c is in the desktop, and I made sure to cd to it, so that can't be the problem.

The error suggests that gtk.h can't be found in the typical include directory (/usr/local/include) or the current directory. Run
find /usr/local/include . -name gtk.h
to verify you actually have the gtk.h file. If you do, you may need to move it. If not, you need to download the file.

Related

GTK compilation on mac returns several errors

I have been trying to get started on GTK for Mac. I followed the installation steps and ran the hello-world program. I keep getting this error with gcc, though. Running
gcc $(pkg-config --cflags gtk4) -o hello-world-gtk hello-world-gtk.c $(pkg-config --libs gtk4)
returns the following errors:
Package gtk4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk4' found
Package gtk4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk4' found
hello-world-gtk.c:1:10: fatal error: 'gtk/gtk.h' file not found
#include <gtk/gtk.h>
^~~~~~~~~~~
1 error generated.
I found a similar problem here, but the user says they already have gcc/g++ working. I don't.
Any ideas? I ran the installation script included in the installation steps and nothing changes.
Also, I have tried replacing the line
#include <gtk/gtk.h>
with
#include <gtk-2.0/gtk/gtk.h>
I then get this error:
Package gtk4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk4' found
Package gtk4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk4' found
In file included from hello-world-gtk.c:1:
/usr/local/include/gtk-2.0/gtk/gtk.h:32:10: fatal error: 'gdk/gdk.h' file not
found
#include <gdk/gdk.h>
^~~~~~~~~~~
1 error generated.
If I add the flags -I/usr/include/gtk-2.0 -lgtk2.0 to my compile statement, I get the same error.

Compile an SDL project using gcc?

How can I compile a sdl project using gcc in the linux command line without using Cmake?
EDIT;
gcc SDLGAME.c pkg-config --cflags --libs sdl2
but i get error.
gcc: error: Pkg-config: No such file or directory
gcc: error: sdl2: No such file or directory
gcc: error: unrecognized command line option ‘--cflags’
gcc: error: unrecognized command line option ‘--libs’; did you mean ‘--libs=’?
#HolyBlackCat
source code --->>
#include <stdio.h>
#include <SDL2/SDL.h>
int main(int argc,char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) !=0)
{
printf("error SDL");
return 0;
}
SDL_Window* win=SDL_CreateWindow("Game",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
500,500,0);
return 0;
}
I get this error--->>
error: XDG_RUNTIME_DIR not set in the environment.
error SDL
Caveat: This isn't a total solution but some suggestions and is prefaced by top comment's and comments under OP's [now deleted] answer.
To review ...
After fixing the original issue by use of:
gcc -o SDLGAME SDLGAME.c `pkg-config --cflags --libs sdl2`
OP running the program produces:
error: XDG_RUNTIME_DIR not set in the environment.
This may be a general issue about the ubuntu install itself. Some resources for that: https://askubuntu.com/questions/872792/what-is-xdg-runtime-dir and
https://askubuntu.com/questions/456689/error-xdg-runtime-dir-not-set-in-the-environment-when-attempting-to-run-naut
A workaround may be:
export XDG_RUNTIME_DIR=/tmp/dir
mkdir -p /tmp/dir
But, I ran the program successfully on my home system, running fedora 29 and my ubuntu system running 18.04.5
On my systems, XDG_RUNTIME_DIR was set to /run/user/1000. However, with/without the workaround and even doing unset XDG_RUNTIME_DIR worked on my systems.
However: On my ubuntu system, I had removed the standard libsdl2 package and rebuilt and installed it from the source package a year ago due to some issues I had.
So, if the workaround doesn't work, I recommend libsdl2 rebuild/reinstall from source.
Even if the standard package is working, when debugging your app, it can be helpful to be able to consult the libsdl2 source.
Note that one change I made to your app was to add a sleep(3) at the bottom so you can see the window come up.
Here is the method I used to build/install from source:
It's probably necessary to uninstall/remove the binary libsdl2 package. So, you'll have to do (e.g.)
sudo apt-get remove libsdl2 libsdl2-dev
Or, whatever the binary package is called [I forget]. But, those also came from: apt-cache search libsdl2
So, once that's cleaned out, what I did was:
Create a directory (e.g.): $HOME/aptsrc
cd $HOME/aptsrc
Download the source package [without sudo]: apt-get source libsdl2
This extracts several files (e.g. *.tar.gz, *.tar.xz, *.dsc and a directory. On my system, it was: libsdl2-2.0.8+dfsg1, but for you it may be different. Do (e.g.): DIR=$HOME/aptsrc/libsdl2-2.0.8+dfsg1
cd $DIR
Configure with: $DIR/configure
Run cmake: cmake $DIR
Run make with: make
Install with: sudo make install
Note that this comes from an internal script I created. Even after the cd $DIR, I think it's necessary to use full path on the commands [where indicated].
Now, the library should be installed under /usr/local. The output of pkg-config --cflags --libs sdl2 should reflect this:
-D_REENTRANT -I/usr/local/include/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -Wl,--enable-new-dtags -lSDL2
The original output of this command would have looked like:
-I/usr/include/SDL2 -D_REENTRANT -lSDL2
This is for the standard install from the binary package, so if you still have that, the binary package may still be installed.
Otherwise, you should now be able to rebuild your app using the original gcc command. Now, it should be attached to the source built version of the library. You can confirm this with: ldd ./SDLGAME but just running it might be easier.

How to use kplot (Cairo plotting library) without installing it

kplot is a UNIX programming library for plotting graphs on a Cairo surface. The source code is available here.
After downloading the source code I extraced it to the directory kplot-master and cd into it. Simple ls now shows
array.c
border.c
bucket.c
buffer.c
....
example0.c
example1.c
....
I am using Ubuntu 14.04 LTS. Cairo is installed in my system and I tested it by successfully compiling C codes available in [zetcode dot com slash gfx slash cairo slash cairobackends slash] (Sorry as I am not allowed to link more than two).
I am new to GTK and Cairo plotting library and would like help in the following directions:
I do not want to install kplot in my system.
I just want to learn how kplot uses Cairo.
When I use the following command:
gcc example0.c -o example `pkg-config --cflags --libs gtk+-3.0`
it produces the following error message:
example0.c:17:20: fatal error: compat.h: No such file or directory
#include "compat.h"
^
compilation terminated.
It will be very helpful if somebody shows me how to test those kplot examples without installing it.
There is no need to install.
First you will need to compile the kplot library. For that, cd to the kplot directory and run a make command. This will generate the file compat.h. After that you will be able to compile example by example with make example(n) command, or with gcc example(n).c -o example(n) `pkg-config --cflags --libs gtk+-3.0` libkplot.a -lbsd -lm command.
If you have GTK+-3.0 and Cairo dev libraries installed, everything should go well.

I have installed SDL but it still doesn't seem to work

I am on a mac.
So I've looked everywhere what i first did was download the SDL file from https://hg.libsdl.org/SDL
After that i followed the instructions on https://wiki.libsdl.org/Installation
and wrote the following on my terminal
mkdir build
cd build
../configure
make
sudo make install
so far so good but after this when i cd to my Desktop and type gcc test.c sdl2-config --cflags --libs into my terminal it still gives me the error
test.c:2:10: fatal error: 'SDL2/SDL.h' file not found
#include <SDL2/SDL.h>
^
1 error generated.
if i type just sdl2-config --cflags --libs into my terminal it returns
-I/usr/local/include/SDL2 -D_THREAD_SAFE
-L/usr/local/lib -lSDL2
so i think it is successfully installed any idea on what the problem is?
Thanks a lot
Your header is located at /usr/local/include/SDL2/SDL.h but you're trying to include /usr/local/include/SDL2/SDL2/SDL.h. Either remove "SDL2" from your #include statement or your -I switch.

OpenCV - C library is not working in my Ubuntu11.10

Resently I'm installed Opencv in my machine. Its working in python well(I just checked it by some eg programs). But due to the lack of tutorials in python I decided to move to c. I just run an Hello world program from http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/
while compiling I got the following error
hello-world.c:4:16: fatal error: cv.h: No such file or directory
compilation terminated.
I'm new in opencv
Qn : Could you please report what may be the problem - and how I run my helloworld program in c?
Your compiler cannot find your cv.h include file. If you installed from your package manager, it is probably in /usr/include/opencv/. You need to add that your include search path. If you are compiling from the command line use -I to specify additional include directories. It will be something like -
gcc -I /usr/include/opencv/ -o helloworld helloworld.c
If you are using Eclipse,
Right click on the project and select properties.
Select C/C++ General -> Path and Symbols.
Select Includes tab.
In Languages list, select 'GNU C' or 'GNU C++' depending on which you are using.
Press 'Add...' button and add /usr/include/opencv/
Save and rebuild.
You need to show compiler path to cv.h file. The quick way to find it is to do (on Ubuntu):
find /usr -name "cv.h"
/usr/local/include/opencv/cv.h
Just add this to the compiler:
gcc -I/usr/local/include/opencv -o helloworld helloworld.c
Since you asking this question your compiler might also have problems linking your program to opencv libraries. Just do the same thing only for library files:
find /usr -iname "libopencv*"
/usr/local/lib/libopencv_flann.so
...
add this folder the same way and specify libraries you want to use:
gcc helloworld.c -I/usr/local/include/opencv -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -o helloworld
that should probably compile. There is a also a short cut you can take and instead of all that steps just use the following command
gcc helloworld.c `pkg-config --cflags --libs opencv` -o helloworld
that should take care of all the work of locating required files for you and let you focus on the fun coding part.
maybe you just installed the opencv package.
But, as you want to use opencv in your C program, you may also install the package named just like opencv-devel. If you haven't, install it and than use it as #iagreen said.
Best wishes to you.

Resources