Makefile returning errors - gcc

I have a makefile that is supposed to compile a set of C files which use the <pthread.h> library. Although the makefile ran on my previous Linux installation, I can't get it running now. Can anyone help me with this?
CC=gcc
CFLAGS=-c -std=c99
LDFLAGS= -lpthread
SOURCES=pc_0.c stak.h
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=pcths
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $#
.c.o:
$(CC) $< -o $# $(CFLAGS)
clean:
rm -f *.o core
Here is the error message I get while typing "make" on my shell prompt.
gcc pc_0.c -o pc_0.o -c -std=c99
pc_0.c: In function ‘main’:
pc_0.c:25:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)()’
pc_0.c:29:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)()’
pc_0.c:33:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)()’
gcc -lpthread pc_0.o stak.h -o pcths
pc_0.o: In function `main':
pc_0.c:(.text+0x100): undefined reference to `pthread_create'
pc_0.c:(.text+0x15d): undefined reference to `pthread_create'
pc_0.c:(.text+0x1ba): undefined reference to `pthread_create'
pc_0.c:(.text+0x206): undefined reference to `pthread_join'
pc_0.c:(.text+0x21a): undefined reference to `pthread_join'
pc_0.c:(.text+0x22e): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
make: *** [pcths] Error 1
However, if I manually run the following instructions the sources are compiled:
cc pc_0.c -o pc_0.o -c -std=c99
cc pc_0.o -o pcths -lpthread

Put -lpthread at the end of the command line, after the object file, which need symbols from libthread.[a|so]
When GNU ld (and other linkers) resolve undefined references it looks for symbol definition only in the object file and libraries, which follow the object, which contains the said references.

Not sure how important it is but I always add '' around assignments of variables

Posting the actual error message is of the utmost importance when trying to debug your issue.
Possibly not your issue but I usually set my editor (emacs) to use spaces instead of tabs for indenting and this causes issue in make. I have to highlight all and run m-x tabify to fix the issue.

Related

ld: undefined reference, but it should leave them unresolved

I am unable to produce a library, which works with another library (SDL). I am using MinGW for make, and ld to link. I am confused because a) it shouldn't be trying to link in these libraries, but do this later when someone links my library in as well; and b) even if I do link in the SDL libraries, it still can't find the SDL functions (SDL_GetTicks, SDL_Delay) it's looking form -- the errors are the same. Also note that some of the missing items are from std.
Here are the errors. As you can see, I'm trying various flags on ld to make it not try to resolve references, but w/o success yet.
C:\Users\...\mcve>make
g++ -c -c -I../../../external/SDL2/include -I../include -o mcve.o mcve.cpp
ld -G --unresolved-symbols=ignore-all --warn-unresolved-symbols -o libmcve.a mcve.o
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x8): undefined reference to `SDL_GetTicks'
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x23): undefined reference to `SDL_GetTicks'
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x2f): undefined reference to `SDL_Delay'
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x46): undefined reference to `std::ios_base::Init::~Init()'
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x67): undefined reference to `std::ios_base::Init::Init()'
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x73): undefined reference to `atexit'
Here's my source file:
#include <SDL.h>
#include <iostream> //If I take this out, I no longer get the
//unresolved references to std::ios_base::Init::Init,
// std::ios_base::Init::~Init, and atexit
Uint32 time;
void doSomething ()
{
if (time > SDL_GetTicks ())
SDL_Delay (time - SDL_GetTicks());
}
This is the Makefile. If I uncomment the rest of the LDFLAGS and let the SDL libraries link in, it does not change the output.
CFLAGS =-c -I../../../external/SDL2/include -I../include
LDFLAGS = --unresolved-symbols=ignore-all --warn-unresolved-symbols #-L. -lSDL2 -lSDL2_ttf -lSDL2_image -lSDL2_mixer
# Files
SOURCE_FILES= mcve.cpp
OBJECT_FILES= mcve.o
libmcve.a: $(OBJECT_FILES)
ld $(LDFLAGS) -o $# $^ -G
$(OBJECT_FILES): %.o: $(SOURCE_FILES)
g++ -c $(CFLAGS) -o $# $<
You are attempting to create the static library libmcve.a from the object file
mcve.o using the linker ld.
The linker cannot produce a static library. A static library is merely
an ar archive of object files that is produced by ar.
The recipe to create or update the static library in your makefile would be:
libmcve.a: $(OBJECT_FILES)
rm -f $# # Delete archive if already exists
ar rcs $# $^ # Recreate archive with contents $(OBJECT_FILES)
BTW, note that you are passing the -c option in your compilation commands
twice:
g++ -c -c -I../../../external/SDL2/include -I../include -o mcve.o mcve.cpp
That is because you have included it in your CFLAGS setting:
CFLAGS =-c -I../../../external/SDL2/include -I../include
(where it should not be), and also in your compilation recipe:
g++ -c $(CFLAGS) -o $# $<
(where it should be).

