LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-05-2004, 04:25 AM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 31
compiling with g++, gcc: basics


I am a way over my head in this one. I don't even know where to start.
I have started using linux about 3 weeks ago, and learning C++ about 6-7 weeks ago.

Untill today, I have been using Bloodshed Dev C++ compiler on my WinXP machine and never had too much trouble.

Yesterday I tried to compile using gcc. It gave me huge number of errors for a fairly simple exercise with operator overloading. So I said 'lets start with simple stuff and work it up to see where is the trouble'

Well I thought I started with pretty simple stuff. The function that gives me all the primes between 0 and a given number:

#include <iostream>
using std::cout;
using std::cin;

void Primes (unsigned int n)
{
bool isPrime;
for (register int num = 2; num <= n; ++num)
{
isPrime = true;
for (register int i = 2; i < num/2; ++i)
if (num%i == 0)
{
isPrime = false;
break;
}

if (isPrime)
cout << num << '\n';

}
}

int main (void)
{
int n;
cout<<"Enter a positive number\n";
cin>>n;
cout<<"Prime numbers between 1 and "<<n<<" are: ";
Primes(n);
return 0;
}

Doesn't get much simpler then that, I thought to myself.
But that's not what g++ thinks. While Dev C++ compiler executes program perfectly, g++ prints

2,3,4,5

REGARDLESS OF THE INPUT NUMBER N!
As I already mentioned, I am completely lost. Any input will be appreciated.
Thanks.
 
Old 10-05-2004, 05:29 AM   #2
SeriousMan
LQ Newbie
 
Registered: Sep 2004
Distribution: Slackware
Posts: 24

Rep: Reputation: 15
Strange enough. On my g++ (version 3.4.0) everything worked fine.
Please, post more info, especially about your gcc/g++ version.
 
Old 10-05-2004, 06:05 AM   #3
320mb
Senior Member
 
Registered: Nov 2002
Location: pikes peak
Distribution: Slackware, LFS
Posts: 2,577

Rep: Reputation: 48
Quote:
Originally posted by SeriousMan
Strange enough. On my g++ (version 3.4.0) everything worked fine.
Please, post more info, especially about your gcc/g++ version.
It worked fine on my box also...................I have version 3.2.3

Code:
g++ -o prime prime.c

I just changed name of C header to prime.....to protect the innocent..........LOL

Last edited by 320mb; 10-05-2004 at 06:06 AM.
 
Old 10-05-2004, 04:42 PM   #4
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 31
the version is 3.3.4
well, i was hoping something was wrong with the program itself. but now...what are the possible options? some libraries are uninstalled or something... i'm shooting in the dark here.
 
Old 10-06-2004, 02:44 PM   #5
laceupboots
Member
 
Registered: Dec 2003
Location: Houston
Distribution: Windows Vista/ XP/ Mepis
Posts: 304

Rep: Reputation: 30
Worked fine on my box too. I'm using version 3.3.1
g++ <filename.cpp> -o <programname>
./<programname>

Last edited by laceupboots; 10-06-2004 at 02:55 PM.
 
Old 10-06-2004, 02:58 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally posted by frankie_DJ
the version is 3.3.4
well, i was hoping something was wrong with the program itself. but now...what are the possible options? some libraries are uninstalled or something... i'm shooting in the dark here.
not too likely ... you'd be getting linker problems.

I'd rather check whether you have strange
values for CXX_FLAGS or something like
that (not that I'm aware of any that would
cause THAT result)... your code compiles
and runs just fine here, Slack-box with
identical g++ version.

What environment are you running the executable
under, just for good measure and to have covered
the possibility of output problems?


Cheers,
Tink
 
Old 10-07-2004, 08:18 AM   #7
laceupboots
Member
 
Registered: Dec 2003
Location: Houston
Distribution: Windows Vista/ XP/ Mepis
Posts: 304

Rep: Reputation: 30
Quote:
It gave me huge number of errors for a fairly simple exercise with operator overloading. So I said 'lets start with simple stuff and work it up to see where is the trouble'
What kind of errors are you getting? Can you post them? There has to be a discrepancy in the source code if you are getting the program to compile, but not run properly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Compiling using gcc Damitha Mandriva 1 12-17-2004 05:11 AM
Kernel compiling: gcc-3.3 is 586, should be gcc-3.3 386 Erik Plaggenmar Linux - Software 0 10-01-2004 11:38 AM
Various Compiling Errors (GCC compiling Openal, GUIlib, xmms-wma) gregorya Linux - Software 2 08-27-2004 05:03 AM
Compiling gcc CuteBug Linux - Software 2 02-22-2004 09:19 AM
Compiling C++ GCC poeta_boy Linux - Newbie 14 10-14-2003 04:23 PM

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

All times are GMT -5. The time now is 11:41 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration