problems running windows c program on ubuntu

March 16, 2011 at 06:04:48
Specs: Windows 7
I'm attempting to comlie a simple c++ multi threading program (really really simple one) for an assignment in university. I have used the c complier in visual studio 9.0 from the command line in win7 to build and run it and it works fine. When I attempt the same thing on my ubuntu installation on a netbook using gcc it throws a load of errors which are beyond ny currently meager skills to understand. I have tried installing several versions of gcc with no sucess.

See More: problems running windows c program on ubuntu

Report •


#1
March 16, 2011 at 06:19:10
Well, it would. Programs have to be designed for the OS on which they are to run, particularly where multi-threading is involved as this is highly OS-dependent.

If the program is short enough to post here, do so and someone may be able to point you in the right direction; but if you are using specific Windows function calls it may be difficult to adapt it to Linux.


Report •

#2
March 16, 2011 at 06:25:08
Unless you're using a portable threading library, like Boost's Thread library, your threading code isn't cross-platform. So something you built and ran on Windows won't work on a Linux system.

How To Ask Questions The Smart Way


Report •

#3
March 16, 2011 at 06:29:52
#include <iostream>
#include <windows.h>
using namespace std;

//once this is spawned, the thread
//will run independenlty while
//main() keeps running
void threadProcess()
{
for (int i=0; i < 10; i++)
{
cout<<"this is a thread"<<endl;
Sleep(500);
}
}

int main()
{
HANDLE threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadProcess, NULL, 0, NULL);

for(int i = 0; i < 1; i++)//in the modified version the number of main processes is only 1
{
cout<<"This is the main process"<<endl;
Sleep(100);
}

//wait for the thread to finish

WaitForSingleObject(threadHandle, INFINITE);

system("pause");
return 0;
}


Report •

Related Solutions

#4
March 16, 2011 at 06:31:24
Does this mean that you have to use a different synatx to program c++ for a unix platform as opposed to windows? I'm really new at this so forgive me if that seems a dull question.

Report •

#5
March 16, 2011 at 06:42:02
#include <windows.h>
What, you didn't think Linux would use Microsoft Windows header files, did you?

It might be kind of basic, but read this tutorial on POSIX pthreads. Preferably all of it, so we know you know the basic concepts of threading.

How To Ask Questions The Smart Way


Report •

#6
March 16, 2011 at 06:45:28
Sarcasm? Cheers for the tutorial.

Report •

#7
March 16, 2011 at 07:02:44
No need to get defensive. I know programming's learning curve is near vertical, but at some point in time, you need to look at the code and ask yourself, "What is this line? What does it do?"

In this case the answers are, "Imports a majority of the Microsoft Windows header files," and, "imports a majority of the Windows specific functions, typedefs, structures, and defines/macros (including CreateThread, Sleep, and WaitForSingleObject)."

How To Ask Questions The Smart Way


Report •

#8
March 16, 2011 at 09:09:22
It's not a question of different syntax, it's just what libraries you use. A great deal of what you see in a C++ program is not an integral part of the language but consists of calls to external libraries. Some of these libraries are portable across platforms but others, particularly those calling upon the lower-level OS functions, are not.

Believe me, it can be hard enough porting a program from one UNIX-like OS to another, let alone porting programs from Windows to Linux. Read the tutorial that Razor linked to and you'll learn a lot.

Look on the bright side. The only way that you really learn is to make mistakes; often you will learn more in correcting those mistakes than if things had gone right first time. It's a bonus really that things didn't work out flawlessly.

You'll have to forgive any perceived sarcasm in comments, including my original reply. Once you've been working with these things for a few years it's all too easy to forget how unobvious they are to a newcomer. I'm afraid that if you want to succeed as a programmer you have to develop a thick skin!


Report •

#9
March 16, 2011 at 09:32:03
I had posted a reply which said thanks and appologies for the knee jerk reaction but it (like several things involving human/computer interaction round me today) doesn't appear to have worked.

Thank you both for what is good advice.That tutorial is really very useful and will most certainly be reference fodder for quite a while to come.

Hope its as sunny wherever you people are as it is here. :)


Report •

#10
March 16, 2011 at 10:20:26
Good man! It's been a beautiful day here on the South Coast of England. :-)

Report •

Ask Question