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...
gcc -Wall -Wextra -pedantic -std=gnu11 -Ofast -o libpngex libpngex.c -lm -lpng
and then run./libpngex output.png
– David C. Rankin Oct 13 '18 at 20:19inline
keyword is just a "hint" to the compiler that it can inline the function. There is no guarantee it does it. But, you must be consistent between the function declaration and function definition. You cannot haveinline
on one and not the other -- they will be seen as two different functions. – David C. Rankin Oct 13 '18 at 20:22libpngex.c
. Just use yourmakePNG
instead, e.g.gcc -Wall -Wextra -pedantic -std=gnu11 -Ofast -o makePNG -lm -lpng makePNG.c
And then run./makePNG somepng.png
– David C. Rankin Oct 13 '18 at 20:32gcc -Wall -Wextra -pedantic -std=gnu11 -Ofast -o libpngex makePNG.c -lm -lpng makePNG.c
. Get rid of one of themakePNG.c
in there and your output will be inlibpngex
or get rid oflibpngex
and remove the'.c'
from the firstmakePNG.c
. – David C. Rankin Oct 13 '18 at 20:34gcc -Wall -Wextra -pedantic -std=gnu11 -Ofast -o libpngex makePNG.c -lm -lpng makePNG.c
you includedmakePNG.c
twice. That is what led to the‘code’ might be clobbered...
warnings. – David C. Rankin Oct 13 '18 at 20:39