Home > OS >  How do I properly link math.h library in makefiles?
How do I properly link math.h library in makefiles?

Time:01-18

I need to plot a function with gnuplot. This function requires the math.h library. Now I want to run it with a makefile.

When I run the makefile I get this output/error:

gcc -I./inc -o./build/result -lm ./src/main.c ./src/gnuplot.c
/usr/bin/ld: /tmp/ccwsiOjK.o: in function `plot_y':
main.c:(.text 0x64): undefined reference to `exp'
/usr/bin/ld: main.c:(.text 0xbc): undefined reference to `pow'
/usr/bin/ld: main.c:(.text 0x121): undefined reference to `exp'
collect2: error: ld returned 1 exit status
make: *** [makefile:5: all] Fehler 1

I think I didn't link the math.h library properly. The makefile:

run: all 
    ./build/result

all: build
    gcc -I./inc -o./build/result -lm ./src/main.c ./src/gnuplot.c
#-I./inc damit der compiler nicht nur im src Ordner sucht
build: 
    mkdir build

clean:
    rm -f -r build


CodePudding user response:

The order matters. First list the .c or .o files, then the external libraries.

gcc -I./inc -o./build/result ./src/main.c ./src/gnuplot.c -lm
  • Related