Quantcast

undefined reference to `fopen' and undefined reference to `strtok'

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

undefined reference to `fopen' and undefined reference to `strtok'

Jum -2

Hi,

 

I wrote a c program to read arrays from a txt file and then do a insertion  sort.  It runs OK while compiling in gcc.  However, I need to convert it to elf file by using ‘avr-gcc –o filename.elf –mmcu=atmega128 filename.c’ command. And then things happened as following:

 

jum@ubuntu-VirtualBox:~$ avr-gcc -o fget.elf -mmcu=atmega128 fget.c

fget.c: In function 'convert':

fget.c:29: warning: assignment makes pointer from integer without a cast

fget.c:36: warning: assignment makes pointer from integer without a cast

fget.c: In function 'main':

fget.c:73: warning: initialization makes pointer from integer without a cast

/tmp/ccWExtSv.o: In function `convert':

fget.c:(.text+0x124): undefined reference to `strtok'

fget.c:(.text+0x170): undefined reference to `strtok'

/tmp/ccWExtSv.o: In function `main':

fget.c:(.text+0x31c): undefined reference to `fopen'  

 

 

My C file is shown below like :

 

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#define LIST_SIZE 200

 

int fgetline(FILE *fp, char s[], int lim) {

    char *t;

    int c, len=lim;

 

    t = s;

    while (--lim>1 && (c=getc(fp)) != EOF && c != '\n')

        *s++ = c;

    if (c == '\n')

        *s++ = c;

    else if (lim == 1) {

                *s++ = '\n';

                fprintf(stderr, "WARNING. fgetline: Line too long, splitted.\n");

    }

    *s = '\0';

    return s - t;

}

 

 

  void convert(char* sData, int* num_list){

                char* pch;

                int i = 0;

        int v;

                pch = strtok(sData, " ");

                while(1)

                {

                                if(pch == NULL)

                                                break;

                                v = atoi(pch);

                                num_list[i++]=v;

                        pch = strtok( NULL, " ");

                }

   }

 

   void insertion_sort(FILE* fp , int * num_list){

              char sData[1000];

              int  value;

              int i, j,done;

                    

              fgetline(fp,sData,1000);

                      convert(sData, num_list);

 

                                for(i=1; i<LIST_SIZE; i++)

                                {

                                                value = num_list[i];        

                                                j = i - 1;

                                                done = 0;

                                   while (!done)

                                                {

                                                                if (value < num_list[j])

                                                                {

                                                                                num_list[j+1] = num_list[j];

                                                                                j--;

                                                                                if (j < 0)

                                                                                                done = 1;                                            

                                                                }

                                                                else

                                                                                done = 1;

                                                }

                                               

               

                                                num_list[j+1] = value;

                                }

}

 

int main(void){

                int i;      

        FILE* fp = fopen("200.txt", "r"); // there are 10K random permutations of 200 number list

       for(i=0;i<10000;i++)

                {

                int num_list[LIST_SIZE];       

                insertion_sort(fp,num_list);

                }

        asm volatile("break");

 

                return 1;

}

 

 

I will appreciate if someone could help me out from this.  Thanks a million

 

 

 

 

Best  Regards

 

Jum


_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: undefined reference to `fopen' and undefined reference to `strtok'

David Brown-4
Hi,

You are trying to use file IO calls that are dependent on an operating
system, such as Linux.  While it is certainly /possible/ to implement
them on an AVR, it is very unlikely to make sense.

If you want to do insertion sorts on a text file, use a PC.  If you want
to program an AVR, write microcontroller programs for it.

mvh.,

David



On 15/11/2011 14:07, Jum wrote:

