Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
/usr/bin/ld: /tmp/cc5LfrxC.o: in function `main':
100-main.c:(.text+0x14): undefined reference to `is_palindrome'
/usr/bin/ld: 100-main.c:(.text+0x39): undefined reference to `is_palindrome'
/usr/bin/ld: 100-main.c:(.text+0x5e): undefined reference to `is_palindrome'
/usr/bin/ld: 100-main.c:(.text+0x83): undefined reference to `is_palindrome'
collect2: error: ld returned 1 exit status
root@d


Code that produced the error -

C
/**
* main - check the code
*
* Return: Always 0.
*/
int main(void)
{
int r;

r = is_palindrome("level");
printf("%d\n", r);
r = is_palindrome("redder");
printf("%d\n", r);
r = is_palindrome("test");
printf("%d\n", r);
r = is_palindrome("step on no pets");
printf("%d\n", r);
return (0);
}


What I have tried:

i have delete it several times and redo it, i have tried bettycode, bit it says space at the start, i have tried gcc and is giving me this undefined reference.
i cant seems to understand what i did wrong
Posted
Updated 6-Jul-23 20:22pm
v6
Comments
Mike Hankey 6-Jul-23 11:03am    
Without the code that produced the error we are not able to help you.
Claire Tochi 6-Jul-23 11:10am    
i will present the code just now

#include "main.h"
#include <stdio.h>

/**
* main - check the code
*
* Return: Always 0.
*/
int main(void)
{
int r;

r = is_palindrome("level");
printf("%d\n", r);
r = is_palindrome("redder");
printf("%d\n", r);
r = is_palindrome("test");
printf("%d\n", r);
r = is_palindrome("step on no pets");
printf("%d\n", r);
return (0);
}
Claire Tochi 6-Jul-23 11:11am    
i have produce the @Mike Hankey, please review
Patrice T 6-Jul-23 12:47pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Dave Kreskowiak 6-Jul-23 13:10pm    
Like I said in my solution below, you don't have a function called "is_palindrome" defined. You're calling a function that doesn't exist in your code.

You have to define and write the code for the "is_palindrome" function.

In the posted code is missing:
  • The inclusion of the stdio.h header.
  • The declaration of the is_palindrome function.
  • The definition of the is_palindrome function.

Therefore, if you try to compile it, as it stands, the compiler will output something like
test.c: In function ‘main’:
test.c:6:6: warning: implicit declaration of function ‘is_palindrome’ [-Wimplicit-function-declaration]
    6 |  r = is_palindrome("level");
      |      ^~~~~~~~~~~~~
test.c:7:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
    7 |  printf("%d\n", r);
      |  ^~~~~~
test.c:7:2: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
  +++ |+#include <stdio.h>
    1 |                                                                                                                                                                                                  
/usr/bin/ld: /tmp/ccqqE0qs.o: in function `main':                                                                                                                                                        
test.c:(.text+0x19): undefined reference to `is_palindrome'                                                                                                                                              
/usr/bin/ld: test.c:(.text+0x43): undefined reference to `is_palindrome'                                                                                                                                 
/usr/bin/ld: test.c:(.text+0x6d): undefined reference to `is_palindrome'                                                                                                                                 
/usr/bin/ld: test.c:(.text+0x97): undefined reference to `is_palindrome'                                                                                                                                 
collect2: error: ld returned 1 exit status  


If you provide (together with stdio.h header inclusion) the is_palindrome function declaration
C
#include <stdio.h>

int is_palindrome(const char * ); // 'is_palindrome' function declaration

int main(void)
{
  int r;

  r = is_palindrome("level");
  printf("%d\n", r);
  r = is_palindrome("redder");
  printf("%d\n", r);
  r = is_palindrome("test");
  printf("%d\n", r);
  r = is_palindrome("step on no pets");
  printf("%d\n", r);
  return (0);
}
Then the compiler will output:
/usr/bin/ld: /tmp/ccelevUP.o: in function `main':                                                                                                                                                        
test.c:(.text+0x14): undefined reference to `is_palindrome'                                                                                                                                              
/usr/bin/ld: test.c:(.text+0x39): undefined reference to `is_palindrome'
/usr/bin/ld: test.c:(.text+0x5e): undefined reference to `is_palindrome'
/usr/bin/ld: test.c:(.text+0x83): undefined reference to `is_palindrome'
collect2: error: ld returned 1 exit status
These linker errors are telling you the defintion of the is_palindrome is missing.


Eventually the following piece of code, providing both the is_palindrome declaration and definition will happily compile:
C
#include <stdio.h>

int is_palindrome(const char * ); // 'is_palindrome' function declaration

int main(void)
{
  int r;

  r = is_palindrome("level");
  printf("%d\n", r);
  r = is_palindrome("redder");
  printf("%d\n", r);
  r = is_palindrome("test");
  printf("%d\n", r);
  r = is_palindrome("step on no pets");
  printf("%d\n", r);
  return (0);
}

int is_palindrome(const char * s ) // 'is_palindrome' function definition
{
  // TODO: check if the passed string is palindrome, and return 1 if it is.

 return 0; // that is: 'not palindrome'
}
 
Share this answer
 
First, that's not C#. That's plain old C.

You're trying to use a function that hasn't been defined in the code yet. In C, a function has to be defined, or at least forward declared, before the compiler comes across code that calls that function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900