undefined reference to `__imp_socket'

I'm doing a socket project on win10, and I get these errors.
g++ -ggdb -std=c++11 -Wall -pedantic -o calcserver CalcServer.c
DieWithError.c HandleTCPClient.c CalcFramer.cpp CalcParser.cpp
C:\Users\HARRYS~1\AppData\Local\Temp\cceLC8Xb.o: In function `main':
D:\src/CalcServer.c:35: undefined reference to `__imp_socket'
D:\src/CalcServer.c:41: undefined reference to `__imp_htonl'
D:\src/CalcServer.c:42: undefined reference to `__imp_htons'
D:\src/CalcServer.c:45: undefined reference to `__imp_bind'
D:\src/CalcServer.c:49: undefined reference to `__imp_listen'
D:\src/CalcServer.c:58: undefined reference to `__imp_accept'
D:\src/CalcServer.c:64: undefined reference to `__imp_inet_ntoa'
collect2.exe: error: ld returned 1 exit status
make: *** [calcserver] Error 1
I tried to link Ws2_32.lib. I download Ws2_32.lib under directory src, and modify my makefile like this:
CC=g++
CFLAGS=-ggdb -std=c++11 -Wall -pedantic
LINKFLAGS = -L"D:\src" -lWS2_32
H_FILES=CalcFramer.hpp CalcParser.hpp
FILES=CalcServer.c DieWithError.c HandleTCPClient.c CalcFramer.cpp CalcParser.cpp
all: calcserver
calcserver: $(FILES) $(H_FILES)
$(CC) $(CFLAGS) -o calcserver $(FILES)
clean:
rm -rf calcserver
However, I still get the above errors. I've already changed all the sys/socket.h headers to Winsock.h and Winsock2.h. So I think it's not that part that leads me to the errors.

I'm trying to create a makefile

makefile:
lambda : main.o
gcc -o lambda main.o
main.o : main.c
gcc -c main.c -o main.o
.PHONY : clean
all I get as an answer instead of my executable is
main.o: In function `fn':
main.c:(.text+0x199): undefined reference to `pow'
main.c:(.text+0x1c6): undefined reference to `pow'
main.c:(.text+0x1eb): undefined reference to `log10'
main.o: In function `fnPrime':
main.c:(.text+0x21d): undefined reference to `pow'
main.c:(.text+0x246): undefined reference to `pow'
main.c:(.text+0x26b): undefined reference to `log10'
main.c:(.text+0x2af): undefined reference to `pow'
collect2: error: ld returned 1 exit status
make: *** [lambda] Error 1
what is wrong?
It looks like you're missing a reference to the Math library. You need to add -lm to the end of your make command
Well, after a search in stackoverflow I find that the problem was that I didn't put the option -lm at the end of the linking line.
lambda : main.o
gcc -o lambda main.o -lm
main.o : main.c
gcc -c main.c -o main.o
.PHONY : clean
clean : rm lambda main.o

My makefile could not find header file

I have write a simple makefile
1 LIBS= -L /usr/local/pgsql/lib
2 INCL= -I /usr/local/pgsql/include -I/home/name/
3
4 pg: pg.o
5 gcc -o pg pg.o $(LIBS) -lpq
6
7 pg.o: pg.c
8 gcc -c $(INCL) $(LIBS) pg.c
Under the folder of name, there are three files: pg.c, timer.c, timer.h
but it reports error of could not find timefunctions. what's wrong with my makefile? thanks.
The error is
gcc -o pg pg.o -L /usr/local/pgsql/lib -lpq
pg.o: In function `main':
pg.c:(.text+0x91): undefined reference to `createTimer'
pg.c:(.text+0xa1): undefined reference to `startTimer'
pg.c:(.text+0x167): undefined reference to `endTimer'
...
pg.c:(.text+0x214): undefined reference to `displayTimer'
pg.c:(.text+0x220): undefined reference to `destroyTimer'
collect2: error: ld returned 1 exit status
make: *** [pg] Error 1
You haven't given us enough information to be certain, but it might be enough to add timer.o to the prerequisite list of the pg rule:
pg: pg.o timer.o
gcc -o pg $^ $(LIBS) -lpq

g++ undefined reference to `boost::system::system_category()'

I have searched high and low for answer to this issue. I am using boost 1.48 and the program is extremely simple, since I have broken it down to its simplest form in order to solve this issue.
#include <boost/filesystem.hpp>
int main(int argc, char **argv) {
return 0;
}
The g++ command executed from my Makefile is as follows:
g++ -m32 -Wall -o mapnik-test -L/usr/lib -I/usr/include -I/usr/include/freetype2 -lpthread -lboost_system mapnik-test.cpp
The complete list of errors during linking is as follows:
/tmp/ccIbmuee.o: In function `__static_initialization_and_destruction_0(int, int)':
mapnik-test.cpp:(.text+0x49): undefined reference to `boost::system::generic_category()'
mapnik-test.cpp:(.text+0x53): undefined reference to `boost::system::generic_category()'
mapnik-test.cpp:(.text+0x5d): undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [mapnik-test] Error 1
I have found many people suffering from the same issue, but in most cases, the solution has been to provide the boost_system library in the LDFLAGS. As you can see from the g++ command line, I already have this specified. I have even tried explicitly linking against the libboost_system.a library to no avail. Am I the only person with this complaint?
Put the source file at the beginning of the command line.
Try
g++ -m32 -Wall mapnik-test.cpp -o mapnik-test -L/usr/lib -I/usr/include -I/usr/include/freetype2 -lpthread -lboost_system
The libraries should be specified only after the source file so that the linker can resolve the undefined references in the source file.

Resources