0

I have been trying to compile someone else's C code myself (GitHub Link). Previously, I had been compiling it with a Makefile provided with the repository. This Makefile is incredibly long and difficult for me to understand, so I instead viewed the GCC commands the Makefile outputted to the terminal:

gcc -DPACKAGE_NAME=\"cartogram\" -DPACKAGE_TARNAME=\"cartogram\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"cartogram\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"cartogram\" -DVERSION=\"1.0\" -DHAVE_LIBM=1 -I.    -O3 -Wall -fopenmp  -I/usr/local/include -I/usr/local/include/cjson  -MT cartogram-cartogram.o -MD -MP -MF .deps/cartogram-cartogram.Tpo -c -o cartogram-cartogram.o `test -f 'cartogram.c' || echo './'`cartogram.c
mv -f .deps/cartogram-cartogram.Tpo .deps/cartogram-cartogram.Po
gcc -DPACKAGE_NAME=\"cartogram\" -DPACKAGE_TARNAME=\"cartogram\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"cartogram\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"cartogram\" -DVERSION=\"1.0\" -DHAVE_LIBM=1 -I.    -O3 -Wall -fopenmp  -I/usr/local/include -I/usr/local/include/cjson  -MT cartogram-diff_integrate.o -MD -MP -MF .deps/cartogram-diff_integrate.Tpo -c -o cartogram-diff_integrate.o `test -f 'diff_integrate.c' || echo './'`diff_integrate.c
mv -f .deps/cartogram-diff_integrate.Tpo .deps/cartogram-diff_integrate.Po
gcc -DPACKAGE_NAME=\"cartogram\" -DPACKAGE_TARNAME=\"cartogram\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"cartogram\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"cartogram\" -DVERSION=\"1.0\" -DHAVE_LIBM=1 -I.    -O3 -Wall -fopenmp  -I/usr/local/include -I/usr/local/include/cjson  -MT cartogram-ffb_integrate.o -MD -MP -MF .deps/cartogram-ffb_integrate.Tpo -c -o cartogram-ffb_integrate.o `test -f 'ffb_integrate.c' || echo './'`ffb_integrate.c
mv -f .deps/cartogram-ffb_integrate.Tpo .deps/cartogram-ffb_integrate.Po
gcc -DPACKAGE_NAME=\"cartogram\" -DPACKAGE_TARNAME=\"cartogram\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"cartogram\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"cartogram\" -DVERSION=\"1.0\" -DHAVE_LIBM=1 -I.    -O3 -Wall -fopenmp  -I/usr/local/include -I/usr/local/include/cjson  -MT cartogram-fill_with_density.o -MD -MP -MF .deps/cartogram-fill_with_density.Tpo -c -o cartogram-fill_with_density.o `test -f 'fill_with_density.c' || echo './'`fill_with_density.c
mv -f .deps/cartogram-fill_with_density.Tpo .deps/cartogram-fill_with_density.Po
gcc -DPACKAGE_NAME=\"cartogram\" -DPACKAGE_TARNAME=\"cartogram\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"cartogram\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"cartogram\" -DVERSION=\"1.0\" -DHAVE_LIBM=1 -I.    -O3 -Wall -fopenmp  -I/usr/local/include -I/usr/local/include/cjson  -MT cartogram-main.o -MD -MP -MF .deps/cartogram-main.Tpo -c -o cartogram-main.o `test -f 'main.c' || echo './'`main.c
mv -f .deps/cartogram-main.Tpo .deps/cartogram-main.Po
gcc -DPACKAGE_NAME=\"cartogram\" -DPACKAGE_TARNAME=\"cartogram\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"cartogram\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"cartogram\" -DVERSION=\"1.0\" -DHAVE_LIBM=1 -I.    -O3 -Wall -fopenmp  -I/usr/local/include -I/usr/local/include/cjson  -MT cartogram-ps_figure.o -MD -MP -MF .deps/cartogram-ps_figure.Tpo -c -o cartogram-ps_figure.o `test -f 'ps_figure.c' || echo './'`ps_figure.c
mv -f .deps/cartogram-ps_figure.Tpo .deps/cartogram-ps_figure.Po
gcc -DPACKAGE_NAME=\"cartogram\" -DPACKAGE_TARNAME=\"cartogram\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"cartogram\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"cartogram\" -DVERSION=\"1.0\" -DHAVE_LIBM=1 -I.    -O3 -Wall -fopenmp  -I/usr/local/include -I/usr/local/include/cjson  -MT cartogram-read_map.o -MD -MP -MF .deps/cartogram-read_map.Tpo -c -o cartogram-read_map.o `test -f 'read_map.c' || echo './'`read_map.c
mv -f .deps/cartogram-read_map.Tpo .deps/cartogram-read_map.Po
gcc -DPACKAGE_NAME=\"cartogram\" -DPACKAGE_TARNAME=\"cartogram\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"cartogram\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"cartogram\" -DVERSION=\"1.0\" -DHAVE_LIBM=1 -I.    -O3 -Wall -fopenmp  -I/usr/local/include -I/usr/local/include/cjson  -MT cartogram-process_json.o -MD -MP -MF .deps/cartogram-process_json.Tpo -c -o cartogram-process_json.o `test -f 'process_json.c' || echo './'`process_json.c
mv -f .deps/cartogram-process_json.Tpo .deps/cartogram-process_json.Po
rm -f cartogram
gcc -O3 -Wall -fopenmp  -I/usr/local/include -I/usr/local/include/cjson    -o cartogram cartogram-cartogram.o cartogram-diff_integrate.o cartogram-ffb_integrate.o cartogram-fill_with_density.o cartogram-main.o cartogram-ps_figure.o cartogram-read_map.o cartogram-process_json.o -fopenmp -lfftw3 -L/usr/local/lib -lcjson -lm 

