LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2004, 02:34 AM   #1
natalinasmpf
Member
 
Registered: Dec 2003
Distribution: Slackware 9.1
Posts: 309

Rep: Reputation: 30
How do I make a C program adapt to both Windows and Linux?


Ie. I want to make a C program, that can run and install itself on both Windows and Linux. Ie. one will adapt if its a Windows system, or install in a different way if its a Linux system.

I don't understand how to do it: also do the standard headers ie. stdio or creation of files, do they work on the logical or kernel level or on the physical level? Or use both?

Ie. I might also want the file to copy only part of itself, ie. the first 6000 bytes or something. Then I might want to adapt to the filesystem. I also might want to control the speed of copying so it doesn't hog all the resources, but still use it on a logical level?

Which brings me to my next question: I want to contain all the data to be copied in one program, then the program would extract the data from that. Sort of like an installer. How do I do this in C, in one program? Also refer to dynamic files: the file isn't created yet, but after it is created, I want to refer to it.
 
Old 03-14-2004, 08:07 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: How do I make a C program adapt to both Windows and Linux?

Quote:
Originally posted by natalinasmpf
Ie. I want to make a C program, that can run and install itself on both Windows and Linux. Ie. one will adapt if its a Windows system, or install in a different way if its a Linux system.
Type the following on the command line to find out what is predefined by the preprocessor itself:

echo | cpp -dM -

These macro's are defined for the specific system you run this on. Among others, you'll see:

#define __unix__ 1
#define unix 1
#define __linux 1
#define __unix 1
#define __linux__ 1
#define linux 1
#define __gnu_linux__ 1

Pick a macro of your choice and do for example:
Code:
#ifdef __linux__
/* put code here you want to compile when compiled on linux */
#else
/* put code here you want to compile when compiled on other OS */
#endif
Quote:
I don't understand how to do it: also do the standard headers ie. stdio or creation of files, do they work on the logical or kernel level or on the physical level? Or use both?
Things defined in stdio.h work (almost) the same way with all ANSI compliant compilers. Of course to open a file in windows for example, you would start the file name with a driver letter like "c:" and use backslashes instead of forward-slashes (linux) in the path for the file name.

Quote:
Ie. I might also want the file to copy only part of itself, ie. the first 6000 bytes or something. Then I might want to adapt to the filesystem. I also might want to control the speed of copying so it doesn't hog all the resources, but still use it on a logical level?
What do you mean by "adapt to the filesystem" and "still use it on a logical level"?

Quote:
Which brings me to my next question: I want to contain all the data to be copied in one program, then the program would extract the data from that. Sort of like an installer. How do I do this in C, in one program?
Just put that file in you source package. If you want a different file for different OS's, use different file names, and use "#ifdef __linux__" (see above) to use the different file names when compiled on a different OS.

Quote:
Also refer to dynamic files: the file isn't created yet, but after it is created, I want to refer to it.
Just create and open it with fopen() from stdio.h. I don't understand the problem you could have with this. Or do I understand your question wrong?

Last edited by Hko; 03-14-2004 at 08:08 AM.
 
Old 03-14-2004, 09:07 AM   #3
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Hko rocks at this.. I was going to write that it should not be possible .

Thanks mate!
 
Old 03-14-2004, 10:41 AM   #4
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
Hko is talking about code that be compiled on either a Unix or a Windows machine, which is certainly possible. Natalinasmpf seems to be talking about binary code that can run on either Windows or Linux, which is not at all possible. Actions like file operations, or even printing to stdout, require calls to the operating system. Needless to say, the APIs for Windows and Linux are quite different. I think the best you can do is write code, like hko is saying, that can be compiled on either machine.
 
Old 03-14-2004, 11:00 AM   #5
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
Don't know how helpful this is, here is a simple work around. Write the code for windows and see if you can get it working under wine. If you are happy with the portability of source, try to stick to posix. That should help most of the time. To be honest, I did not read your mail. I read HKO's reply. To copy the same file use argv[0]. Don't bother much about speed control. There is an operating system to take care of such stuff. 6000 bytes is not much of data.

Bye,
Sarin
 
Old 03-14-2004, 01:35 PM   #6
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
#ifdefs are a really bad idea. avoid whenever possible. instead, try encapsulating platform specific code behind portable interfaces.

For eg.
// portable header (memmap.h)...
void MemoryMapFile( ... )

// win32 implementation (win32_memmap.c)
void MemoryMapFile( ... )
{
CreateFile( ... )
CreateFileMapping( ... )
MapViewOfFile( ... )
}

// linux implementation (win32_memmap.c)
void MemoryMapFile( ... )
{
open( ... )
mmap( ... )
}
 
