{"id":273,"date":"2013-01-22T11:57:26","date_gmt":"2013-01-22T10:57:26","guid":{"rendered":"http:\/\/paguilar.org\/?p=273"},"modified":"2013-01-22T11:57:26","modified_gmt":"2013-01-22T10:57:26","slug":"cross-compiling-libgtop2","status":"publish","type":"post","link":"https:\/\/paguilar.org\/?p=273","title":{"rendered":"Cross-compiling LibGTop2"},"content":{"rendered":"<p>When you have a Linux embedded system and you&#8217;re programming in C (that is quite common), sometimes you need to calculate inside a program the resources usage for a given process ID such as CPU or memory; or in general you could need to know the network bandwith or the list of mounted devices.<br \/>\nThe most commmon way for gathering this data is to parse out the files inside <em>\/proc\/<\/em>, but this can be tedious. There is a discussion at <a href=\"http:\/\/stackoverflow.com\/questions\/1420426\/calculating-cpu-usage-of-a-process-in-linux\">stackoverflow<\/a> about it.<\/p>\n<p>An alternative, if you already have or can install glib2, is to use LibGTop  that <em>&#8220;is a library to get system specific data such as CPU and Memory Usage and information about running Processes&#8221;<\/em>.<\/p>\n<p>Compiling and installing it in a PC is not a problem as the nice <a href=\"http:\/\/www.linuxfromscratch.org\/blfs\/view\/svn\/gnome\/libgtop.html\">Beyond Linux From Scratch<\/a> guide shows, but for cross-compiling it is not straight forward.<\/p>\n<p>Here&#8217;s the simple procedure:<\/p>\n<p><strong>Package<\/strong><br \/>\n<a href=\"ftp:\/\/ftp.gnome.org\/pub\/gnome\/sources\/libgtop\/2.28\/libgtop-2.28.4.tar.xz\">glibtop-2.28.4<\/a><\/p>\n<p><strong>Requirements<\/strong><br \/>\nThis library is built on top of other libraries such as glib2 and intltool. I wrote <a href=\"https:\/\/paguilar.org\/?p=11\">a post<\/a> some time ago that could be helpul for compiling glib2. If you&#8217;re using buildroot, it already builds glib2 for you if selected.<\/p>\n<p><strong>Environment configuration<\/strong><br \/>\nAs normal, set the environment variables that make our life easier. Change them according to your needs. These settings are for an ARM based processor:<\/p>\n<pre lang=\"bash\">\n$ export HOST=arm-linux\n$ export BUILD=i386-linux\n$ export PREFIX=\/home\/projects\/rootfs\/usr\/local\n<\/pre>\n<p><strong>Configuration<\/strong><br \/>\nSince I have an embedded system without graphical interface and it&#8217;s a single-core processor, I don&#8217;t need X nor the SMP parameter. Adjust it according to your system:<\/p>\n<pre lang=\"bash\">\nGLIB_CFLAGS=\"-I$PREFIX\/usr\/include\/glib-2.0 -I$PREFIX\/usr\/lib\/glib-2.0\/include -I$PREFIX\/usr\/include\" \nGLIB_LIBS=\"-L$PREFIX\/lib\" \n.\/configure \n--build=$BUILD \n--host=$HOST \n--prefix=$PREFIX\/usr \n--without-x \n--without-libgtop-smp \n--disable-gtk-doc-html \n--disable-nls\n<\/pre>\n<p>The <em>examples\/<\/em> directory doesn&#8217;t build properly unless you modify the <em>Makefile<\/em>. If you don&#8217;t want to compile this directory you can skip it by adding to the above configure command the parameter <em>&#8211;with-examples<\/em>.<\/p>\n<p>Let&#8217;s assume that you want the <em>examples\/<\/em> directory:<br \/>\nEdit the file <em>examples\/Makefile<\/em>:<br \/>\nSet the <em>LIBS<\/em> variables to the following value:<\/p>\n<pre lang=\"bash\">\nLIBS = -L\/home\/projects\/rootfs\/usr -L\/home\/projects\/rootfs\/usr\/lib -lglib-2.0 -lintl\n<\/pre>\n<p><strong>Compilation and installation<\/strong><\/p>\n<pre lang=\"bash\">\nmake\nmake install\n<\/pre>\n<p><strong>Using LibGTop2<\/strong><br \/>\nFor compiling and linking a program that uses LibGTop2 you need to include the header files and the libraries. Here&#8217;s a fragment of the Makefile I use:<\/p>\n<pre lang=\"bash\">\nCC = arm-linux-gcc\nCFLAGS = -Wall -Os \n\nINCLUDES = -I. -I$(PREFIX)\/usr\/include -I$(PREFIX)\/usr\/include\/glib-2.0 -I$(PREFIX)\/usr\/lib\/glib-2.0\/include -I$(PREFIX)\/usr\/include\/libgtop-2.0\nLDFLAGS = -L$(PREFIX)\/usr\/lib -L$(PREFIX)\/usr\/local\/lib -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl -lgtop-2.0\n\nSRC = my_top.c\nOBJS = $(patsubst %.c, %.o, $(SRC))\n\n# Link\nmy_top: my_top.o\n    @echo LD my_top\n    @$(CC) $(LDFLAGS) $(OBJS) -o $@\n\n# Pull in dependency info for *existing* .o files\n-include $(OBJS:.o=.d)\n\n# Compile\nall: $(OBJS)\n\n%.o: %.c \n    @echo CC $*.c\n    @$(CC) $(INCLUDES) -c $(CFLAGS) $*.c -o $*.o\n    @$(CC) -MM $(CFLAGS) $*.c > $*.d\n<\/pre>\n<p>That&#8217;s it. Have fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you have a Linux embedded system and you&#8217;re programming in C (that is quite common), sometimes you need to calculate inside a program the resources usage for a given process ID such as CPU or memory; or in general you could need to know the network bandwith or the list of mounted devices. The\u2026 <span class=\"read-more\"><a href=\"https:\/\/paguilar.org\/?p=273\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[52,22,26],"class_list":["post-273","post","type-post","status-publish","format-standard","hentry","category-compiling","tag-compiling","tag-cross-compiling","tag-open-source"],"_links":{"self":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=273"}],"version-history":[{"count":0,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/273\/revisions"}],"wp:attachment":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}