Thread: Run C on any/many platforms

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    11

    Run C on any/many platforms

    I am a complete 'newb' to C, recently started learning(similar to python kinda).I cannot find online through google, how I can turn a C file into an actual application that can be used on many platforms (not expecting an apple)- plus with it how to add other files to be compiled with it (specially when I get to gui, which I am no where near or sure if I even need an extra library for basic gui).

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    C doesn't define any kind of binary interface, so basically you have to compile the code for each platform. Compilers like GCC can produce output for many different machines (even simultaneously, with the aid of makefiles), others may be much more limited. Besides that, just avoid non-standard header files and non-standard extensions to the lanuage and you should be okay (assuming any third-party libraries you're using as well are portable too). You also might want to check out a good C programming book, just work through all the exercises until you get the hang of things.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    When you are ready to distribute your application (along with associated data files) to end users this process is typically called "deployment" and it varies based on user base and operating system. For example, in Windows using an installer program is the most common way. In Linux distributions there is typically a package format (such as DEB or RPM) that you should provide to allow users to install your application.

    As Sebastiani mentioned the program itself must be compiled for and tested on each target architecture/operating system.

  4. #4
    Registered User
    Join Date
    Aug 2014
    Posts
    11
    Thanks, I think I understand it now. C has to be compiled specific lye for each platform AND tested/avoid non standard headers(as I am still new, I am guessing this is the stuff at the very like like studio.h) & file types. And, I did try looking into a book, but was very confusing since it just gives examples without explaining the smaller parts.,I think I'll try using online, like I did with python, and learn as I need. Learned basic to intermediate(with gui) in about half a month.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Tim Leitzke View Post
    C has to be compiled specific lye for each platform AND tested/avoid non standard headers(as I am still new, I am guessing this is the stuff at the very like like studio.h) & file types.
    I assume by "studio.h" you mean "stdio.h".

    Anyway, one basic technique is to build your program (compiler, link, test) with several distinct compilers, even on the same target platform. Although that doesn't give a guarantee of catching problems with standard headers (any more than building for different platforms does) it does increase the odds.

    Another basic technique is to configure your compiler to emit maximum warning levels (use of command line options, build options in a project, etc). The methods are compiler (and IDE, if you use an IDE) specific. The thing to remember is that virtually ALL C compilers are configured at a minimum warning level (i.e. the compilers can give indications of more problems than they do by default). This is a sad result of lazy C programmers lobbying compiler vendors to do it that way.

    Quote Originally Posted by Tim Leitzke View Post
    And, I did try looking into a book, but was very confusing since it just gives examples without explaining the smaller parts.
    I think it's the other way around, actually. Most books give small examples that explain one or two points (language or library features, etc) but few explain the gotchas that arise in practice when several of those points come together and interact.

    Quote Originally Posted by Tim Leitzke View Post
    ,I think I'll try using online, like I did with python, and learn as I need.
    Python is actually a bit different, since almost everything associated with it originated online.

    With C, you will probably need to find some decent textbooks, compiler documentation (bear in mind that some compiler vendors have a penchant for documenting their vendor-specific extensions as standard - by which they mean "standard for their products", not standard as in "ISO standard compliant), work through examples, try to do real things with code, etc. As with any learning the mix of reading, thinking, and "hands on" depends on your learning style.

    But learning as you need is a reasonable idea. Despite some uninformed claims, C is actually a pretty large language/library combination and it takes time to learn.

    Quote Originally Posted by Tim Leitzke View Post
    Learned basic to intermediate(with gui) in about half a month.
    It'll probably take longer than that with C.

    GUI is not part of standard C at all - people who do GUI with C typically make use of third-party frameworks or libraries and the authors of such libraries typically rely on a fairly robust understanding of the basic parts of standard C. If you care about portability of your code, there are typically porting concerns associated with your chosen GUI framework/library. Some libraries are system specific, so code using them can't be easily ported. Others are designed with portability in mind, but have specific gotchas that vary between host systems, compilers, standard libraries, etc.

    GUI aside, there are distinct differences between what someone with Python experience will call "intermediate" and what a person experienced with C will. They are different languages, designed for quite different purposes. Some of the things that are easy in Python are quite easy in C. And vice versa. One rather poor way of putting it is Python supports "high level" techniques, and C supports "low level" techniques - the skills needed overlap, but differ substantially.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing For Other Platforms
    By Grantyt3 in forum C++ Programming
    Replies: 1
    Last Post: 01-19-2006, 11:10 AM
  2. Platforms
    By okinrus in forum Game Programming
    Replies: 0
    Last Post: 04-05-2004, 05:11 PM
  3. Checking Platforms
    By Elrek in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2004, 06:16 PM
  4. Platforms, your Fav?
    By DarkViper in forum Linux Programming
    Replies: 21
    Last Post: 12-27-2002, 04:34 PM
  5. Porting C to Various Platforms
    By to_ani in forum C Programming
    Replies: 9
    Last Post: 01-12-2002, 03:12 AM