If I copy this to a shell script and run that, it compiles the program with no issues. This tells me that, theoretically, these commands are all I need to compile the program. Also, I don't quite understand what all of those "DPACKAGE" flags are doing, so please explain that if you know. I'm assuming those are important, since they are the primary difference between these compilation directives and the ones that I created:

gcc  -I.. -I/usr/local/include -I/usr/local/include/cjson -fopenmp -MP -MD -c -o process_json.o ../process_json.c
gcc  -I.. -I/usr/local/include -I/usr/local/include/cjson -fopenmp -MP -MD -c -o ffb_integrate.o ../ffb_integrate.c
gcc  -I.. -I/usr/local/include -I/usr/local/include/cjson -fopenmp -MP -MD -c -o fill_with_density.o ../fill_with_density.c
gcc  -I.. -I/usr/local/include -I/usr/local/include/cjson -fopenmp -MP -MD -c -o cartogram.o ../cartogram.c
gcc  -I.. -I/usr/local/include -I/usr/local/include/cjson -fopenmp -MP -MD -c -o main.o ../main.c
gcc  -I.. -I/usr/local/include -I/usr/local/include/cjson -fopenmp -MP -MD -c -o read_map.o ../read_map.c
gcc  -I.. -I/usr/local/include -I/usr/local/include/cjson -fopenmp -MP -MD -c -o ps_figure.o ../ps_figure.c
gcc  -I.. -I/usr/local/include -I/usr/local/include/cjson -fopenmp -MP -MD -c -o diff_integrate.o ../diff_integrate.c
gcc  -I.. -I/usr/local/include -I/usr/local/include/cjson -fopenmp -L/usr/local/lib -lcjson -lm -lfftw3 -o ./cartogram_generator.out process_json.o ffb_integrate.o fill_with_density.o cartogram.o main.o read_map.o ps_figure.o diff_integrate.o

I tried to avoid using the "DPACKAGE" directives in mine because I don't exactly know what they do - ChatGPT (yes, I used it) told me they were "preprocessor definitions", but I can't seem to find the preprocessor directives they refer to in the include headers or the source files. All I know is that the first set of commands compile the program, but this one doesn't. My own commands generate a linker error upon compilation. All functions from the header files math.h, fftw3.h, and cjson/cJSON.h generate an undefined reference linker error.

Following up on this, I have 2 main questions: Why does the first set of commands compile the program while mine doesn't?; what do all the "DPACKAGE" and "M*" flags in the first script mean?

AbeMonk
  • 110
  • 7
  • 6
    You generally want to link the libraries after the other .o files. Move all of the `-l whatever` parts to the end of the command line. The linker generally only links what it needs when processing a library, so if the code that has dependencies isn't processed first the dependencies aren't known. – Retired Ninja May 23 '23 at 01:58
  • 1
    Alright, yeah, that worked (your first comment). Can't believe I didn't know that. What a stupid mistake. Thanks for the help! – AbeMonk May 23 '23 at 02:01
  • See [Undefined reference to … linker command option order and libraries](https://stackoverflow.com/q/9253200/15168) – Jonathan Leffler May 23 '23 at 04:04

0 Answers0