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-27-2004, 07:04 AM   #1
m_suman
LQ Newbie
 
Registered: Mar 2004
Location: London
Posts: 3

Rep: Reputation: 0
how to compile C++ programs in linux


hello guys,

i have written a simple program in c++ to convert a byte to binary number but i do not understand what the complier is try to tell me. the program and error are below.if any one can understand them please mail me. Cheers!!

====================
C++ Program (cnvt.cc)
====================
#include <iostream>

void binary_op(char byte);

int main()
{
char byte=55;
binary_op(byte);
return 0;
}

void binary_op(char byte)
{
int count =8;
while(count--)
{
printf("%d",(byte&128) ? 1 : 0);
byte<<=1;
}
printf("\n");
}

here is the error when i have tried to compiled the program
the commad i used and the errors are showen below

suman@linux:~> gcc cnvt.cc
======
errors
======
/tmp/cc1RdLHm.o(.text+0xa0): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/cc1RdLHm.o(.text+0xcf): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/cc1RdLHm.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
 
Old 03-27-2004, 07:09 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
ok, you need to choose if your using c or c++.

to make it c replace '#include <iostream>' with '#include <stdio.h>' and rename to cnvt.c

to make it c++ replace '#include <iostream>' with '#include <cstdio>' add the line 'using namespace std;' after the #include and compile with g++ not gcc
 
Old 03-27-2004, 09:03 AM   #3
m_suman
LQ Newbie
 
Registered: Mar 2004
Location: London
Posts: 3

Original Poster
Rep: Reputation: 0
thank you for your suggestion. i made the changes and it worked.

it looks like the new GCC has quite a lot of changes added to it. Could you tell me if there is a data type for stroing binary numbers in GCC.
 
Old 03-27-2004, 09:41 AM   #4
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Numbers are infact stored binary in memory , by the way if you want to do some bit manipulations <<, >>, |, &, and ^ operators are useful.
 
Old 03-27-2004, 09:41 AM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
well all (signed integer)numbers are stored in two's complement binary notation so any of int, char, long, etc store the numbers in binary. if your actual question is - is there a function to convert a number to a base2(binary) string then there is, its called itoa but unfortunatly its not ansi c, i dont know if gcc supports it or not. its better to write your own version of itoa as its not very hard to do.
 
Old 03-27-2004, 09:46 AM   #6
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
The other way is to change your number into string (using itoa(), which is an ANSI C function, or sprintf()) and then strtoul() or strtol().
But that's too dirty. The best way is to use bitwise operators.
 
Old 03-27-2004, 01:12 PM   #7
m_suman
LQ Newbie
 
Registered: Mar 2004
Location: London
Posts: 3

Original Poster
Rep: Reputation: 0
hi mohsen
i do not think those function will work in GCC. but i will try them out. i think the best option will be to a customised function than relying on the inbuild ones.
 
Old 03-28-2004, 05:00 AM   #8
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Quote:
i think the best option will be to a customised function than relying on the inbuild ones
yes, btw those mentioned functions are ANSI and present in GCC (stdio and stdlib I think)
 
Old 03-28-2004, 06:21 AM   #9
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
itoa is not ANSI C, check this under portability, and according to a grep of my /usr/include its not in my glibc
 
Old 03-28-2004, 12:14 PM   #10
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
oh yes I mixed that with atoi
 
  


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
cannot compile c programs umeshpandey007 Linux - Newbie 7 08-29-2005 09:19 PM
Compile programs for PPC Muffe Linux - Software 2 08-17-2005 11:50 AM
what compile programs to install? provkitir Debian 7 12-29-2004 09:33 AM
Certain programs won't compile thegeekster Linux - Software 2 01-15-2004 06:25 AM
Can't compile gtk+ programs =\ EnigmaX Linux - Software 19 09-17-2003 05:41 PM

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

All times are GMT -5. The time now is 08:08 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