-1

I'm modifing a c project and is the first time I'm tryng to use the stdlib.h and string.h libraries in my project. I'm working on MCUXpresso (IDE based on eclipse). This is my code:

#include <string.h>
#include <stdlib.h>
#include "config.h"

int number=100;
int n1,n2;
char test[5]="test";
char str[5];

extern void fntest(TTASKTABLE *ptrTaskTable)
{
    itoa (number,str,10);
    n1=strlen(test);
    n2=atoi(test);
}

As you can see I've included the header files but the compiler give the errors: undefined reference to 'itoa' ; undefined reference to 'strlen' ; undefined reference to 'atoi' And in my includes folder there is already (by default on my project) the folder containing the standard libraries. I see the functions are used in some other files in the project... I can't understand why I've this error. In the original code the functions are in a body function, I've corrected that. Can you help me please?

  • It's hard to tell withoput seeing the exact error messages,but the problem may be that you're not linking in the correct libraries. (That is, it may have nothing to do with the header files.) Look at those other projects and see if they have some libraries specified to link against, that you don't. – Steve Summit May 04 '18 at 12:36
  • BTW `itoa` is a non standard function, but `strlen` definitely is. It's probably a linker problem. – Jabberwocky May 04 '18 at 12:42
  • Is that your code in its entirety? Like it is all out side of a function and there is no main? – afic May 04 '18 at 12:47
  • 1
    It's a common beginner's misconception that a header would provide some functionality. It does not. A header only provides declarations. It tells your compiler how the functions have to be used. You still need to link the implementation of this functionality to your code. This step seems to be missing in your code. – Gerhardh May 04 '18 at 12:48
  • There is no standard function called `itoa`. It is a non-standard library extension. – Lundin May 04 '18 at 13:07
  • 2
    Never use the atoi family of functions, there is never any reason to do so - use strtol instead. See [What are the functions from the standard library that must/should be avoided?](https://stackoverflow.com/a/46563868/584518). – Lundin May 04 '18 at 13:08
  • I've edited the post putting the functions in a body function as is in the original code, sorry about that. – Simone Del Gobbo May 04 '18 at 17:15
  • @Gerhardh How can I link the implementation of this functionality to my code? – Simone Del Gobbo May 04 '18 at 17:16
  • I am not familiar with your tool chain. You need to browse your compiler manual for correct invocation of compiler and linker. – Gerhardh May 04 '18 at 18:03
  • @Gerhardh ok, I will try. What do you think about the other files in the project that use those functions? In those files are just included the stdlib.h and string.h libraries and everything seems work properly – Simone Del Gobbo May 04 '18 at 20:43

2 Answers2

1

I don't know if you have posted your actual code, but at this stage as you have uploaded its not going to compile.

#include <string.h>
#include <stdlib.h>
#include "config.h"

int number=100;        // This is okay
int n1, n2;            // This is okay
char test[5]="test";   // This is okay
char str[5];           // This is okay

itoa (number,str,10);  // This is wrong
n1=strlen(test);       // This is wrong
n2=atoi(test);         // This is wrong

Whatever that I have appended with a comment // This is wrong is because, these needs to be in a function body.

Now since you already stated that,

I see the functions are used in some other files in the project..

I don't have to provide you with a way to implement itoa function.

Update: Since now that you have added it into a function, the point that I talked about is fixed. Now, it should ideally compile, provided that there a valid definition of a itoa function in one of the header files that have been included.

WedaPashi
  • 3,561
  • 26
  • 42
1

finally I've solved my problem ! Ijust had to change the linker settings: project-->properties-->C/C++ Build-->settings-->MCU Linker-->general and change form No startup or default libs to Do not use standard start files and then no more errors!