0

I am on Ubuntu and have developed a modbus1.c that #includes a certain modbus.h. I want to cross compile what I wrote to run on an embedded computer; to do this I know I need to use the arm-linux-gcc crosscompiler.

I have this library called libmodbus installed on Ubuntu. It conveniently uses pkg-config (which defines -I/usr/include/modbus and -lmodbus for --cflags and --libs respectively)

When I use the regular GCC:

gcc -o modbus1-release modbus1.c `pkg-config --cflags --libs libmodbus`

Everything works fine, modbus1-release is created and I can execute it.

The problem arises when I try to use the arm-linux-gcc compiler:

/usr/local/arm-linux/bin/arm-linux-gcc -o modbus1-release modbus1.c `pkg-config --cflags --libs libmodbus`
/usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/../../../../arm-linux/bin/ld: cannot find -lmodbus
collect2: ld returned 1 exit status
make: *** [release] Error 1

Apart from a direct answer, when I imagine what could 'help' me Im thinking answer to the following questions might 'help' me:

  1. how do I expand all the ../ and ../../../../? If I know where it is then maybe I can put -lmodbus in there
  2. what is -lmodbus? is it a file? where is it such that the regular gcc and ld can find it?
  3. is -l a flag and modbus the file name?

My intuition is telling me that the problem is coming from linking... the cc and ld in the arm-linux-gcc toolchain is missing infromation that the normal cc and ld has.

Any info will help!

TIA

Niko

verbose mode...

/usr/local/arm-linux/bin/arm-linux-gcc -v -o modbus1-release modbus1.c `pkg-config --cflags --libs libmodbus`
Reading specs from /usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/specs
Configured with: ../configure --target=arm-linux --disable-shared --with-headers=/home/gerg/new-wave.xscale/linux-2.4.x/include --with-gnu-as --with-gnu-ld --enable-multilib
Thread model: posix
gcc version 3.3.2
 /usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/cc1 -quiet -v -I/usr/include/modbus -iprefix /usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/ -D__GNUC__=3 -D__GNUC_MINOR__=3 -D__GNUC_PATCHLEVEL__=2 -D__ARM_ARCH_4T__ modbus1.c -quiet -dumpbase modbus1.c -auxbase modbus1 -version -o /tmp/ccVLqs9W.s
GNU C version 3.3.2 (arm-linux)
 compiled by GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "NONE/include"
ignoring nonexistent directory "/usr/local/lib/gcc-lib/arm-linux/3.3.2/include"
ignoring nonexistent directory "/usr/local/arm-linux/sys-include"
ignoring duplicate directory "/usr/local/arm-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/modbus
 /usr/local/arm-linux/lib/gcc-lib/arm-linux/3.3.2/include
 /usr/local/arm-linux/arm-linux/sys-include
 /usr/local/arm-linux/arm-linux/include
End of search list.
 /usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/../../../../arm-linux/bin/as -o /tmp/ccMyv4IL.o /tmp/ccVLqs9W.s
 /usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/collect2 -dynamic-linker /lib/ld-linux.so.2 -X -m armelf_linux -p -o modbus1-release /usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/../../../../arm-linux/lib/crt1.o /usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/crti.o /usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/crtbegin.o -L/usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2 -L/usr/local/arm-linux/bin/../lib/gcc-lib -L/usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/../../../../arm-linux/lib -L/usr/local/lib/../arm-linux/lib -L/usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/../../.. /tmp/ccMyv4IL.o -lmodbus -lgcc -lc -lgcc /usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/crtend.o /usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/crtn.o
/usr/local/arm-linux/bin/../lib/gcc-lib/arm-linux/3.3.2/../../../../arm-linux/bin/ld: cannot find -lmodbus
collect2: ld returned 1 exit status
make: *** [release] Error 1
asked Apr 4, 2014 at 0:05
2
  • Sounds like you forgot to build the MODBUS library for ARM... Commented Apr 4, 2014 at 0:08
  • how can I do that? I have a cross-compiler for the ARM and the source code for the MODBUS library... but cannot figure out how to build the modbus library for the ARM. I tried changing CC in the Makefile to arm-linux-gcc. Commented Apr 18, 2014 at 22:36

1 Answer 1

2

If you want to use pgk_config in a cross compile situation (which is apparently supported, at least with newer versions of autotools), you need to set PKG_CONFIG_SYSROOT_DIR in your Makefile, otherwise pkg_config will set up an environment suitable for the host compiler only, later trying to combine cross compiled binaries with host libraries which obviously doesn't work.

answered Apr 4, 2014 at 5:00
Sign up to request clarification or add additional context in comments.

6 Comments

ok that definitely sounds reasonable, what directory should I set PKG_CONFIG_SYSROOT_DIR to?
wherever you have your "cross tree" located - pkgtools expect a tree which looks like a native UNIX directory tree ( $PKG_CONFIG_SYSROOT_DIR/usr $PKG_CONFIG_SYSROOT_DIR/lib, etc.)
ok I see, I understand what needs to be done and why. Thanks! I just wish all those directories hidden above in the original post were not hidden as ../ or ../../../ so that I could see and try them
so you're saying that I am trying to link the host modbus library with target binary code and thats why its not working? so if lmodbus is a library for my host machine I need a library for the target machine in order for the cross compiler to link it properly?
but then how does specifying PKG_CONFIG_SYSROOT_DIR give me the library I need for the target machine? the library was only compiled once from source and that was for my host machine.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.