Issue with compiling a c project on Ubuntu 20.4 WSL - c

I am trying to compile a project using WSL bash on Windows (it compiles using cmd, but I want to test it using a bash terminal). I have three modules and these are the libraries I include in the project:
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
The WSL still compiles a hello world program, but it could not compile my project as it gives the following error.
The error:
gcc line.o main.o textutil.o content.o -o textutil
/usr/bin/ld: line.o:line.c:(.text+0x69): undefined reference to `__imp__assert'
/usr/bin/ld: line.o:line.c:(.text+0xb3): undefined reference to `__imp__assert'
/usr/bin/ld: line.o:line.c:(.text+0x123): undefined reference to `__imp__assert'
/usr/bin/ld: line.o:line.c:(.text+0x417): undefined reference to `__imp__assert'
/usr/bin/ld: line.o:line.c:(.text+0x444): undefined reference to `__imp__assert'
/usr/bin/ld: line.o:line.c:(.text+0x471): more undefined references to `__imp__assert' follow
/usr/bin/ld: textutil.o:textutil.c:(.text+0x33): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x25d): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x318): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x45c): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x503): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x65c): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x703): more undefined references to `__imp___acrt_iob_func' follow
/usr/bin/ld: textutil.o:textutil.c:(.text+0x100b): undefined reference to `___chkstk_ms'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x1079): undefined reference to `___chkstk_ms'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x1196): undefined reference to `___chkstk_ms'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x1520): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x15d4): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: content.o:content.c:(.text+0xac): undefined reference to `__imp__assert'
/usr/bin/ld: content.o:content.c:(.text+0xfa): undefined reference to `__imp__assert'
/usr/bin/ld: content.o:content.c:(.text+0x16a): undefined reference to `__imp__assert'
collect2: error: ld returned 1 exit status
make: *** [Makefile:17: textutil] Error 1
this is the Makefile
CC = gcc
CFLAGS = -std=c11 -W -Wall -Werror -pedantic -Wfatal -errors
CFLAGS += -D_XOPEN_SOURCE=700 # we use strdup() and strndup()
LIBS = # -lm
EXEC= textutil
HEADERS = $(wildcard *.h)
SOURCES = $(wildcard *.c)
MODULES = $(basename $(SOURCES))
OBJECTS = $(addsuffix .o , $(MODULES))
all: $(EXEC)
run: ./$(EXEC).exe
$(EXEC): $(OBJECTS)
#echo === LINKING $# ===
$(CC) $^ $(LIBS) -o $#
%.o : %.c
#echo === COMPILING $# ===
$(CC) $< $(CFLAGS) -c
depend: $(SOURCES) $(HEADERS)
#echo === COMPUTING $# ===
$(CC) -MM $(SOURCES) | tee $#
clean ::
#echo === CLEANING ===
rm -f *.o depend
-include depend
this is gcc version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Related

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.

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

What's wrong with my cross compiling makefile?

