How do I properly link math.h library in makefiles? - c

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

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

using .rodata and -fPIE in compliation [duplicate]

This question already has an answer here:
32-bit absolute addresses no longer allowed in x86-64 Linux?
(1 answer)
Closed last month.
I'm a student currently taking a computer structures course and I have an assignement for writing in aseembly and C. I've found an issue I don't quite understand. We were given a makefile for running the program, and when connecting to our school's servers it works as intended. The issue arises when trying to use the same makefile on my personal machine. One of the assembly files (and likely more as I continue working) utilizes a .rodata section. Attempting to use the makefile as it was given results in the following error:
gcc -g -o a.out main.o run_main.o func_select.o pstring.o
/usr/bin/ld: func_select.o: relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
make: *** [makefile:2: a.out] Error 1
attempting to add one of the following
-fpie -fPIE or -no-pie
in between
-o
and a.out
results in the following error
gcc -g -o -fPIE a.out main.o run_main.o func_select.o pstring.o
/usr/bin/ld: cannot find a.out: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [makefile:2: a.out] Error 1
I'm sorry if this is considered a silly question however I can't figuere it out.
The expectation was for the makefile to create all of the .o files and a.out, however the a.out file never works.
In addition writing the following: gcc -g -fPIE -o a.out main.o run_main.o func_select.o pstring.o still creates an error, specifically
gcc -g -fPIE -o a.out main.o run_main.o func_select.o pstring.o
/usr/bin/ld: func_select.o: relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
make: *** [makefile:2: a.out] Error 1
As per man gcc:
gcc [-c|-S|-E] [-std=standard]
[-g] [-pg] [-Olevel]
[-Wwarn...] [-Wpedantic]
[-Idir...] [-Ldir...]
[-Dmacro[=defn]...] [-Umacro]
[-foption...] [-mmachine-option...]
[-o outfile] [#file] infile...
-o requires outfile parameter. This should work:
gcc -g -fPIE -o a.out main.o run_main.o func_select.o pstring.o

mcsema using error,has someone use mcsema successful

I flow the Official website to build mcsema using native build approach.Everything seems OK.Since my ida is on Windows,so I use the command of
%IDA_ROOT%\ida64.exe -S"C:\Users\dkl\Desktop\source\mcsema\tools\mcsema_disass\ida7\get_cfg.py --output
C:\Users\dkl\Desktop\output.cfg --arch amd64 --os linux" C:\Users\dkl\Desktop\work\t2\a.out
to generate output.cfg ,that is OK
then
mcsema-lift-11.0 --arch amd64 --os linux --cfg ./output.cfg --output ./output.bc
that is OK,then
remill-clang-11 -o ./output ./output.bc -lpthread -lm -ldl -llzma -Wl,--section-start=.section_1ff00000=0x1ff00000
COUNTER A ERROR
root#DESKTOP-CS6BMJI:/mnt/c/Users/dkl/Desktop# mcsema-lift-11.0 --arch amd64 --os linux --cfg ./output.cfg --output ./output.bc
root#DESKTOP-CS6BMJI:/mnt/c/Users/dkl/Desktop# remill-clang-11 -o ./output ./output.bc -lpthread -lm -ldl -llzma -Wl,--section-start=.section_1ff00000=0x1ff00000
warning: overriding the module target triple with x86_64-unknown-linux-gnu [-Woverride-module]
1 warning generated.
/usr/bin/ld: /tmp/output-3e1099.o: in function sub_401020': llvm-link:(.text+0x152): undefined reference to __remill_jump'
/usr/bin/ld: /tmp/output-3e1099.o: in function sub_401040__start': llvm-link:(.text+0x2cb): undefined reference to __remill_function_call'
/usr/bin/ld: /tmp/output-3e1099.o: in function sub_401160___libc_csu_init': llvm-link:(.text+0x939): undefined reference to __remill_function_call'
/usr/bin/ld: /tmp/output-3e1099.o: in function sub_401000__init_proc': llvm-link:(.text+0xe2d): undefined reference to __remill_function_call'
/usr/bin/ld: /tmp/output-3e1099.o: in function ext_404040_printf': llvm-link:(.text+0x10da): undefined reference to __remill_function_call'
/usr/bin/ld: /tmp/output-3e1099.o:(.rodata+0x2138): undefined reference to `__mcsema_attach_call'
remill-clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
root#DESKTOP-CS6BMJI:/mnt/c/Users/dkl/Desktop#

how to compile netcdf

Hello I am using given code from the examples to test if I have installed netcdf correctly. I installed the prebuilt netCDF4.8.1-NC4-DAP-64 and put it in the environment variable path (I checked to make sure it worked, it did. Next I did #include "netcdf.h" in the main c file and then made the make file as such:
CC = gcc
EXECUTABLES = try
INCLUDES = -IC:/Program\ Files/netCDF\ 4.8.1/include/
CFLAGS = $(INCLUDES)
LIBS = -LC:/Program\ Files/netCDF\ 4.8.1/lib -lnetcdf -lmfhdf -ljpeg -lhdf5_hl -lhdf5 -lz -lm
all: $(EXECUTABLES)
try: try.c
$(CC) $(CFLAGS) -o try try.c $(LIBS)
clean:
rm core $(EXECUTABLES) *.o
I tried to look at forums and added diffrent flags such as all of
-lnetcdf -lpnetcdf -lmfhdf -ldf -ljpeg -lhdf5_hl -lhdf5 -lz -lsz -lm
but it would throw
gcc -IC:/Program\ Files/netCDF\ 4.8.1/include/ -o try try.c -LC:/Program\ Files/netCDF\ 4.8.1/lib -lnetcdf -lpnetcdf -lmfhdf -ldf -ljpeg -lhdf5_hl -lhdf5 -lz -lsz -lm
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -lpnetcdf
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -ldf
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -lsz
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:10: try] Error 1
so I removed theose flags and kept the rest but got the error:
gcc -IC:/Program\ Files/netCDF\ 4.8.1/include/ -o try try.c -LC:/Program\ Files/netCDF\ 4.8.1/lib -lnetcdf -lmfhdf -ljpeg -lhdf5_hl -lhdf5 -lz -lm
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0xa3): undefined reference to `nc_create'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0xc3): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x109): undefined reference to `nc_def_dim'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x129): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x16f): undefined reference to `nc_def_dim'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x18f): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x204): undefined reference to `nc_def_var'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x224): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x24f): undefined reference to `nc_enddef'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x26f): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x2ad): undefined reference to `nc_put_var_int'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x2cd): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x2f8): undefined reference to `nc_close'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x318): undefined reference to `nc_strerror'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:10: try] Error 1
Any help is appreciated, Thank You
Update!! I think that because the files I was trying to link were .lib, gcc was not linking it. I changed form vscode to vs community and added the .lib and it started to work properly.
Instead of compiling with gcc I compiled with MSVC.

