Using strtok in CodeWarrior - linker error??

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using strtok in CodeWarrior - linker error??

3,442 Views
MarcVDH
Contributor I
Hello,
I am trying to do some string manipulation in codewarrior C language.
All functions I use (like strcpy and strcat) compile and link fine.
strtok also compiles fine but generates an error in the linker.
I tried explicitly including <string.h> to no avail...
Is there anything else I need to do to make strtok work?
Labels (1)
0 Kudos
8 Replies

689 Views
vier_kuifjes
Senior Contributor I
This is the code I use. I appended the suffix "_mv" to some of the names to differentiate between the original strtok function names and those from my routine. But it should be functionally identical. At least, it works for me!:smileywink:

Code:
char * strtok_mv(char * str, const char * set){ static unsigned char * strtok_n_mv = (unsigned char *) ""; static unsigned char * strtok_s_mv = (unsigned char *) "";  unsigned char *p, *q;    int c;   unsigned char map[32]; for (c=0; c<32; c++)  map[c] = 0;  if (str)  strtok_s_mv = (unsigned char *) str;  p = (unsigned char *) set;  while (c = *p++)  set_char_map(map, c);  p = strtok_s_mv;  while (c = *p++)  if (!tst_char_map(map, c))   break;  if (!c) {  strtok_s_mv = strtok_n_mv;  return(NULL); }  q = p - 1;  while (c = *p++)  if (tst_char_map(map, c))   break;  if (!c)  strtok_s_mv = strtok_n_mv; else {  strtok_s_mv = p;  *--p = 0; } return((char *) q);}

 

0 Kudos

689 Views
alager
Contributor II
Code:
data = string_search(&data[0],'&',TRUE); //null terminate at the &data = string_search(&data[0],'=',FALSE); //point to the right hand side of the '='onoffbutton = data; char * string_search(char * data, const char delimitor, int terminate){ if(data != NULL) {  while (*data != delimitor){   data++;  }  if (terminate)   *data=0; //null terminate the string  data++;    return (data); } else {  return NULL; } }

 I ended up rolling my own.  It's actually a lot smaller than strtok(), and works fine for my application since I'm just parsing html headers.  When I need something more I'll try your code.
 There must have been something that I was missing, because it just didn't make sense why strtok didn't work.  Maybe it's the suffix that prevented the error in your version.

Thanks,
Aaron
0 Kudos

689 Views
MarcVDH
Contributor I
The CPU I'm using is the coldfire 52233 (on the little demo board).
I'm using CW6.4 for coldfire. (special edition)
0 Kudos

689 Views
CrasyCat
Specialist III
Hello
 
Not sure what is missing.
 
I did create a project from a Coldfire stationery for CF_M52223EVB.
I did plug in the code below in there:
 
Code:
#include <stdio.h>#include <string.h>char str[] = "now # is the time for all # good men to come to the # aid of their country";char delims[] = "#";   char *result = NULL;   int main(void){   printf("Hello World in C\n\r");    result = strtok( str, delims );      while( result != NULL ) {      printf( "result is \"%s\"\n", result );      result = strtok( NULL, delims );      }               fflush(stdout);  while(1) {  }}

 
I could compile, link and execute the code in the simulator
I might be missing something.
 
CrasyCat
0 Kudos

689 Views
alager
Contributor II
*update: nope that doesn't work either.  I keep getting some weird link error about "__clear" being referenced, but it isn't in the strtok code.

So back to the original question, how to get string.h to be included?

Aaron
0 Kudos

689 Views
MarcVDH
Contributor I
This example indeed works fine on my side too.
The reason why it doesn't work in my application is probably because my project, which is based on application note AN3518, does not include any libraries (the .a files). I read something about the strtok function being (multi)thread unsafe. I don't know much about that...

Anyway, I got it to work in a different manner. I extracted the strtok code from string.c and implemented that in my application. No need for libraries and it works fine!


Message Edited by MarcVDH on 2008-04-28 01:27 PM
0 Kudos

689 Views
alager
Contributor II
I'm having the same issue when using the tcp lite software.
I included stdio.h and string.h, but the linker can not find strtok.
I'm using CW 7.0 build 15
tcp lite version 2.0
I guess I can copy the c code out of the string.c file too, but what  a cludge!

Aaron
0 Kudos

689 Views
CrasyCat
Specialist III
Hello
 
- Which CPU are you targeting (MPC5xx, Coldfire, ..)
- Which version of CodeWarrior are you using?
To retrieve that info:
- Start CodeWarrior
- Select Help -> About Freescale CodeWarrior
- Click on "Install Products"
- CodeWarrior version used is displayed on top in the Installed Products dialog.
 
CrasyCat
0 Kudos