cross-platform object file

Aug 12, 2011 at 3:30pm
Hi,
I have a very simple c++ code, and it is using all standard static libraries, which I believe are all defined on Unix/Linux and Windows.
I have developed my code on Lnix, and I've build an object file, which I can run easily on Linux.

My question is that is there a way for me to tell gcc, my Linux compiler, to be
able to produce an object file, that can be used on let's say a Windows machine? How about a Mac machine?
Or maybe there is a way, that I can specify in my source code, to know what the target operating system is?

please advise,
thanks,
RB
Aug 12, 2011 at 5:56pm
Google around "cross compiling" with the GCC.
Aug 13, 2011 at 6:15pm

thanks for the reply,

In fact, I have a VERY simple C++ program, it is probably not even more than 30 lines of code, and all it is doing it is mostly dealing with File IO's. I will be honest, and the only library that I included is something called boost::filesystem, but I did search and found out that the "boost" library should run on all platforms. Granted that if this is true, is there a simple way for me to cross platforms.
As I said, I don't have anything fancy, it is just simple c++ code, with opening files, and reading files, and maybe renaming some files, and that is about it. It is a command line C++ code, and doesn't involve any fancy GUI's.
I developed this C++ code on a linux machine, with the following version info:
Linux 2.6.18-164.el5 x86_64
And, I have access to most of gcc versions (4.3.5, 4.4.0, 4.5.1, 4.5.2, 4.6.0)

And, all I want to do is to bring this code over to my classic Mac:
Mac OS leopard 10.6

I tried to read more about this, and I came across the following link:
http://www.sandroid.org/imcross/

This looks very complicated, and just to do that it will take me days, to get it going, and plus I would need a huge storage place on my disk to install all these packages.

I am really hoping there should be a way to bring my simple code over from Linux to Mac, without going through all these massive steps listed above.

my c++ code, has only one function() and then the main().
My guess would be that there should be way simpler method of doing this, since my code is fairly simple.

I really appreciate some help.

thanks,
RB

Aug 13, 2011 at 7:12pm
No, you will never be able to produce an object file that, without modifications, links properly into executables for different OSs. The only exception is maybe if you have an extremely simple function that performs no I/O, allocates no memory, calls no functions (specially dynamically linked ones), and in general doesn't interact with the OS in any way. In that case, you might be able to do it.
But code that does something as complex as file I/O will just never work, unless one of the OSs does something special to support executables from another OS (like what FreeBSD does to support Linux executables), but in that case you could probably just run the final executable on it.

As for why it doesn't work, it's because the compiler has to put references to system calls and/or external functions into the object file. Different OSs have different system calls.
Aug 14, 2011 at 12:10am
Okay,
I tired to use a simpler method. In fact, I borrowed my friend MAC, and I installed xcode on it, then I installed the mac version of the IO library that I had used (boost::filesystem), and now I thought that I had everything ready to go,
I ran simple tests to make both the xcode gcc compiler, and the boost library are working fine, but then when I ran the code that I had developed, it just didn't work, and I was getting an error message regarding of the functions, boost::filesystem::remove(&path), which allow the user to remove a file, by giving it a file path.

I had never had any issue with this function call on unix, but i guess MAC didn't like it.
I guess bosst:filesystem still has its issues.

--RB
Aug 14, 2011 at 12:28am
Can you post the code?
Aug 14, 2011 at 3:34pm
I am running mac os x 10.5.8 and using boost::filesystem from 1.47.0. I have not encountered any issues.

I can't help with your set up in xcode, but if you get boost with macports, then the following line should compile your code:

 
g++ <srcfile> -I/opt/local/include -L/opt/local/lib -lboost_fileysytem-mt -lboost_system-mt
Aug 14, 2011 at 3:57pm
I am running mac x 10.6, and boost 1.47.0

And I am running g++ exactly as you listed above, and pointing to the exact same libraries.

I am saying that it is not behaving the same way on Mac, as I saw it behave under Linux.

this is what I have:

string file1= "my_file1.txt";
string file2= "my_file2.txt";
time_t file1_time;

boost::filesystem::path path1(file1.c_str());
boost::filesystem::path path2(file2.c_str());
if (boost::filesystem::exists(path1) {
file1_time = boost::filesystem::last_write_time(path1);
boost::filesystem::remove(path1);
}

It didn't complain for calling out the "last_write_time" function, but apparently it complained when tried to "remove" it.

I used the same directory/file structure that I had tested on Linux. In fact, I copied over my directories from Linux.
I never encountered any error in Linux, but now when I run the SAME code on the SAME directory/file pattern, I get the error that
boost::filesystem::remove(path1) permission denied

but how come I never got this error on Linux for the same file??
thanks,
RB

Last edited on Aug 14, 2011 at 3:58pm
Aug 14, 2011 at 4:34pm
Permission denied means to me that you don't have write access to the file and possibly (I can't remember if it matters) write permission to the parent directory.

Run ls -l in the directory and look at the permissions. Are you the listed owner and do you have write permission to the file. What about the parent directory?
Aug 14, 2011 at 4:59pm
Yes, the file is a read only file, but the point is that the code works on ready-only files on the Linux platform, and it doesn't bark, but Mac doesn't like it. And I hoping that when I develop a platform independent code using such libraries (such as boost::filesystem), I would expect to see the same behavior, regardless of the platform on which the code is run.

Any reason why the boost::remove works differently on the same type of file, with the same privileges, when run on two different platforms? And is there a way to fix this.
Because I don't want to care about the file write/read permissions of the file, just how I do it on the Linux machine.

RB
Aug 14, 2011 at 5:14pm
I can't replicate your behaviour on Linux (Ubuntu 10.04 LTS). If the parent directory has write access the file will be deleted, if it doesn't you get permission denied.
Aug 14, 2011 at 7:26pm
You should ask this in the Boost mailing lists, but I think it's more likely that you're doing something wrong on your end than there being inconsistent semantics between two such common target platforms.
Aug 14, 2011 at 8:18pm
I also asked this in boost mailing list, but hasn't got any answer!

http://groups.google.com/group/boostusers/browse_thread/thread/806d2e3d11f8efe2?hl=en#
Topic archived. No new replies allowed.