> Hi,
>
> I wrote a c program to read arrays from a txt file and then do a
> insertion sort. It runs OK while compiling in gcc. However, I need to
> convert it to elf file by using ‘avr-gcc –o filename.elf –mmcu=atmega128
> filename.c’ command. And then things happened as following:
>
> jum@ubuntu-VirtualBox:~$ avr-gcc -o fget.elf -mmcu=atmega128 fget.c
>
> fget.c: In function 'convert':
>
> fget.c:29: warning: assignment makes pointer from integer without a cast
>
> fget.c:36: warning: assignment makes pointer from integer without a cast
>
> fget.c: In function 'main':
>
> fget.c:73: warning: initialization makes pointer from integer without a cast
>
> /tmp/ccWExtSv.o: In function `convert':
>
> fget.c:(.text+0x124): undefined reference to `strtok'
>
> fget.c:(.text+0x170): undefined reference to `strtok'
>
> /tmp/ccWExtSv.o: In function `main':
>
> fget.c:(.text+0x31c): undefined reference to `fopen'
>
> My C file is shown below like :
>
> #include <stdlib.h>
>
> #include <stdio.h>
>
> #include <string.h>
>
> #define LIST_SIZE 200
>
> int fgetline(FILE *fp, char s[], int lim) {
>
> char *t;
>
> int c, len=lim;
>
> t = s;
>
> while (--lim>1 && (c=getc(fp)) != EOF && c != '\n')
>
> *s++ = c;
>
> if (c == '\n')
>
> *s++ = c;
>
> else if (lim == 1) {
>
> *s++ = '\n';
>
> fprintf(stderr, "WARNING. fgetline: Line too long, splitted.\n");
>
> }
>
> *s = '\0';
>
> return s - t;
>
> }
>
> void convert(char* sData, int* num_list){
>
> char* pch;
>
> int i = 0;
>
> int v;
>
> pch = strtok(sData, " ");
>
> while(1)
>
> {
>
> if(pch == NULL)
>
> break;
>
> v = atoi(pch);
>
> num_list[i++]=v;
>
> pch = strtok( NULL, " ");
>
> }
>
> }
>
> void insertion_sort(FILE* fp , int * num_list){
>
> char sData[1000];
>
> int value;
>
> int i, j,done;
>
> fgetline(fp,sData,1000);
>
> convert(sData, num_list);
>
> for(i=1; i<LIST_SIZE; i++)
>
> {
>
> value = num_list[i];
>
> j = i - 1;
>
> done = 0;
>
> while (!done)
>
> {
>
> if (value < num_list[j])
>
> {
>
> num_list[j+1] = num_list[j];
>
> j--;
>
> if (j < 0)
>
> done = 1;
>
> }
>
> else
>
> done = 1;
>
> }
>
> num_list[j+1] = value;
>
> }
>
> }
>
> int main(void){
>
> int i;
>
> FILE* fp = fopen("200.txt", "r"); // there are 10K random permutations
> of 200 number list
>
> for(i=0;i<10000;i++)
>
> {
>
> int num_list[LIST_SIZE];
>
> insertion_sort(fp,num_list);
>
> }
>
> asm volatile("break");
>
> return 1;
>
> }
>
> Iwill appreciate if someone could help me out from this. Thanks a million
>
> Best Regards
>
> Jum
>
>
>
> _______________________________________________
> AVR-GCC-list mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/avr-gcc-list


_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: undefined reference to `fopen' and undefined reference to `strtok'

Georg-Johann Lay-2
In reply to this post by Jum -2
Jum schrieb:

> Hi,
>
> I wrote a c program to read arrays from a txt file and then do a insertion
> sort.  It runs OK while compiling in gcc.  However, I need to convert it to
> elf file by using 'avr-gcc -o filename.elf -mmcu=atmega128 filename.c'
> command. And then things happened as following:
>
> jum@ubuntu-VirtualBox:~$ avr-gcc -o fget.elf -mmcu=atmega128 fget.c
>
> fget.c: In function 'convert':
> fget.c:29: warning: assignment makes pointer from integer without a cast
> fget.c:36: warning: assignment makes pointer from integer without a cast
> fget.c: In function 'main':
> fget.c:73: warning: initialization makes pointer from integer without a cast
> /tmp/ccWExtSv.o: In function `convert':
> fget.c:(.text+0x124): undefined reference to `strtok'
> fget.c:(.text+0x170): undefined reference to `strtok'
> /tmp/ccWExtSv.o: In function `main':
> fget.c:(.text+0x31c): undefined reference to `fopen'  

strtok is implemented in libc. Thus you need a libc to link against.
Common libc to cooperate with avr-tools are avr-libc or newlib.

For avr-libc, see
http://nongnu.org/avr-libc/user-manual/group__avr__string.html

> Best  Regards
>
> Jum

Johann

_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Loading...