I'm trying to create a makefile - 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

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.

linking assembly object file with C object file on OS X and can't find symbol

I have a library defined in libadd.asm, it exposes one "function" _add. I have a .c source file that refers to add and I'm trying to get the two object files to link, but am encountering this error regardless of the order in which I link the object files:
Undefined symbols for architecture x86_64:
"_add", referenced from:
_main in prog.o
Here's the code:
// prog.c
#include <stdio.h>
int add();
int main() {
printf("%d\n", add(4, 5));
return 0;
}
And here's the assembly file. It almost certainly doesn't respect the appropriate calling convention. I don't really understand what I should be doing to shuffle the values between registers. (That's what I was trying to figure out originally.)
; libadd.asm
_add:
add eax, edx
ret
Here's what I'm using to the tiny project. I'm intentionally shadowing the implicit .c.o rule with one that does as little as possible and ignores *FLAGS. I'm using cc to drive the linker because that's the simplest way I know to link in the c runtime/standard library/whatever it's called. I've always tried linking with prog.o and libadd.o in the other order.
all: prog
prog: prog.o libadd.o
$(CC) -o prog $^
%.o: %.asm
nasm -f macho64 -o $# $<
%.o: %.c
$(CC) -c -o $# $<
clean:
$(RM) $(wildcard *.o)
running make produces the following output
cc -c -o prog.o prog.c
nasm -f macho64 -o libadd.o libadd.asm
cc -o prog prog.o libadd.o
Undefined symbols for architecture x86_64:
"_add", referenced from:
_main in prog.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [prog] Error 1
Exit 2
libadd.o gets assembled successfully and appears to have the right symbol in it.
% nm libadd.o
0000000000000000 t _add
Why is ld complaining that it can't find the symbol?

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

Makefile returning errors

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.

Resources