Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cross-compile C++-library with dependencies?

I need to cross-compile some C/C++ library. The library depends on several C/C++ libraries. Some of those of libraries in turn depend on other libraries. All libraries come with configure script. I know how to compile and install libraries on host system – install dependencies before the lib I need. Obviously this won't work when cross-compiling. Any tips are appreciated. Thank you.

like image 851
pic11 Avatar asked Aug 03 '12 12:08

pic11


3 Answers

Generally, to cross-compile an autotooled package, you pass a couple of extra arguments to ./configure: --host and --build. --host is the name of the system that the built programs will run on, and --build is the name of the system that does the compiling.

When I say "name of the system", I mean a tuple of the form ARCH-VENDOR-OS-LIBC. (For example i686-pc-linux-gnu is the tuple describing the system I'm currently using.) Sometimes parts of the tuple are elided, as in the case of the mingw32 toolchain (on my system, the mingw32 cross tools are installed with the tuple i586-mingw32msvc and/or amd64-mingw32msvc).

(There's another argument to configure, --target, which is for cross-compiling compilers and specifies the system that the compiler being built will target when generating code.)

Each toolchain has its own subdirectory under /usr such as /usr/i586-mingw32msvc. You're going to want to install new packages here so they get found. Use the --prefix argument to configure.

So to cross-compile from my GNU/Linux system to a MinGW32 system, I would run configure like this:

./configure --host=i586-mingw32msvc --build=i686-pc-linux-gnu --prefix=/usr/i586-mingw32msvc

So start with the leaves of your dependency graph and work your way up. You may want to also pass --enable-static --disable-shared to configure: that will stop the creation of dynamic libraries for libtooled packages. You may have to install some packages natively as well as cross-compiling them, if a package needs to run a program as part of the build.

Sometimes configure's tests will fail: where it tries to compile and run a program, for instance. Often these tests set a cache variable which you can also pass on the command line to configure. Similarly, you can override things like program paths and library compile/link flags. Check your package's ./configure --help.

like image 164
Jack Kelly Avatar answered Oct 03 '22 01:10

Jack Kelly


There is absolutely no problem in building against cross-compiled shared libraries so long as you ensure that you are building against headers and libraries for the target system rather than the development host.

Even if you built all of the obvious dependancies as static libraries, you'll probably still find yourself linking against shared C and C++ language runtime libraries. You also want to be sure that the header files used for cross-compiling are those from your target system rather than the development host.
Not doing this will probably work most of the time. The remainder of the time you will get subtle hard-to-debug crashes.

How you achieve this depends on how your cross tool-chain and target environment. As you've mentioned neither, I'll take a guess it's GCC and some flavour of Linux as the development host and target - although the principles apply equally to using other development hosts.

Cross-compiling GCC and bin-utils distributions are typically build so that the default library and header search paths points to a copy of the target system's root filing system. This is where the linker will look for shared libraries to link against, and it is to here that you install your dependant shared libraries when they are built.

You can override GCC's default system root by passing the --sysroot <mySysRoot> option to GCC on the command line.

like image 42
marko Avatar answered Oct 03 '22 01:10

marko


What you need to do is to create static libraries for all the dependencies and statically link them to your executable. You'll need to use the cross compiler to produce those libraries. You can find some good examples by looking up "mingw static link cross compile"

like image 45
gmlime Avatar answered Oct 02 '22 23:10

gmlime