Compile a simple program on ubuntu arm

夙愿已清 提交于 2019-11-29 15:52:15

The order of options to gcc (and other compilers) matters.

gcc -Wall main.c $(pkg-config --libs --cflags glib-2.0) -o main

And I don't really like the above. You should learn how to use GNU make. At the very least, put the compile flags, then the sources, then the object files, then the libraries (from high level to low level).

gcc -Wall $(pkg-config  --cflags glib-2.0) main.c \
    $(pkg-config --libs glib-2.0) -o main

Better yet, have a Makefile starting with

CC=gcc
CFLAGS= -Wall  $(pkg-config  --cflags glib-2.0) 
LIBES= $(pkg-config --libs glib-2.0)

And compiling as root should be avoided. Only installation should require root privilege...

You probably want to add -g (compiler flag for debugging information). Once the program is ready and nearly bugfree, replace it with -O2 (optimizations) and do some serious testing again!

Ok found a solution

gcc -o main main.c  `pkg-config --libs --cflags glib-2.0`

works, but I don't know why on my X64 linux system it worked also the other way.

You need to add the $(pgk-config --libs glib-2.0) after the main.c in your compile line - because library functions are only dragged into the binary if there is anything using them - so main.c is what is using the g_print, and if the -lglib (or whatever the result of the pkg-config bit is) is before main.c, it doesn't get included in the resulting binary.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!