how to compile netcdf - c

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.

Related

program wont compile via Makefile when using ncurses library?

I'm trying to make a program using the ncurses library, but for some reason when I try to compile using my Makefile it says there's an error in the Makefile and all of the functions from ncurses are undefined. I included the library in the main.c file correctly since when I compile using gcc main.c -lncurses the program works correctly and doesn't give any errors or say that anything is undefined. I've tried messing around with the Makefile and I can't get it to work properly, does anyone have a solution?
Makefile:
CC = gcc
CFLAGS = -Wall --std=c99 -g
LDFLAGS = -lncurses
OBJECTS = main.o
ALL: space_invaders
space_invaders: $(OBJECTS)
$(CC) $(CFLAGS) -o space_invaders $(OBJECTS)
main.o: main.c
$(CC) $(CFLAGS) -c main.c -o main.o
clean:
rm space_invaders $(OBJECTS)
Output:
gcc -Wall --std=c99 -g -o space_invaders main.o
/usr/bin/ld: main.o: in function main': /home/kyle/space_invaders/main.c:9: undefined reference to initscr'
/usr/bin/ld: /home/kyle/space_invaders/main.c:10: undefined reference to noecho' /usr/bin/ld: /home/kyle/space_invaders/main.c:11: undefined reference to curs_set'
/usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr'
/usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr'
/usr/bin/ld: /home/kyle/space_invaders/main.c:23: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:23: undefined reference to wclear'
/usr/bin/ld: /home/kyle/space_invaders/main.c:24: undefined reference to mvprintw' /usr/bin/ld: /home/kyle/space_invaders/main.c:25: undefined reference to stdscr'
/usr/bin/ld: /home/kyle/space_invaders/main.c:25: undefined reference to `wrefresh'
collect2: error: ld returned 1 exit status
make: *** [Makefile:7: space_invaders] Error 1
LDFLAGS is defined but not used. Add it to the rule that links the final executable.
space_invaders: $(OBJECTS)
$(CC) $(CFLAGS) -o space_invaders $(OBJECTS) $(LDFLAGS)
Alternatively the Make implicit rules can be used to simplify the makefile:
CC = gcc
CFLAGS = -Wall --std=c99 -g
LDFLAGS = -lncurses
OBJECTS = main.o
ALL: space_invaders
space_invaders: $(OBJECTS)
clean:
rm space_invaders $(OBJECTS)
you have to specify the $(LDFLAGS) variable in the linking command:
space_invaders: $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) -o space_invaders $(OBJECTS)
As you probably have seen, the linking command in the output of your run doesn't show the -lncurses option, so the library has not been including, making all the calls to library functions to be unresolved.
I don't recommend you to use the alternative given in another answer about using the implicit linking rule, as it is applicable only to GNU make, and so, it is not portable to other make's around.

Linking a jpeglib to in makefile

Hi I am trying to use jpeglib in my code and i have trouble linking it with my Makefile
I downloaded it in tar.gz file then extracted it and did all the ./configure then the makes and all this stuff but now I have to link it in Makefile and I dunno how here is the Makefile
CFLAGS+= -Wall -Werror -fPIE -std=gnu99 -g
LDFLAGS= -pthread
HW=prgsem
BINARIES=prgsem
#LDFLAGS += -L/usr/local/lib -ljpeglib
#CXXFLAGS += -I/usr/local/include
CFLAGS+=$(shell sdl2-config --cflags)
LDFLAGS+=$(shell sdl2-config --libs) -lSDL2_image
all: ${BINARIES}
OBJS=${patsubst %.c,%.o,${wildcard *.c}}
prgsem: ${OBJS}
${CC} ${OBJS} ${CXXFLAGS} ${LDFLAGS} -o $#
${OBJS}: %.o: %.c
${CC} -c ${CFLAGS} $< -o $#
clean:
rm -f ${BINARIES} ${OBJS}
The commented stuff is what I tried and didnt work. Also I tried to change the #include itself. Tried #include "jpeglib.h" also #include <jpeglib.h> nothing worked.
EDIT: added make compile error message
cc xwin_sdl.o event_queue.o prg_io_nonblock.o gui.o main.o prgsem.o messages.o keyboard.o computation.o utils.o -pthread -L/usr/local/lib -Wl,-rpath,/usr/local/lib -Wl,--enable-new-dtags -lSDL2 -lSDL2_image -o prgsem
/usr/bin/ld: gui.o: in function `save_img':
/home/peter/Cprog/bab36prga-sem/gui.c:67: undefined reference to `jpeg_std_error'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:69: undefined reference to `jpeg_CreateCompress'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:74: undefined reference to `jpeg_stdio_dest'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:81: undefined reference to `jpeg_set_defaults'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:83: undefined reference to `jpeg_start_compress'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:90: undefined reference to `jpeg_write_scanlines'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:93: undefined reference to `jpeg_finish_compress'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:97: undefined reference to `jpeg_destroy_compress'
collect2: error: ld returned 1 exit status
make: *** [Makefile:19: prgsem] Error 1
Thanks for any answers.
Your problem is not during the compilation phase of your program, so changing the #include etc. won't help in this case.
Your problem is during the linking phase, so that means you have not added the library to your link line. If, for example, the library is named libjpeg.a or libjpeg.so, then you need to add -ljpeg to your link line. The easiest way is to add it to the end of LDFLAGS:
LDFLAGS+=$(shell sdl2-config --libs) -lSDL2_image -ljpeg

