LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-28-2013, 05:25 AM   #1
pankaj8332
LQ Newbie
 
Registered: Oct 2013
Posts: 2

Rep: Reputation: Disabled
compilation error: text+0x1e undefined refrence to a function


i tried to compile this program which does a conversion of a string of characters form ascii to binary .
i dont understand how to fix this last error .



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* asciitobinary(char* a);
int main(int argc ,char* argv[])
{
char* b;
b= asciitobinary(argv[1]);
printf("\n%s\n",b);
return 0;

}

char* asiitobinary(char* a)
{
char* b;
int len,j=0,k=0,i;
len=strlen(a);
for(i=0;i<len;i++)
{
for(k=j+7,j=j+7;k>=j;k--)
{

if(a[i]>1)
{
b[k]=a[i]%2;
a[i]=(a[i]-b[k])/2;
}
else
b[k]=0;
}

}
b[k]='\0';
return b;
}



error is this :
/tmp/cc4P9tCC.o: In function `main':
assignment.c.text+0x1e): undefined reference to `asciitobinary'
collect2: error: ld returned 1 exit status
 
Old 10-28-2013, 05:28 AM   #2
pankaj8332
LQ Newbie
 
Registered: Oct 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pankaj8332 View Post
i tried to compile this program which does a conversion of a string of characters form ascii to binary .
i dont understand how to fix this last error .



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* asciitobinary(char* a);
int main(int argc ,char* argv[])
{
char* b;
b= asciitobinary(argv[1]);
printf("\n%s\n",b);
return 0;

}

char* asiitobinary(char* a)
{
char* b;
int len,j=0,k=0,i;
len=strlen(a);
for(i=0;i<len;i++)
{
for(k=j+7,j=j+7;k>=j;k--)
{

if(a[i]>1)
{
b[k]=a[i]%2;
a[i]=(a[i]-b[k])/2;
}
else
b[k]=0;
}

}
b[k]='\0';
return b;
}



error is this :
/tmp/cc4P9tCC.o: In function `main':
assignment.c.text+0x1e): undefined reference to `asciitobinary'
collect2: error: ld returned 1 exit status
actually it compiled there was spelling mistakke
but not working
it is saying
segmentation fault core dumped
 
Old 10-28-2013, 09:07 AM   #3
GianfrancoD
LQ Newbie
 
Registered: Oct 2013
Location: Milan (Italy)
Distribution: Fedora Client / Centos Server
Posts: 1

Rep: Reputation: Disabled
Which is the size of char* b in asciitobinary() ?

Last edited by GianfrancoD; 10-28-2013 at 09:09 AM.
 
Old 10-28-2013, 09:18 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by GianfrancoD View Post
Which is the size of char* b in asciitobinary() ?
You are probably hinting at the correct issue, but I think your hint is too subtle to be understood by someone who would make the original error.

Quote:
Originally Posted by pankaj8332 View Post
but not working
Pointers in C do not point anywhere usable unless your code makes them point somewhere usable.

If you just declare
char* b
it doesn't point to usable memory.

Quote:
for(k=j+7,j=j+7;k>=j;k--)
You also might want to think through the sequence of actions specified by that line.

Last edited by johnsfine; 10-28-2013 at 09:22 AM.
 
Old 10-28-2013, 09:35 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
also you may try to use debug info (compile with -g). in that case you will get much better error messages (instead of: assignment.c.text+0x1e)
 
Old 10-28-2013, 09:40 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by pan64 View Post
also you may try to use debug info (compile with -g). in that case you will get much better error messages (instead of: assignment.c.text+0x1e)
You missed a change in the question. That original unclear error message was at link time, not run time and debug info would not have helped. That error was due to a typo the OP found and corrected without waiting from help from this thread.

The seg fault seems to be due to a basic misunderstanding of pointers in C. So debugging, to narrow down the moment at which the seg fault occurs, probably would not help.
 
  


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
Error in compilation : (.text+0x3e2): undefined reference to `elf_setshstrndx' fright Programming 4 11-08-2012 08:38 AM
undefined refrence while reverse dependency in shared library.. er_khatke Linux - Newbie 1 08-25-2009 04:24 AM
'Undefined refrence to main error' in winavr gcc compiler???? zack670303 Programming 1 02-26-2009 12:47 PM
undefined refrence return status link error azucarmom Programming 7 03-30-2005 11:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:42 AM.

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