Cannot compile a simple c-program using gtk-lib - c

I am trying to implement a graphic userinterface to my c-programs. To that i have installed the GTK3.0-pacakge to my computer and followed the tutorial. But I am not able to compile a simple c-program that shows a 200*200 px window.
According to the tutorial one should compile like this:
gcc base.c -o base ‘pkg-config --cflags --libs gtk+-3.0‘
Where base.c is the actual program and pkg-config is the program in the bin-folder that tells the compiler where to find the header-files and libraries.
If I just test if the pkg-config-file is there I write its name in the cmd-window:
pkg-config
I get the following answer:
Must specify package names on the command line
So I actually have this config-file.
But when I try to compile base.c with the above syntax that is
gcc base.c -o base pkg-config --cflags --libs gtk+-3.0
I get the following errors
gcc: error: pkg-config: No such file or directory
gcc: error: unrecognized command line option '--clfags'
gcc: error: unrecognized command line option '--libs'
gcc: error: gtk+-3.0: no such file or directory
So why do I get these compilation errors?
Have I missunderstood what is shown in the tutorial? Is the compilation syntax just an abbreviation of what one should do?

You've just wrongly understood the tutorial. In you string disappeared the back ticks which are vital.
That means, the right line is
gcc base.c -o base `pkg-config --cflags --libs gtk+-3.0`
Compare to your (wrong) line:
gcc base.c -o base pkg-config --cflags --libs gtk+-3.0
All the staff starting with pkg-config is an independent command, not a flag for gcc.
Back ticks tell the shell to run its contents in a subshell and substantiate the result of the command run in the gcc command line.

Related

Get error compiling example of gtk example from https://www.gtk.org/docs/getting-started/hello-world/

Hi i am trying to compile the Hello World of gtk from the official website. I done everything asked and I installed GTK by msys2 package manager. My cmd cant detect the package-config directory. I have done setting path for my msys264 on the environment variable. I am sorry if it sounded confusing as I am begineer. TQ.
C:\Users\60182\Desktop\vc>gcc `pkg-config --cflags gtk+-3.0` -o hello-world-gtk hello-world-gtk.c `pkg-config --libs gtk+-3.0`
gcc: error: `pkg-config: No such file or directory
gcc: error: gtk+-3.0`: No such file or directory
gcc: error: `pkg-config: No such file or directory
gcc: error: gtk+-3.0`: No such file or directory
gcc: error: unrecognized command-line option '--cflags'
gcc: error: unrecognized command-line option '--libs'
I can see the problem. Windows shell does not work like linux shells. The backtick character is used for passing the output of one command as a parameter to another.
So in order to do this manually, you can do something like this. First run the command pkg-config --cflags gtk+-3.0 and then pkg-config --libs gtk+-3.0. I don't know what the output will be, but assume the outputs are just "foo" and "bar" and that it looks like this:
C:\Users\60182\Desktop\vc>pkg-config --cflags gtk+-3.0
foo
C:\Users\60182\Desktop\vc>pkg-config --libs gtk+-3.0
bar
Then you do this:
C:\Users\60182\Desktop\vc>gcc foo -o hello-world-gtk hello-world-gtk.c bar

Why does tcc not recognize "main" when I include the -pthreads flag?

I am trying to compile a c application with the gtk 3.0 library with tcc. The documentation says the command to run to compile is
gcc `pkg-config --cflags gtk+-3.0` -o [executable name] [source file] `pkg-config --libs gtk+-3.0`
I am trying to use tcc to compile, and it from what I can tell, the syntax should be the same. However, where gcc compiles it fine, when I use tcc, compilation fails with the error:
tcc: error: undefined symbol 'main'
I isolated the problem to the -pthread flag inserted by pkg-config --cflags gtk+-3.0 Thus, running a simple "Hello, World" c program and compiling with
tcc -pthread -o [executable name] [source file]
results in the same error. Am I compiling wrong, is it a compiler bug, or something else?
try -lpthread
tcc hello.c -lpthread -o hello

Compiling in c: include file not found

