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

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

问题描述

我有一个简单的程序

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

并尝试在嵌入式系统(Odroid X2)上使用 Ubuntu 编译它

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 make.至少,放置编译标志,然后是源代码,然后是目标文件,然后是库(从高级到低级).

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

更好的是,有一个以

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

并且应该避免以 root 身份编译.只有安装需要root权限...

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

您可能想要添加 -g(用于调试信息的编译器标志).一旦程序准备好并且几乎没有错误,将其替换为 -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 arm上编译一个简单的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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