Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross compilation requirements for C

I have some basic knowledge about compiling C but need to get a couple of generic cross-compilation questions answered. In my case, I am trying to cross-compile a program on my Fedora Linux box that is going to be run on an ARM single board computer.

  1. My first question is about headers. I have downloaded the arm Linux tools package and it contains headers files such as stdio.h in an include directory. Am I supposed to use this "target" include directory as opposed to my system include directories when I am cross-compiling? Or is it OK to point to my system's include dirs such as /usr/include? (These header files seem to be different when I diff them.)

  2. What happens if a header file does not exist. In my case, I am also planning to utilize the cURL library on the ARM board. Can I simply point to the include directory present in the curl source package that I downloaded without worrying about the target architecture? If yes, does this mean my first question is irrelevant?

  3. Let's say I want to statically link to a library. Does this static library need to be compiled for the target ARM platform before this happens? Or can I use the static libraries installed on my system directly (hoping that the cross compilation process takes care of business)?

  4. If I decide to dynamically link to a library, the only requirement would be that the target system has this library compiled for ARM and installed in one of the LD_LIBRARY_PATH directories on the ARM board, am I correct?

Thanks for the help.

like image 386
Burak Avatar asked Nov 08 '10 00:11

Burak


People also ask

What is cross-compilation in C?

To cross-compile is to build on one platform a binary that will run on another platform. When speaking of cross-compilation, it is important to distinguish between the build platform on which the compilation is performed, and the host platform on which the resulting executable is expected to run.

What is cross-compiler in CD?

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a compiler that runs on a PC but generates code that runs on an Android smartphone is a cross compiler.

Why is cross compiling so hard?

"building a cross-compiler is significantly harder than building a compiler that targets the platform it runs on." The problem exists due to the way libraries are built and accessed. In the normal situation all the libraries are located in a specific spot, and are used by all apps on that system.

Is GCC is a cross-compiler?

Explanation: GCC, a free software collection of compilers, also can be used as cross compile. It supports many languages and platforms.


2 Answers

  1. Always use the target headers. They may differ from your host headers. The compiler should already have them as part of its default include path for the standard issue, such as libc.
  2. You will need to build cURL using the cross compiler into a fake "target" system directory, and build your application with cURL in this target directory. As you need a cURL library as well, you MUST use the cross compiler. For compiles which are not cross compile friendly (such as building programs and running then as part of the compile), you will need to modify the build process. Sometimes fakeroot can be helpful for dirty build systems.
  3. You can't arbitrarily use a static library for a different architecture from your system. They must be built by the cross compiler.
  4. Incorrect. The library generally must be present.
like image 169
Yann Ramin Avatar answered Oct 06 '22 23:10

Yann Ramin


Any library you intend on using, including the libc, must be built for the target platform before you can link against it. Use the target headers instead of the host headers for building so that you're assured of using the correct API.

like image 34
Ignacio Vazquez-Abrams Avatar answered Oct 07 '22 00:10

Ignacio Vazquez-Abrams