g++ 3.2.X: undefined reference to `abs(int)'

D

Daniel Heiserer

hi,

I am sure that is a stupid error of mine, but why the hell
does this not work:
-------------------------------------------
g++ test.cc
/tmp/ccUN1Hel.o: In function `main':
/tmp/ccUN1Hel.o(.text+0x26): undefined reference to `abs(int)'
collect2: ld returned 1 exit status
-------------------------------------------
cat test.cc
#include <stdlib.h>
#include <stdio.h>

int main(){
int abs(int);
for (int jj=10;jj>=-10;jj--){
fprintf(stdout,"%d ===> %d\n",jj,abs(jj));
}
return 0;
}
-------------------------------------------
man 3 abs
ABS(3) Linux Programmer's
Manual ABS(3)
NAME
abs, labs, llabs, imaxabs - compute the absolute value of an
integer.
SYNOPSIS
#include <stdlib.h>

int abs(int j);
long int labs(long int j);
long long int llabs(long long int j);
 
A

Attila Feher

Daniel said:
hi,

I am sure that is a stupid error of mine, but why the hell
does this not work:
-------------------------------------------
/tmp/ccUN1Hel.o: In function `main':
/tmp/ccUN1Hel.o(.text+0x26): undefined reference to `abs(int)'
collect2: ld returned 1 exit status
-------------------------------------------
#include <stdlib.h>
#include <stdio.h>

int main(){
int abs(int);

You don't need the above line. <stdlib.h> declares abs so you must not do
it.
 
C

Chris Dams

Hello,
#include <stdlib.h>
#include <stdio.h>

Should nowadays be:

#include <cstdlib>
#include <cstdio>

using namespace std;
int main(){
int abs(int);

Remove this line, that will solve the error.
for (int jj=10;jj>=-10;jj--){
fprintf(stdout,"%d ===> %d\n",jj,abs(jj));
}
return 0;
}

Bye,
Chris Dams
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top