Problems with WinSocks2 library in C++

L00PeR
7 years ago

0

Hi, I’ve been learning C++ basics and I just started learning networking basics, but I have a problem, when I try to compile my program returns me this error:
main.cpp:(.text+0xc6): undefined reference to `__imp_WSAStartup'

I’ve been reading some post on the internet and they said that I needed to add this to the makefile: -L"ws2_32"

I’m using dev-c++ and this are the steps I follow for adding that “thing” to the make file:
Project>Project Options(alt+p)>Makefile
But after doing this still doesn’t work, any idea?
PD: I’m not sure I need to write that “thing” on that input…

4replies
3voices
219views
SIGKILL [r4v463]
7 years ago

0

Hi, can you send your Makefile?

dloser
7 years ago

0

Yes, we can’t say much without the Makefile, but I can tell you that you shouldn’t just add that “thing” anywhere in the Makefile. Usually you add it to LDFLAGS (or perhaps CXXFLAGS) in there. But it kind of depends how the Makefile is structured.

L00PeR
7 years ago

0

I couldn’t find the make file, so I ended by downloading VS and adding: #pragma comment(lib,"ws2_32.lib")

SIGKILL [r4v463]
7 years ago

0

So, I wouldn’t use #pragma. I don’t know how it works in your case but I know that when you use #pragma once in the headers files, it might screwed up with include path depending on how you write it (I let you search on SO if that interests you), so I would rather recommend a basic ifndef against the double inclusion. So be sure of what you do if you use pragma.

Second, maybe you can create your own Makefile, there are good tutorials on Internet I guess. If you need to include a .h in a specific location you should use -I option (an upper case i) to specify the directory of all possible .h. If I remind well, -L is a linker option and you should put it after filenames when you compile : g++ compiler_args files linker_args

Here is one of my old makefile if it can help you:
```SRC = main.cpp \
src/DB.cpp \
src/Table.cpp

OBJ = $(SRC:%.cpp=%.o)

NAME = orm

CXX = g++
CXXFLAGS = -I ./ -Wall -Wextra -pedantic -Werror -std=c++14

RM = rm -f

all: $(NAME)

$(NAME): $(OBJ)
$(CXX) $(OBJ) -o $(NAME) -lmysqlcppconn

clean:
$(RM) $(OBJ)

fclean: clean
$(RM) $(NAME)

re: fclean all
```

You must be logged in to reply to this discussion. Login
1 of 5

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss