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

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

Related

Compiling GTK 3 apps on Debian

I'm trying to compile a GTK program on my Debian desktop. I installed libgtk-3-dev and all of that, but when I go to compile the program, I get this error:
$ gcc -o client client.c `pkg-config --cflags --libs glib-2.0`
client.c:6:10: fatal error: gtk/gtk.h: No such file or directory
6 | #include <gtk/gtk.h>
| ^~~~~~~~~~~
compilation terminated.
All of the GTK headers actually seem to be in /usr/include/gtk-3.0, but even if I include <gtk-3.0/gtk/gtk.h> I get errors, since the files inside include other GTK headers as if they were in a normal include path. Then if I compile with -I/usr/include/gtk-3.0 I still get errors, this time about glib. Well, glib files are inside /usr/include/glib-2.0 and it's the same problem as before. Finally, if I compile with -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0, I this time get this error:
$ gcc -o client -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0 client.c `pkg-config --cflags $ --libs glib-2.0`
In file included from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:30,
from /usr/include/gtk-3.0/gdk/gdk.h:32,
from /usr/include/gtk-3.0/gtk/gtk.h:30,
from client.c:6:
/usr/include/gtk-3.0/gdk/gdktypes.h:35:10: fatal error: pango/pango.h: No such file or directory
35 | #include <pango/pango.h>
| ^~~~~~~~~~~~~~~
compilation terminated.
Seems like any combination of compile flags I choose, I get some sort of error, all caused by Debian putting the headers in a non-standard subdirectory. This is not me messing with them, this is what apt installs for me.
You are asking pkg-config for glib, you must ask pkg-config for the GTK+ library:
gcc -o client client.c `pkg-config --cflags --libs gtk+-3.0`
You can keep glib-2.0 in the command line, but since GTK+ depends on glib, it's already included.

How do you use PGO + LTO optimization with GCC

I've been researching a lot lately on how PGO and LTO can significantly optimize a programs speed (some say around 20%). I currently just program in C and build GUI's with GTK+ in Windows (compiling everything through GCC) and only use -O2 for optimization.
I've been reading to compile with the flags
gcc -Wall -g -mwindows -O3 -fprofile-generate -flto example.c -o example.exe `pkg-config --cflags --libs gtk+-3.0`
a .gcno and a .gcda file is created, then you run the compiled .exe a bunch of times then just re-compile the same .c file again but swap the "-fprofile-generate" with "-fprofile-use"? (while making sure all these files stay in the same folder/directory, and use a different name for the .exe compiled the second time)
gcc -Wall -g -mwindows -O3 -fprofile-use -flto example.c -o program.exe `pkg-config --cflags --libs gtk+-3.0`
Or do you make use of the .gcno/.gcda file somehow? Also read things about using a "benchmark".
Question:
What are the steps to use it in GCC? (Step by step guide would help :)

Error compiling with GLib

I'm pretty new to C programming, and am trying to work through the exercises in '21st Century C' second edition. I'm stuck on page 202, Example 9-7, unicode.c. This example starts with:
#include <glib.h>
#include <locale.h> //setlocale
#include "string_utilities.h"
#include "stopif.h"
//Frees instring for you--we can't use it for anything else.
char *localstring_to_utf8(char *instring){
GError *e=NULL;
setlocale(LC_ALL, ""); //get the OS's locale.
char *out = g_locale_to_utf8(instring, -1, NULL, NULL, &e);
free(instring); //done with the original
Stopif(!out, return NULL, "Trouble converting from your locale to UTF-8.");
Stopif(!g_utf8_validate(out, -1, NULL), free(out); return NULL,
"Trouble: I couldn't convert your file to a valid UTF-8 string.");
return out;
}
When I try to compile it with:
c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o -o unicode
I get errors such as:
$ c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o -o unicode
/tmp/ccBDQFiH.o: In function `localstring_to_utf8':
/home/kevin/21st_Century_C/ch09/unicode.c:29: undefined reference to `g_locale_to_utf8'
/home/kevin/21st_Century_C/ch09/unicode.c:32: undefined reference to `g_utf8_validate'
/tmp/ccBDQFiH.o: In function `main':
/home/kevin/21st_Century_C/ch09/unicode.c:48: undefined reference to `g_utf8_strlen'
This seems to indicate that the Glib library is not found, but the compiler didn't complain about this, and the Glib libraries and include files are right where I specified on the command line. I've installed the libglib2.0-dev package in addition to the libglib2.0 package (all installed with 'sudo apt-get ..'). 'pkg-config' seems to find glib-2.0 just fine.
This is all on a Ubuntu 14.04.2 system.
I can't figure out how to correct this error, and don't understand why it can't find the specific Glib functions, if it finds the glib include and lib files.
The order of things in the command line matter. In general it should be something like:
gcc [options] [source files] [object files] [-L stuff] [-lstuff] [-o outputfile]
So give this a whirl instead:
gcc -g -Wall -O3 -std=gnu11 `pkg-config --cflags glib-2.0` \
unicode.c string_utilities.o `pkg-config --libs glib-2.0` \
-o unicode
This is also covered in the Compiling GLib Applications section of the GLib Reference Manual:
$ cc hello.c `pkg-config --cflags --libs glib-2.0` -o hello

Cannot compile a simple c-program using gtk-lib

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.

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