微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在ubuntu arm上编译一个简单的程序

我有一个简单的程序
#include <glib.h>
int main(){
  g_print("hallo\n");
}

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

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

链接器找不到引用的任何想法?

gcc(和其他编译器)的选项顺序很重要.
gcc -Wall main.c $(pkg-config --libs --cflags glib-2.0) -o main

而且我不喜欢上述内容.您应该学习如何使用GNU make.至少,将编译标志,然后是源,然后是目标文件,然后是库(从高级到低级).

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身份编译.只有安装才需要root权限……

您可能希望添加-g(用于调试信息的编译器标志).一旦程序准备好并几乎无bug,将其替换为-O2(优化)并再次进行一些严格的测试!

原文地址:https://www.jb51.cc/ubuntu/347577.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