Littlecms - error during compiling "undefined reference to `cmsOpenProfileFromFile' "

Please help me to use LittleCMS framework - I'm receiving
" undefined reference to `cmsOpenProfileFromFile' "
I had made:
1) Downloaded from https://github.com/mm2/Little-CMS file lcms2.h
2) Downloaded https://sourceforge.net/projects/lcms/and tried to install it like it written in documentation (downloaded tar file, unTARed it, runned
./configure
make
sudo make install
3) Tried to implement a sample from tutorial:
#include "lcms2.h"
int main(void)
{
cmsHPROFILE hInProfile, hOutProfile;
cmsHTRANSFORM hTransform;
hInProfile = cmsOpenProfileFromFile("AdobeRGB1998.icc", "r");
hOutProfile = cmsOpenProfileFromFile("WebCoatedSWOP2006Grade5.icc", "r");
hTransform = cmsCreateTransform(hInProfile, TYPE_BGR_8, hOutProfile, TYPE_BGR_8, INTENT_PERCEPTUAL, 0);
cmsCloseProfile(hInProfile);
cmsCloseProfile(hOutProfile);
return 0;
}
After runing "make" I had received " undefined reference to `cmsOpenProfileFromFile' ".
I think I have to install some library (and I have to add something like this -L/opt/local/lib64 -llcms2 to a makefile or add it to clang ...), but I had read kilobytes of webpages and no one is telling how to do it from scratch - libraries does not appears in folder /opt/local/lib64 or /opt/local/lib (and I cannot find lcms2 in any directory) .
My IDE is: CS50 IDE, Linux version 4.9.17-c9 (root#30db80bfe262) (gcc version 4.9.2 (Debian 4.9.2-10) ).
I am very new in programming, so please, be condescending.
Please help me to solve and to inmplement Little CMS.
Update:
3 types of compilations:
1) Command prompt (internal IDE's makefile, without my Makefile):
~/workspace/cmyk/ $ make cmyk
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow -c -o cmyk.o cmyk.c
clang cmyk.o -lcrypt -lcs50 -lm -o cmyk
cmyk.o: In function `main':
/home/ubuntu/workspace/cmyk/cmyk.c:9: undefined reference to `cmsOpenProfileFromFile'
/home/ubuntu/workspace/cmyk/cmyk.c:10: undefined reference to `cmsOpenProfileFromFile'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `__ubsan_handle_shift_out_of_bounds'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `__ubsan_handle_shift_out_of_bounds'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `__ubsan_handle_shift_out_of_bounds'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `__ubsan_handle_shift_out_of_bounds'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `__ubsan_handle_shift_out_of_bounds'
cmyk.o:/home/ubuntu/workspace/cmyk/cmyk.c:12: more undefined references to `__ubsan_handle_shift_out_of_bounds' follow
cmyk.o: In function `main':
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `cmsCreateTransform'
/home/ubuntu/workspace/cmyk/cmyk.c:14: undefined reference to `cmsCloseProfile'
/home/ubuntu/workspace/cmyk/cmyk.c:15: undefined reference to `cmsCloseProfile'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [cmyk] Error 1
2) Makefile1 (with clang):
CC = clang
CFLAGS = -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror
EXE = cmyk
HDRS = lcms2.h
LIBS =
SRCS = cmyk.c
OBJS = $(SRCS:.c=.o)
$(EXE): $(OBJS) $(HDRS) Makefile
$(CC) $(CFLAGS) -o $# $(OBJS) $(LIBS)
$(OBJS): $(HDRS) Makefile
clean:
rm -f core $(EXE) *.o
Output:
~/workspace/cmyk/ $ make cmyk
clang -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror -c -o cmyk.o cmyk.c
clang -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror -o cmyk cmyk.o
cmyk.o: In function `main':
/home/ubuntu/workspace/cmyk/cmyk.c:9: undefined reference to `cmsOpenProfileFromFile'
/home/ubuntu/workspace/cmyk/cmyk.c:10: undefined reference to `cmsOpenProfileFromFile'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `cmsCreateTransform'
/home/ubuntu/workspace/cmyk/cmyk.c:14: undefined reference to `cmsCloseProfile'
/home/ubuntu/workspace/cmyk/cmyk.c:15: undefined reference to `cmsCloseProfile'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [cmyk] Error 1
3) Makefile2:
iEdit: cmyk.o
gcc $^ -o $# -std=c99
.c.o:
gcc -c $< -std=c99
cmyk.o: lcms2.h
Output:
~/workspace/cmyk/ $ make cmyk
gcc -c cmyk.c -std=c99
clang cmyk.o -lcrypt -lcs50 -lm -o cmyk
cmyk.o: In function `main':
cmyk.c:(.text+0x13): undefined reference to `cmsOpenProfileFromFile'
cmyk.c:(.text+0x26): undefined reference to `cmsOpenProfileFromFile'
cmyk.c:(.text+0x50): undefined reference to `cmsCreateTransform'
cmyk.c:(.text+0x60): undefined reference to `cmsCloseProfile'
cmyk.c:(.text+0x6c): undefined reference to `cmsCloseProfile'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [cmyk] Error 1
Solution is to use other IDE - CS50 IDE does not allowed to install additional libraries. I had installed VirtualBox, mounted xubuntu, installed lib, added -L/usr/local/lib -llcms2 to Makefile and my program was compiled without errors.

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.