I have spent several days now trying to cross compile a c program on my windows machine. the code runs fine on my linux machine but I had too many errors trying to cross compile it on that machine. Now I'm using Cygwin and Mingw32.
CC = x86_64-w64-mingw32-gcc
CFLAGS = -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image -lfftw3 -lftdi1
-mwindows `sdl-config --cflags --libs`
OBJECTS = main.o fft.o draw.o table.o serial.o
main : $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) -o main
%.o : %.c
$(CC) $(CFLAGS) -c $<
clean:
#echo Cleaning up...
rm -fr *.o main
#echo Done.
This is what my makefile looks like after editing it to what I thought was needed to cross compile it. But I get many undefined reference errors. I realize this question is very similar to a few others I've looked over on SO, but I cannot seem to find my mistake.
$ make
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image -
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c main.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image -
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c fft.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image -
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c draw.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image -
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c table.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image -
lfftw3 -
lftdi1 -mwindows `sdl-config --cflags --libs` -c serial.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image -
lfftw3 -
lftdi1 -mwindows `sdl-config --cflags --libs` main.o fft.o draw.o table.o
serial.o -o main
/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
/usr/src/debug/mingw64-x86_64-runtime-5.0.3-1/crt/crt0_c.c:18: undefined reference to `WinMain'
fft.o:fft.c:(.text+0xa): undefined reference to `fftw_malloc'
fft.o:fft.c:(.text+0x1b): undefined reference to `fftw_malloc'
fft.o:fft.c:(.text+0x2c): undefined reference to `fftw_malloc'
fft.o:fft.c:(.text+0x4d): undefined reference to `fftw_plan_dft_r2c_1d'
fft.o:fft.c:(.text+0x73d): undefined reference to `fftw_execute'
draw.o:draw.c:(.text+0xb): undefined reference to `__imp_glShadeModel'
draw.o:draw.c:(.text+0x1e): undefined reference to `__imp_glClearColor'
draw.o:draw.c:(.text+0x43): undefined reference to `gluOrtho2D'
draw.o:draw.c:(.text+0x4e): undefined reference to `__imp_glDisable'
draw.o:draw.c:(.text+0x5e): undefined reference to `__imp_glEnable'
draw.o:draw.c:(.text+0x82): undefined reference to `__imp_glViewport'
draw.o:draw.c:(.text+0x8e): undefined reference to `__imp_glMatrixMode'
draw.o:draw.c:(.text+0x97): undefined reference to `__imp_glLoadIdentity'
draw.o:draw.c:(.text+0xbe): undefined reference to `gluOrtho2D'
draw.o:draw.c:(.text+0xeb): undefined reference to `SDL_Init'
draw.o:draw.c:(.text+0xf4): undefined reference to `SDL_GetVideoInfo'
draw.o:draw.c:(.text+0x11c): undefined reference to `SDL_GL_SetAttribute'
draw.o:draw.c:(.text+0x13a): undefined reference to `SDL_SetVideoMode'
draw.o:draw.c:(.text+0x171): undefined reference to `SDL_GetError'
draw.o:draw.c:(.text+0x1a1): undefined reference to `SDL_GetError'
draw.o:draw.c:(.text+0x1d1): undefined reference to `SDL_GetError'
draw.o:draw.c:(.text+0x244): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x24b): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x252): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x29f): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x415): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x42c): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x447): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x4ef): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x55b): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x574): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x580): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x5e4): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x602): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x638): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x648): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x664): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x6b8): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x6e8): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x6fb): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x717): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x76b): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x7b5): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x80d): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x814): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x935): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0xa00): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0xa07): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0xa0e): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0xb0b): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0xc09): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0xc10): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0xc19): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0xc20): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0xea7): undefined reference to `__imp_glClear'
draw.o:draw.c:(.text+0xeb2): undefined reference to `__imp_glMatrixMode'
draw.o:draw.c:(.text+0xeb8): undefined reference to `__imp_glLoadIdentity'
draw.o:draw.c:(.text+0xec7): undefined reference to `__imp_glTranslatef'
draw.o:draw.c:(.text+0xf48): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0xf72): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0xf8f): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0xfba): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x1089): undefined reference to `SDL_GL_SwapBuffers'
draw.o:draw.c:(.text+0x10c3): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x10d8): undefined reference to `SDL_GetKeyState'
draw.o:draw.c:(.text+0x133d): undefined reference to `SDL_WM_ToggleFullScreen'
draw.o:draw.c:(.text+0x1364): undefined reference to `SDL_PollEvent'
draw.o:draw.c:(.text+0x1385): undefined reference to `SDL_PollEvent'
draw.o:draw.c:(.text+0x13ab): undefined reference to `SDL_SetVideoMode'
draw.o:draw.c:(.text+0x13e1): undefined reference to `SDL_GetError'
serial.o:serial.c:(.text+0x11): undefined reference to `ftdi_init'
serial.o:serial.c:(.text+0x1f): undefined reference to `ftdi_set_interface'
serial.o:serial.c:(.text+0x32): undefined reference to `ftdi_usb_open'
serial.o:serial.c:(.text+0x45): undefined reference to `ftdi_set_baudrate'
serial.o:serial.c:(.text+0x94): undefined reference to `ftdi_get_error_string'
serial.o:serial.c:(.text+0xc7): undefined reference to `ftdi_get_error_string'
serial.o:serial.c:(.text+0x139): undefined reference to `ftdi_write_data'
serial.o:serial.c:(.text+0x233): undefined reference to `ftdi_write_data'
collect2: error: ld returned 1 exit status
make: *** [makefile:6: main] Error 1
When you link to a SO in Linux , for example with -lGL, you are linking the shared object directly libGL.so.
But in Windows, when you link a DLL you are actually linking a static library libGL.a (libGL.lib in native Windows) that contains one thunk per imported function.
And as it happens, linker rules are different between shared and static libraries. Static libraries are linked using the order in the linker command line.
TL;DR; You have to put the -l* at the end of the linker command. Also, it is a good practice to separate CFLAGS and LDFLAGS:
CC = x86_64-w64-mingw32-gcc
CFLAGS = -Wall -O2 `sdl-config --cflags`
LDFLAGS = -lmingw32 -lGL -lGLU -lm -lSDL_image -lfftw3 -lftdi1 -mwindows `sdl-config --libs`
OBJECTS = main.o fft.o draw.o table.o serial.o
main : $(OBJECTS)
$(CC) -o $# $(CFLAGS) $(OBJECTS) $(LDFLAGS)
%.o : %.c
$(CC) $(CFLAGS) -c $<
About the WinMain reference, that is because you wrote -mwindows: that is to build a Windows application, that is one that starts with WinMain() instead of main(). If you want to stick to your main() you can use -mconsole instead.
I switched the objects and cflags at the top, but forgotten to switch objects and cflags in
main : $(OBJECTS)
$(CC) $(OBJECTS) $(CFLAGS) -o main
Now I only get a undefined reference to WinMain
$ make
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lm -lGL -lGLU -lSDL_image -
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c main.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lm -lGL -lGLU -lSDL_image -
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c fft.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lm -lGL -lGLU -lSDL_image -
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c draw.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lm -lGL -lGLU -lSDL_image -
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c table.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lm -lGL -lGLU -lSDL_image -
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c serial.c
x86_64-w64-mingw32-gcc main.o fft.o draw.o table.o serial.o -Wall -O2 -
lmingw32 -lm -lGL -lGLU -lSDL_image -lfftw3 -lftdi1 -mwindows `sdl-config --
cflags --libs` -o main
/usr/x86_64-w64-mingw32/sys-
root/mingw/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function
`main':
/usr/src/debug/mingw64-x86_64-runtime-5.0.3-1/crt/crt0_c.c:18: undefined
reference to `WinMain'
collect2: error: ld returned 1 exit status
make: *** [makefile:6: main] Error 1

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