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.
Related
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).
I have got 4 files: pass1.c , pass2.c, main.c and header1.h.
Each of these files, include the file header.h
I wrote the following makefile:
assembler: pass1.o pass2.o main.o
gcc pass1.o pass2.o -o assembler
pass1.o: pass1.c
gcc -c -ansi -Wall -pedantic pass1.c -o pass1.o
pass2.o: pass2.c
gcc -c -ansi -Wall -pedantic pass2.c -o pass2.o
main.o: main.c
gcc -c -ansi -Wall -pedantic main.c -o main.o
When I did make, I got the following error:
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'assembler' failed
make: *** [assembler] Error 1
Note that I didn't write a function called 'start'.
What is the problem here and how can I fix it?
gcc pass1.o pass2.o -o assembler
should be
gcc main.o pass1.o pass2.o -o assembler
Rename main.c to assembler.c and your entire makefile can be condensed to
CFLAGS := -ansi -Wall -pedantic
objects := assembler.o pass1.o pass2.o
assembler: $(objects)
$(objects): header.h
assuming that cc on your system is a symlink to gcc.
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
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
I managed to build ffmpeg and libx264 on my Ubuntu 11.10 machine from source.
I'm trying to work on the example source file decoding_encoding.c. The examples comes with a makefile, so I can just type in "make all" and it magically compiles and links everything. Now, I'm trying to compile and link from the command line but I can't seem to get it to link.
Here is the makefile:
# use pkg-config for getting CFLAGS abd LDFLAGS
FFMPEG_LIBS=libavdevice libavformat libavfilter libavcodec libswscale libavutil
CFLAGS+=$(shell pkg-config --cflags $(FFMPEG_LIBS))
LDFLAGS+=$(shell pkg-config --libs $(FFMPEG_LIBS))
EXAMPLES=decoding_encoding filtering metadata muxing
OBJS=$(addsuffix .o,$(EXAMPLES))
%: %.o
$(CC) $< $(LDFLAGS) -o $#
%.o: %.c
$(CC) $< $(CFLAGS) -c -o $#
.phony: all clean
all: $(OBJS) $(EXAMPLES)
clean:
rm -rf $(EXAMPLES) $(OBJS)
I can compile the source using this:
gcc -Wall -I/usr/local/include -c -o decoding_encoding.o decoding_encoding.c
When I try to link using this:
gcc -Wall -L/usr/local/lib -lavdevice -lavformat -lavfilter -lavcodec -lswscale -lavutil -o decoding_encoding decoding_encoding.o
At this point, I get a huge list of 'undefined reference error'
decoding_encoding.o: In function `audio_encode_example':
decoding_encoding.c:(.text+0x25): undefined reference to `avcodec_find_encoder'
decoding_encoding.c:(.text+0x6a): undefined reference to `avcodec_alloc_context3'
decoding_encoding.c:(.text+0xad): undefined reference to `avcodec_open'
decoding_encoding.c:(.text+0x1ce): undefined reference to `sin'
decoding_encoding.c:(.text+0x238): undefined reference to `avcodec_encode_audio'
decoding_encoding.c:(.text+0x297): undefined reference to `avcodec_close'
decoding_encoding.c:(.text+0x2a3): undefined reference to `av_free'
decoding_encoding.o: In function `audio_decode_example':
decoding_encoding.c:(.text+0x2f7): undefined reference to `av_init_packet'
decoding_encoding.c:(.text+0x30b): undefined reference to `avcodec_find_decoder'
decoding_encoding.c:(.text+0x359): undefined reference to `avcodec_alloc_context3'
decoding_encoding.c:(.text+0x379): undefined reference to `avcodec_open'
..... etc.
collect2: ld returned 1 exit status
Am I doing something wrong with my linker command? I've examined the makefile and I believe that I'm doing exactly what the makefile is....or am I?
Thanks,
Leo
The -l options should go after any other options.