Makefile Linker unable to find functions in static library

sorry for this question that may seem trivial, but I looked at a few tutorials and SO questions and still could not figure out what is wrong.
Anyway, when using gcc, the linker is not able to find functions in the static library I have included.
Error message:
arm-none-linux-gnueabi-gcc -I. -I./include yuv.c -c -o yuv.o
arm-none-linux-gnueabi-gcc -I. -I./include main.c -c -o main.o
arm-none-linux-gnueabi-gcc -L./lib -I. -I./include yuv.o main.o -lpthread -lrt -ljpeg -o grab.elf
main.o: In function `jpegWrite':
main.c:(.text+0x118): undefined reference to `jpeg_std_error'
main.c:(.text+0x134): undefined reference to `jpeg_CreateCompress'
main.c:(.text+0x144): undefined reference to `jpeg_stdio_dest'
main.c:(.text+0x17c): undefined reference to `jpeg_set_defaults'
main.c:(.text+0x198): undefined reference to `jpeg_set_quality'
main.c:(.text+0x1a8): undefined reference to `jpeg_start_compress'
main.c:(.text+0x1e4): undefined reference to `jpeg_write_scanlines'
main.c:(.text+0x200): undefined reference to `jpeg_finish_compress'
main.c:(.text+0x20c): undefined reference to `jpeg_destroy_compress'
collect2: ld returned 1 exit status
make: *** [grab.elf] Error 1
My file structure is as follows:
Makefile
main.c
In Folder lib
libjpeg.a
My Makefile reads:
GCC=gcc
INC_PATH= -I. -I./include
LIBS_PATH = -L./lib
HEADER_FILE=./yuv.h ./include/jpeglib.h
grab.elf: yuv.o main.o
$(CROSS_COMPILE)$(GCC) $(LIBS_PATH) $(INC_PATH) yuv.o main.o -lpthread -lrt -ljpeg -o grab.elf
yuv.o:yuv.c $(HEADER_FILE)
$(CROSS_COMPILE)$(GCC) $(INC_PATH) yuv.c -c -o yuv.o
main.o:main.c $(HEADER_FILE)
$(CROSS_COMPILE)$(GCC) $(INC_PATH) main.c -c -o main.o
I also tried:
nm libjpeg.a | grep jpeg_std
000001f0 T _jpeg_stdio_dest
00000140 T _jpeg_stdio_src
000001f0 T _jpeg_std_error
000012c0 R _jpeg_std_message_table
Would any kind soul care to help me? Thank you.
Could it be that libjpeg.a was compiled on the platform on which you are developing, whereas the target of grab.elf is different? i.e., you are developing on x86 environment and targeting ARM?

Resources