Old 03-14-2004, 11:30 PM   #7
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Having a binary that can run on both windows and linux systems is possible. Just check out the "dual" app at http://www.deater.net/weave/vmwprod/asm/ . Maybe it could help you?
 
Old 03-15-2004, 12:29 PM   #8
shortfuse
Member
 
Registered: Feb 2004
Location: Texas
Distribution: Debian
Posts: 30

Rep: Reputation: 15
Quote:
Originally posted by jinksys
Having a binary that can run on both windows and linux systems is possible. Just check out the "dual" app at http://www.deater.net/weave/vmwprod/asm/ . Maybe it could help you?
While it may be possible to do something as simple as write characters to the console using assembly I doubt that interfacing with something more complex like a filesystem will be possible.

Hko has the right idea, your gonna have to compile for the host platform simply becuz they both use different implementations of the standard c runtime library. Linux uses glibc, Windoz uses msvcrt, so, unless you can find a runtime lib that will work across both platforms and can be statically linked, your stuck with two different binaries.

Now the Windoz runtime lib is pretty much ANSI compatible and I have had much success writing code that runs on both platforms AS LONG AS your just using stuff like stdio.h, stdlib.h...etc. Basically if you have to add a -l to link some lib then it's gonna be a little more complicated. Here's a couple links for ya, Mingw32 works fine for the simple stuff, CygWin is for the more complicated ports.

http://www.mingw.org

http://www.cygwin.com
 
Old 03-15-2004, 04:52 PM   #9
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
I didnt say it would be easy, the only thing holding you back from writing a complex multi OS program would be the lack of knowledge in the subject.
 
Old 03-25-2004, 08:15 AM   #10
brainstormingin
LQ Newbie
 
Registered: Mar 2004
Location: india
Distribution: red hat 7.3
Posts: 6

Rep: Reputation: 0
Question Re:How do I make a C program adapt to both Windows and Linux?

looks like a crossplatform virus in da makin !!
 
Old 03-25-2004, 08:38 AM   #11
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
Quote:
Originally posted by jinksys
I didnt say it would be easy, the only thing holding you back from writing a complex multi OS program would be the lack of knowledge in the subject.
Presumably the only thing holding me back from inventing a time machine is lack of knowledge of the subject, too.

Or maybe some things are actually impossible?
 
Old 03-25-2004, 01:26 PM   #12
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
can anyone tell me why such a program is impossible?
 
Old 03-25-2004, 02:33 PM   #13
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
its not impossible, but very difficult. windows executes PE format executables and linux generally executes ELF executables, the header of both is different so you have to write either one or the other. it is however quite easy to teach linux to execute PE executables, the problem is they make OS/library calls that are not present on linux, the idea of wine is to provide these missing calls.

a description of ELF and PE are here

even if you could write a program that operated on both it would be useless on non x86 machines. why not write your program in an interpreted language thats available on both platforms such as python/java.

by shortfuse
While it may be possible to do something as simple as write characters to the console using assembly I doubt that interfacing with something more complex like a filesystem will be possible.

the file was being executed as a .com and therefore should have access to int 0x21 allowing it to make filesystem calls.
 
Old 03-25-2004, 10:28 PM   #14
shortfuse
Member
 
Registered: Feb 2004
Location: Texas
Distribution: Debian
Posts: 30

Rep: Reputation: 15
Quote:
Originally posted by kev82
the file was being executed as a .com and therefore should have access to int 0x21 allowing it to make filesystem calls.
That kev82 he's da guru!!
 
Old 03-25-2004, 10:33 PM   #15
shortfuse
Member
 
Registered: Feb 2004
Location: Texas
Distribution: Debian
Posts: 30

Rep: Reputation: 15
Quote:
Originally posted by kev82
the file was being executed as a .com and therefore should have access to int 0x21 allowing it to make filesystem calls.
That kev82 he's da guru!!

Jinksys, For me I just don't see why something like that would be worth the trouble. There are plenty of language choices that would make porting much much easier than worrying about one executable so, why put yourself through that kind of pain?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sms program and database in linux web program in windows.. does not see each other.. keikun_naruchan Programming 0 07-06-2005 01:40 AM
What Linux program is available to pull cd music off a cd and make it into a mp3? BajaNick Linux - Software 11 02-02-2005 10:09 PM
Running a windows program from windows partition, on linux, possible? Kriptis Linux - Newbie 4 01-18-2005 10:52 PM
Slower to adapt to technology? Clark Bent General 3 07-26-2004 11:56 PM
Is it possible to make a Linux server execute a program on a WinXP system? Cichlasoma Linux - General 4 04-23-2004 06:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration