Reputation: 780
I was trying out this libPNG example, but it failed at compilation. I used gcc -lm -lpng makePNG.c
to compile it and got the following error:
/tmp/ccgGO8zw.o: In function `writeImage':
makePNG.c:(.text+0x360): undefined reference to `setRGB'
collect2: error: ld returned 1 exit status
I removed the function definitions and simply moved the functions so that they were in the following order:
void setRGB(png_byte *ptr, float val)
int writeImage(char* filename, int width, int height, float *buffer, char* title)
float *createMandelbrotImage(int width, int height, float xS, float yS, float rad, int maxIteration)
int main(int argc, char *argv[])
And it worked. My question is: why didn't it work before? SetRGB was defined prior to writeImage, so how could the 'setRGB' reference be undefined?
Edit:
I forgot to mention something that I now realize is very important. I removed the 'inline' keyword from the setRGB function. I tried compiling with the inline keyword and it had the same error message. So clearly my issue has to do with the inline keyword and not the forward declarations like I originally thought...
Upvotes: 1
Views: 1497
Reputation: 780
Thanks to David C. Rankin I have found a command that works to compile this. gcc -Ofast -lm -lpng makePNG.c
works because it includes the -Ofast option. This is the description of that option in man gcc
:
-Ofast
Disregard strict standards compliance.
-Ofast enables all -O3 optimizations. It also enables optimizations that are not valid for all standard-compliant programs. It turns on -ffast-math and the Fortran-specific -fno-protect-parens and -fstack-arrays.
I suspect the way the author uses inline does not comply with the c specification, and that's why it won't compile without this option. Needless to say it is never OK to write code that doesn't comply with the c standards.
Edit:
Proof that the -Ofast option is the culprit:
$ gcc -Ofast -lm -lpng makePNG.c
$ gcc -lm -lpng makePNG.c
/tmp/cc2JlymP.o: In function `writeImage':
makePNG.c:(.text+0x360): undefined reference to `setRGB'
collect2: error: ld returned 1 exit status
$
Upvotes: 1