473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Portable C code

I was just wondering whether you knew whether it is possible to compile a
fully portable C program? I want to write a 'C' CGI program on Windows,
then just upload it to my Plus.Net Debian-based server.
As I want to write pure ANSI C, then I thought it may be possible to write a
program that would run fine on both. Having said that, surely (?), it would
have to be compiled for specific platforms, unless of course there is some
abstraction layer at runtime, like with java. Anyway, this is probably not
possible now I have thought about it.
But I would love to know what you guys and gals think?!

Cheers,

Kristan
Oct 5 '06 #1
14 2086
>I was just wondering whether you knew whether it is possible to compile a
>fully portable C program? I want to write a 'C' CGI program on Windows,
then just upload it to my Plus.Net Debian-based server.
Source code is portable. Executables are not. It is quite possible
that an executable on one OS is not recognized as an executable on
another, even if they are running on the actual same CPU (which
dual-boots different OSs). And even if it is, it may not run correctly.
>As I want to write pure ANSI C, then I thought it may be possible to write a
program that would run fine on both.
Compile it on both.
>Having said that, surely (?), it would
have to be compiled for specific platforms, unless of course there is some
abstraction layer at runtime, like with java. Anyway, this is probably not
possible now I have thought about it.
But I would love to know what you guys and gals think?!
Oct 5 '06 #2
[Followups set to comp.lang.c, since I don't suppose the Microsoft people
are all that interested in portability]

Kristan Dyson said:
I was just wondering whether you knew whether it is possible to compile a
fully portable C program?
That depends on what you mean by "fully portable", of course. But it's
certainly possible to have a very, very portable C program.

What is not possible, alas, is to have one binary that runs on any platform
you like - no matter what language it's written in.

So you do need a compiler on each platform (or group of binary-compatible
platforms) that you wish to support.
I want to write a 'C' CGI program on Windows,
then just upload it to my Plus.Net Debian-based server.
As I want to write pure ANSI C, then I thought it may be possible to write
a program that would run fine on both.
Well, the point of writing it in pure ANSI C is so that you can compile the
source code without first having to customise it to a particular platform.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 5 '06 #3
Kristan Dyson wrote:
I was just wondering whether you knew whether it is possible to compile a
fully portable C program? I want to write a 'C' CGI program on Windows,
then just upload it to my Plus.Net Debian-based server.
As I want to write pure ANSI C, then I thought it may be possible to write a
program that would run fine on both. Having said that, surely (?), it would
have to be compiled for specific platforms, unless of course there is some
abstraction layer at runtime, like with java. Anyway, this is probably not
possible now I have thought about it.
It depends on what the CGI program must do.
If it can accomplish its goal by standard C, go for it.
CGI programs typically only reads from stdin, writes
to stdout which surly can be done in standard C.
Care must be taken on how you handle and convert
input/output data though.

If you need other features - sockets, database access, etc.
it is not, but perhaps you can find third party packages for
such things that atleast are portable among the systems you
want to support.
Oct 5 '06 #4

"Richard Heathfield" <in*****@invalid.invalidwrote in message
news:Ht******************************@bt.com...
[Followups set to comp.lang.c, since I don't suppose the Microsoft people
are all that interested in portability]
snip
There is nothing quite as stationary as an island, e.g. England. It is c
that doesn't talk to MS, not the other way around. EC
Oct 5 '06 #5
Elijah Cardon wrote:
"Richard Heathfield" <in*****@invalid.invalidwrote in message
news:Ht******************************@bt.com...
>[Followups set to comp.lang.c, since I don't suppose the Microsoft people
are all that interested in portability]
snip
There is nothing quite as stationary as an island, e.g. England.
Eh?
It is c that doesn't talk to MS, not the other way around. EC
C doesn't talk /at all/. Neither does it eat, or write three-volume
novels, nor snore at twenty-seven minutes past three in the morning.

Unpack?

--
Chris "falling further in" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Oct 5 '06 #6
Thank you all for your help! Hopefully, I will one day know enough about C
/ C++ to return the favour

cheers
kristan

"Kristan Dyson" <kr*****@hotmail.co.ukwrote in message
news:45***********************@ptn-nntp-reader04.plus.net...
>I was just wondering whether you knew whether it is possible to compile a
fully portable C program? I want to write a 'C' CGI program on Windows,
then just upload it to my Plus.Net Debian-based server.
As I want to write pure ANSI C, then I thought it may be possible to write
a program that would run fine on both. Having said that, surely (?), it
would have to be compiled for specific platforms, unless of course there
is some abstraction layer at runtime, like with java. Anyway, this is
probably not possible now I have thought about it.
But I would love to know what you guys and gals think?!