How do I change my makefile to avoid the undefined reference to a function in the maths library?

I'm trying to install PintOS on my local Ubuntu 14.04 machine. When I try to run make to compile the utilities. I get the following error.
ankitkal#ankitkal-Inspiron-5521:~/os/pintos/src/utils$ ls
backtrace Makefile pintos pintos.~1.55.~ pintos-mkdisk setitimer-helper.o squish-unix.c
CVS Makefile~ pintos~ pintos-gdb setitimer-helper.c squish-pty.c
ankitkal#ankitkal-Inspiron-5521:~/os/pintos/src/utils$ make
gcc -lm setitimer-helper.o -o setitimer-helper
setitimer-helper.o: In function `main':
setitimer-helper.c:(.text+0xbe): undefined reference to `floor'
collect2: error: ld returned 1 exit status
make: *** [setitimer-helper] Error 1
ankitkal#ankitkal-Inspiron-5521:~/os/pintos/src/utils$
The maths library (for the <math.h> header which is used in setitimer-helper.c) is not getting linked properly. When I look into the Makefile, this is the output.
ankitkal#ankitkal-Inspiron-5521:~/os/pintos/src/utils$ cat Makefile
all: setitimer-helper squish-pty squish-unix
CC = gcc
CFLAGS = -Wall -W
LDFLAGS = -lm
setitimer-helper: setitimer-helper.o
squish-pty: squish-pty.o
squish-unix: squish-unix.o
clean:
rm -f *.o setitimer-helper squish-pty squish-unix
Please tell me how to fix it. I'm using gcc-4.8.6 by the way.
gcc -lm setitimer-helper.o -o setitimer-helper
The problem is in the order of your arguments to GCC. Try this:
gcc -o setitimer-helper setitimer-helper.o -lm
This is because of the way that ld resolves undefined symbols when linking. Basically, the way you had it before, ld first sees -lm and says "I have no reason to include this library". It then includes your setitimer-helper.o which has an unresolved reference to floor. After that, there are no more libraries to consider, and floor remains unresolved.
If -lm comes afterward, it is able to resolve the reference to floor.

'ld' cannot link symbols, although they are in library

I have a problem while trying to compile and link my program with "dmalloc".
bin
+--dmalloc
include
+--dmalloc.h
lib
+--libdmalloc.a
+--libdmallocth.a
main.c
I have the following directory structure
Now I try to compile my program with the following command:
gcc -Iinclude -Llib -ldmalloc -DDMALLOC main.c
/tmp/ccSDFmWj.o: In function `main':
main.c:(.text+0x29): undefined reference to `dmalloc_malloc'
collect2: ld returned 1 exit status
Okay, I get that there's a problem with linking the symbols, ld simply cannot find reference to dmalloc_malloc. However...
nm lib/libdmalloc.a | grep dmalloc_malloc
0000000000001170 T dmalloc_malloc
0000000000000fe0 t dmalloc_malloc.part.6
I am puzzled... The symbol is there in that library. Why does 'ld' has problem with it?
List the libraries last:
gcc -Iinclude -Llib -DDMALLOC main.c -ldmalloc

Resources