How do I make a C program adapt to both Windows and Linux?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
#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?
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.
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.
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?
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.
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.
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.
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.