Cheers,

Kristan


Oct 5 '06 #7
Kristan Dyson wrote:
I was just wondering whether you knew whether it is possible to compile a
fully portable C program? I want to write a 'C' CGI program on Windows,
then just upload it to my Plus.Net Debian-based server.
You would need to upload the source code and then recompile on the
server to generate new binary executables. If don't have shell access,
or if there is no compiler on the server, you may be screwed.

That's probably one of the things that makes an interpreted language
like Perl more popular for CGI stuff. Install a Windows version of Perl
(ActivePerl ??), monkey with your scripts until they work, then just put
them in proper place on the server.
Oct 5 '06 #8

John Smith wrote:
Kristan Dyson wrote:
I was just wondering whether you knew whether it is possible to compile a
fully portable C program? I want to write a 'C' CGI program on Windows,
then just upload it to my Plus.Net Debian-based server.
I don't know much about that
You would need to upload the source code and then recompile on the
server to generate new binary executables. If don't have shell access,
or if there is no compiler on the server, you may be screwed.
People have told, make the source code portable, binaries are not
portable.
That's probably one of the things that makes an interpreted language
like Perl more popular for CGI stuff. Install a Windows version of Perl
(ActivePerl ??), monkey with your scripts until they work, then just put
them in proper place on the server.
Perl is good for CGI. So decide early to go for perl/script type
thingies or a full fledged programming language like C.

How to make portable code in C:

1. Have an indepth knowledge of features in ANSI-C. (Not just internals
in C)
2. Study the compilers on platforms you are working in. Different
compilers may or may not support all the features of ANSI-C
3. Try not to use dirty hacks, though great, they are often device
specific
4. Then the lame stuff: design your program logic independent of OS. So
divide device/platform specific code to the logical part of it. e.g.
Don't directly assume fopen would be available created a wrapper for
it.

These are just starters if you are new to making code portable.

Oct 6 '06 #9
In article <c4****************@twister.nyroc.rr.com>,
John Smith <us**@example.netwrote:
>Kristan Dyson wrote:
>I was just wondering whether you knew whether it is possible to compile a
fully portable C program? I want to write a 'C' CGI program on Windows,
then just upload it to my Plus.Net Debian-based server.
>You would need to upload the source code and then recompile on the
server to generate new binary executables. If don't have shell access,
or if there is no compiler on the server, you may be screwed.
There are cross-compilers available in limited cases.

>That's probably one of the things that makes an interpreted language
like Perl more popular for CGI stuff. Install a Windows version of Perl
(ActivePerl ??), monkey with your scripts until they work, then just put
them in proper place on the server.
No, Perl turns out to relatively *not* portable. For more words
on this subject, please see my earlier posting,
http://groups.google.ca/group/comp.l...4fa97697731535
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Oct 6 '06 #10
Kristan Dyson wrote:
I was just wondering whether you knew whether it is possible to compile a
fully portable C program? I want to write a 'C' CGI program on Windows,
then just upload it to my Plus.Net Debian-based server.
As I want to write pure ANSI C, then I thought it may be possible to write a
program that would run fine on both. Having said that, surely (?), it would
have to be compiled for specific platforms, unless of course there is some
abstraction layer at runtime, like with java. Anyway, this is probably not
possible now I have thought about it.
But I would love to know what you guys and gals think?!

Cheers,

Kristan

/* Stirling's approximation for n! & ln(n!) */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.141592653589793
long double stirling(long double n);
long double lnstirling(long double n);

int main(int argc, char *argv[])
{
long double n, sa, lnsa, pi;

if(argc != 2) exit(EXIT_FAILURE);
pi = PI;
n = strtold(argv[1], NULL);
sa = stirling(n);
lnsa = lnstirling(n);
printf("%.Lf! = %.2Lf\n", n, sa);
printf("ln(%.Lf!) = %.8Lf\n", n, lnsa);

return 0;
}

/* Stirling's approximation */
long double stirling(long double n)
{
long double t1, t2, t3;

t1 = sqrt(2.0 * PI * n);
t2 = pow(n, n);
t3 = exp(-n + 1.0 / 12.0 / n);

return t1 * t2 * t3;
}

/* ln(stirling) */
long double lnstirling(long double n)
{
return (n +.5) * log(n) - n + log(2.0 * PI) / 2.0;
}

*** not part of the program above for Sterlings approximation **

/* Return the natural log of gamma(x) */
double log_gamma(double x)
{
int idx, sizep;
double r, g;
double p[] = {1.000000000190015,
76.18009172947146,
-86.50532032941677,
24.01409824083091,
-1.231739572450155,
1.208650973866179E-3,
-5.395239384953E-6};
r = p[0];
g = 5.;
sizep = sizeof(p) / sizeof(*p);

for (idx = 1; idx < sizep; idx++)
{
r += p[idx] / (x + idx);
}

return log(r) + log(atan(1) * 8) / 2 - log(x) +
log(x + g + .5) * (x + .5) - (x + g + .5);
}

Oct 7 '06 #11
"quarkLore" <ag*************@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
4. Then the lame stuff: design your program logic independent of OS. So
divide device/platform specific code to the logical part of it. e.g.
Don't directly assume fopen would be available created a wrapper for
it.
Good point (and very smart design), however do you really think that fopen
should be included in this category? I mean isn't fopen() a part of the C
standard? So it should be in the implementation of every C compiler

--
Abdo Haji-Ali
Programmer
In|Framez
Oct 7 '06 #12
fopen (standard library) is part of conforming virtual machine. Standard C
allows non-conforming environment, too. Filename format is also
platform-dependent.

"Abdo Haji-Ali" <ah***@inframez.org_use_com_insteadwrote in message
news:eU**************@TK2MSFTNGP04.phx.gbl...
"quarkLore" <ag*************@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
>4. Then the lame stuff: design your program logic independent of OS. So
divide device/platform specific code to the logical part of it. e.g.
Don't directly assume fopen would be available created a wrapper for
it.
Good point (and very smart design), however do you really think that fopen
should be included in this category? I mean isn't fopen() a part of the C
standard? So it should be in the implementation of every C compiler

--
Abdo Haji-Ali
Programmer
In|Framez


Oct 7 '06 #13
"Alexander Grigoriev" <al***@earthlink.netwrites:
"Abdo Haji-Ali" <ah***@inframez.org_use_com_insteadwrote in message
news:eU**************@TK2MSFTNGP04.phx.gbl...
>"quarkLore" <ag*************@gmail.comwrote in message
news:11**********************@c28g2000cwb.googleg roups.com...
>>4. Then the lame stuff: design your program logic independent of OS. So
divide device/platform specific code to the logical part of it. e.g.
Don't directly assume fopen would be available created a wrapper for
it.
Good point (and very smart design), however do you really think that fopen
should be included in this category? I mean isn't fopen() a part of the C
standard? So it should be in the implementation of every C compiler
fopen (standard library) is part of conforming virtual
machine. Standard C allows non-conforming environment, too. Filename
format is also platform-dependent.
Please don't top-post. Read these links:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

Standard C doesn't "allow" non-conforming environments, except in the
fairly obvious sense that if an environment doesn't conform to the C
standard, the standard has nothing to say about it.

fopen() is required for all conforming hosted implementations. It's
not required for conforming freestanding implementations (typically
embedded systems). But an implementation that doesn't provide fopen()
likely doesn't have a file system at all.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 7 '06 #14
fopen() is required for all conforming hosted implementations. It's
not required for conforming freestanding implementations (typically
embedded systems). But an implementation that doesn't provide fopen()
likely doesn't have a file system at all.
....or several of them :)
Oct 9 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: James Harris | last post by:
Hi, Can someone recommend a book that will teach me how to approach C programming so that code is modularised, will compile for different environments (such as variations of Unix and Windows),...
8
by: suresh_C# | last post by:
Dear All, What is difference between Portable Executable (PE) file and a Assembly? Thanks, Mahesh
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
162
by: Richard Heathfield | last post by:
I found something interesting on the Web today, purely by chance. It would be funny if it weren't so sad. Or sad if it weren't so funny. I'm not sure which. ...
12
by: Kristan Dyson | last post by:
I was just wondering whether you knew whether it is possible to compile a fully portable C program? I want to write a 'C' CGI program on Windows, then just upload it to my Plus.Net Debian-based...
5
by: copx | last post by:
How portable are direct bit operations? Which ones are portable? I have never bothered learning such low-level stuff (I have an excuse: I am not a professional programmer), so I really don't know....
409
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are...
7
by: jacob navia | last post by:
There are some people here (let's call them "regulars" for short) that are always giving lessons to people about how easy is to write portable code, etc. They always point fingers at you telling...
23
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.