编译ubuntu上臂上一个简单的程序 [英] Compile a simple program on ubuntu arm

查看:157
本文介绍了编译ubuntu上臂上一个简单的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序

#include <glib.h>
int main(){
  g_print("hallo\n");
}

,并尝试与Ubuntu编译它的嵌入式系统(Odroid X2)像

and try to compile it on the embedded system (Odroid X2) with Ubuntu like that

root@odroid:~/# gcc $(pkg-config --libs --cflags glib-2.0) -o main main.c
/tmp/cci48ASK.o: In function `main':
main.c:(.text+0xc): undefined reference to `g_print'
collect2: error: ld returned 1 exit status

安装的编译:

root@odroid:~/x# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.7/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libitm --enable-plugin --with-system-zlib --enable-objc-gc --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --enable-multilib --disable-sjlj-exceptions --with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-mode=thumb --disable-werror --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix

任何想法,为什么链接器没有找到参考?

Any ideas why the linker does not find the reference?

推荐答案

选项以 GCC (和其他编译器)订单的问题的。

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

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

和我真的不喜欢上面。你应该学习如何使用 GNU使。最起码,把编译的标志,则源,那么对象的文件,则库(从高电平到低电平)。

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

更妙的是,有一个的Makefile 开始

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

和应该避免编译为根。只有安装应该需要root权限...

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

您可能要添加 -g (编译器标志用于调试信息)。一旦程序已经准备好,几乎bugfree,用 -O2 替换(优化),并再次做一些认真的测试!

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!

这篇关于编译ubuntu上臂上一个简单的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