Thread: best process for cross compiling an application

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    4

    Question best process for cross compiling an application

    Hello!

    I'm creating a small commandline game in C. I have never done anything cross platforms, but this is small enough (so far) that it might not be too bad.

    When I am done, I'm not sure how it will be distributed: Either I will just send people the C files and say "compile on your system with these options", or I will just have executables for various systems. Probably Windows 7/8, Ubuntu, CentOS, and whatever I can find to test on.

    I right now I'm testing/developing on Windows 7 using MinGW. So my questions are:
    while I'm developing, how should I be compiling/testing it?
    What kinds of things should I look out for?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Using only standard C headers will make it more portable.

    NOTE: You can try compiling under CygWin with the CygWin C Compiler to test a little bit for doing Windows only code.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I recommend early testing on all your intended targets.

    How often you re-test depends on what kinds of problems you encountered on your first test.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    From your description, I suspect you're not really understanding what cross-compilation is. It is the process of compiling on one type of system (OS, hardware, etc) to produce objects, libraries, and (if you have a cross-linker) executables that run - preferably natively - on another. It does not refer to writing your code in a portable manner (which is a good idea, if possible, regardless) so you can distribute source and tell the recipient to build the program (which is usually not a good idea with anyone other than software developers).

    As a general rule, if you can compile/build on a desired target, then it is better to do so, rather than resorting to cross-compilation. And then do required validation (e.g. testing) to determine if it behaves as intended on each target machine you care to support. That means you need representative machines for both building and validating your program.

    Cross-compilation is usually employed when it is not possible or practical to get a development environment that targets the desired machine. For example, embedded devices. However, in rough terms, the process usually goes something like

    1) Build using cross-compiler
    2) Test program in some environment on development machine that emulates target machine
    3) Transfer executable to actual target machine
    4) Test it on that target machine with representative inputs.

    Generally speaking, I would suggest you need to spend more time up front with analysis and design, before even cutting code.

    A common trap to fall into is not testing enough on an actual target machine. Another common trap is reaching for a cross-compiler unless there is a reasonable need to. Cross-compilers are more likely to have incompatibilities and bugs than native compilers (getting a cross-compiler right takes more effort than getting a compiler right, because of dependencies on both development machine and target machine). Most emulators have incompatibilities, so passing tests in an emulator is not always sufficient to give assurance of passing tests on the actual target. Another trap is not compiling with all compiler diagnostics (warnings, errors) enabled, because the the problems that come up when porting code (i.e. writing code so it can be built on multiple machines) are magnified when doing cross-compilation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    Quote Originally Posted by grumpy View Post
    It is the process of compiling on one type of system (OS, hardware, etc) to produce objects, libraries, and (if you have a cross-linker) executables that run - preferably natively - on another.
    I misspoke with the term "cross-compiling". What I was really aiming for is probably described as portable code. Whether it being just using stuff from the standard library, or by having preprocessor #ifdef statements to use different code depending on the platform it is being compiled on. The end result being so that I can put the same *.c files on a Windows machine or on a Ubuntu machine and have it compile to a working program.

    So I was looking for things like "use -ansi flag when testing" or "strcmpi isn't portable so use XXX for non-windows"... things like that.

    I apologize for not being clear enough in my original post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cross Compiling GDB for MIPS
    By vlrk in forum Linux Programming
    Replies: 4
    Last Post: 10-21-2009, 09:18 AM
  2. Cross-compiling a Windows command-line application
    By papagaio in forum C Programming
    Replies: 6
    Last Post: 08-29-2008, 02:38 AM
  3. Cross compiling
    By X56THN in forum Linux Programming
    Replies: 5
    Last Post: 07-04-2008, 03:30 PM
  4. Cross-Compiling: Libraries Necessary?
    By JupiterV2 in forum C++ Programming
    Replies: 5
    Last Post: 05-22-2008, 05:17 PM
  5. Cross-process data transfer
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 07-22-2003, 04:11 PM