I got following error
'libwebsockets.h' file not found
but I have installed libwebsockets with the command
brew install libwebsockets
How can I solve this error?
I want to implement a websocketserver and i need this.
If my code is only has this following line
#include <libwebsockets.h>
it gave me an error.
I tried to compile it with gcc foo.c
You need to pass the appropriate compile and link flags to the compiler for it to find the headers and libraries.
This is usually done with the program "pkg-config". To get the compile flags, run:
pkg-config libwebsockets --cflags
To get the link flags:
pkg-config libwebsockets --libs
If you compile and link in the same step, you need to pass the flags that are output by both of the above commands to the compiler. If you have separate compilation and link commands, you pass the "--cflags" output during compiling, and the "--libs" output during linking.
In your case, you can compile with:
gcc $(pkg-config libwebsockets --cflags) foo.c $(pkg-config libwebsockets --libs)
The $() syntax takes the output of the command you put between ( and ) and puts it into yours, as if you had typed it in.
You should probably write a small build script to do that for you. For example in a file named "build" in the same directory as "foo.c":
#! /bin/sh
gcc $(pkg-config libwebsockets --cflags) foo.c $(pkg-config libwebsockets --libs)
Make the script executable:
chmod +x build
And then just call it:
./build

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

how to compile a program with gtkmozembed.h

i have written a program under ubuntu, in which i include gtkmozembed.h. I am facing a problem in compiling the program.Below is the simplest form of a program which uses gtkmozembed.
#include <gtk/gtk.h>
#include <stdio.h>
#include <gtkmozembed.h>
int main(){
GtkWidget *mozEmbed;
mozEmbed = gtk_moz_embed_new();
return 0;
}
Eventhough, the above program is doing nothing, compiling that program is a lot for me...
I am trying to comile the above program like below
gcc `pkg-config --libs --cflags gtk+-2.0` test.c -o test
and it is giving the following error...
error: gtkmozembed.h: No such file or directory
I can understand, something else has to be added to the above gcc line,so that the compiler can find the gtkmozembed.h, but not getting what is that, 'something'...Looking for someone's help..Thank you...
Install libxul-dev (sudo apt-get install libxul-dev) and include
#include <gtkmozembed.h>
in the main file(test.c) and compile with
gcc `pkg-config --cflags --libs gtk+-2.0 xulrunner-gtkmozembed` test.c -o test
Your problem is that gtkmozembed.h is not found in the standard include file lookup path (well, the error does tell you that pretty obviously). On my system it lives in $(include)/gtkmozembed/, so you have two options
Change the path of the included file in your source
#include <gtkmozembed/gtkmozembed.h>
or manually add the path to the lookup path
gcc `pkg-config --libs --cflags gtk+-2.0` -I/usr/include/gtkmozembed test.c -o test
You should go with option 1).
This will tell gcc where to find the include file, but as pointed out by Matthew this is not enough: you will most probably also need to add more information for linking and required additional includes. Thankfully gtk-mozembed comes with a pkg-config file, so you can get all the needed information like you did for gtk+-2.0 with
pkg-config --libs --cflags mozilla-gtkmozembed-embedding
or combined with the other call
gcc `pkg-config --libs --cflags gtk+-2.0 mozilla-gtkmozembed-embedding` test.c -o test
You should also (just for kicks) have a look at what pkg-config does. The part in "`" is just what is return by the shell when executing that command. On my machine:
$ pkg-config --libs --cflags mozilla-gtkmozembed-embedding
-DXPCOM_GLUE -fshort-wchar \
-I/usr/include/xulrunner-1.9.2 -L/usr/lib/xulrunner-devel-1.9.2/lib -lxpcomglue
(line breaks added by me). The -I parts just adds additional needed directories to the include file lookup path -- they were emitted because you called with --cflags. The entries with -lxpcomglue is due to calling with --libs and ask for linking against this library, i.e. libxpcomglue.so. It is located in /usr/lib/xulrunner-devel-1.9.2/lib. The rest are a define and a gcc flag needed for gtkmozembed.
Try this:
gcc `pkg-config --libs --cflags gtk+-2.0 mozilla-gtkmozembed-embedding` test.c -o test

Resources