Can't add "ws2_32.lib" in any way [duplicate] - c++

This question already has answers here:
C - Undefined Reference to WSAStartup#8'
(5 answers)
Closed 5 months ago.
I'm beginning with socket programming in C++, and as a normal student, I went to search online.
The problem is, it doesn't matter what I do, I always get the following errors:
C:\Users\farin\AppData\Local\Temp\ccQCkFqw.o:main.cpp:(.text+0x37): undefined reference to `WSAStartup#8'
C:\Users\farin\AppData\Local\Temp\ccQCkFqw.o:main.cpp:(.text+0x48): undefined reference to `WSAGetLastError#0'
collect2.exe: error: ld returned 1 exit status
I'm using MinGW in Visual Studio Code.
Here's the sample code I've been messing around with:
/*
Initialise Winsock
*/
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
WSADATA wsa;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.");
return 0;
}
I've already tried linking to the compiler, and possibly gone throught all the pages
about the subject that I could find.

Solution is here!
Just ran throught the command line, as follow:
g++ main.cpp -o main.exe -lws2_32
Thanks to Retired Ninja.

Related

g++ can't find libavcodec, but gcc can [duplicate]

This question already has an answer here:
linking with libavcodec, still seeing undefined references
(1 answer)
Closed 4 months ago.
I'm on Ubuntu 20.04 trying to compile some code that uses libav. Take the following example script:
// main.c
#include <libavcodec/avcodec.h>
int main()
{
avcodec_find_encoder((enum AVCodecID) 0);
return 0;
}
If I build this with gcc test.c -lavcodec it builds just fine, but if I build it with g++ test.c -lavcodec I get:
/usr/bin/ld: /tmp/ccHxMTp1.o: in function `main':
test.c:(.text+0xe): undefined reference to `avcodec_find_encoder(AVCodecID)'
collect2: error: ld returned 1 exit status
I think you're 'suffering' from C++ name mangling. Try wrapping the #include line with an extern "C" {, } pair...
extern "C" {
#include <libavcodec/avcodec.h>
}
int main()
{
avcodec_find_encoder((enum AVCodecID) 0);
return 0;
}

compiling sdl simple program in dev C++ shows undefined reference to `WinMain' [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
undefined reference to `WinMain#16'
(7 answers)
Closed 2 years ago.
My version of DEV C++ is 5.11 and I have used version SDL2-2.0.12 and OS is Windows 10.
Folder path of sdl.h file is C:\Program Files (x86)\Dev-Cpp\SDL2-2.0.12\x86_64-w64-mingw32\include\SDL2
I have done my project settings as shown in ::
https://thenumbat.github.io/cpp-course/sdl2/01/devSetup.html#:~:text=Open%20up%20your%20project%20and,and%20select%20the%20lib%20folder.
My program
#include <iostream>
#include "SDL.h"
using namespace std;
int main( int argc, char* args[] )
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
cout << "SDL init failed.\n";
return 1;
}
SDL_Quit();
return 0;
}
While compiling I am getting such message::
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib\libmingw32.a(lib64_libmingw32_a-crt0_c.o) In function `main':
C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crt0_c.c undefined reference to `WinMain'
------ this is in red color ---------
C:\Program Files (x86)\Dev-Cpp\SDL2-2.0.12\x86_64-w64-mingw32\collect2.exe [Error] ld returned 1 exit status
C:\Program Files (x86)\Dev-Cpp\SDL2-2.0.12\x86_64-w64-mingw32\Makefile.win recipe for target 'SDLproj1.exe' failed
Please see the attached image.

How to link giflib? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
Unfortunately, I couldn't find any beginner friendly tutorial on how to use giflib.
This is my code:
#include <iostream>
#include <gif_lib.h>
int main(int argc, char** argv)
{
if (argc < 2)
{
std::cerr << "No argument" << std::endl;
return 1;
}
GifFileType *gifFile = DGifOpenFileName(argv[1], NULL);
DGifSlurp(gifFile);
}
I try to compile it like this:
g++ main.cpp
but it yields this error:
/usr/bin/ld: /tmp/ccd5G2QR.o: in function `main':
main.cpp:(.text+0x5c): undefined reference to `DGifOpenFileName'
/usr/bin/ld: main.cpp:(.text+0x6c): undefined reference to `DGifSlurp'
collect2: error: ld returned 1 exit status
I'm on Ubuntu 20.04 and I've installed the libgif-dev package.
How to link the giflib library for the functions to work?
Trying randomly around I found it's:
g++ main.cpp -lgif

libssh problems compile problem undefined errors [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
Hi im brand new to c++ im trying to get a ssh connection to work here is my code
Its been pointed out to me i need to link the libraries ... how is this done?
#include <libssh/libssh.h>
#include <stdlib.h>
int main()
{
ssh_session my_ssh_session;
my_ssh_session = ssh_new();
int verbosity = SSH_LOG_PROTOCOL;
int port = 22;
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "localhost");
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
ssh_free(my_ssh_session);
}
I keep getting this error
[brett#badbox sshcpp1]$ make
g++ -Wl,-O1 -Wl,-z,relro -o sshcpp1 main.o -lQt5Core -lpthread
main.o: In function `main':
/home/brett/sshcpp1/main.cpp:7: undefined reference to `ssh_new'
/home/brett/sshcpp1/main.cpp:10: undefined reference to `ssh_options_set'
/home/brett/sshcpp1/main.cpp:11: undefined reference to `ssh_options_set'
/home/brett/sshcpp1/main.cpp:12: undefined reference to `ssh_options_set'
/home/brett/sshcpp1/main.cpp:13: undefined reference to `ssh_free'
collect2: error: ld returned 1 exit status
make: *** [sshcpp1] Error 1
Outocomplete seems to be picking up the ssh library.
What am i doing wrong??
You need to link libssh by adding -lssh to your linker command.

undefined reference to `__imp_WSACleanup'

This is my first program with winsock. As you can see, I've #include <winsock2.h> and linked ws2_32.dll, but the code still doesn't compile:
#include<winsock2.h>
#pragma comment(lib, "ws2_32")
class CInitSock{
public:
CInitSock(BYTE minorVer=2,BYTE majorVer=2){
//initialize WS2_32.dll
WSADATA wsaData;
WORD sockVersion = MAKEWORD(minorVer,majorVer);
if(::WSAStartup(sockVersion,&wsaData)!=0){
exit(0);
}
}
//release winSock libary
~CInitSock(){
::WSACleanup();
}
};
#include "CInitSock.h"
#include<stdio.h>
CInitSock initSock;
int main(void){
char szHost[256];
::gethostname(szHost,256);
hostent *phost = ::gethostbyname(szHost);
in_addr addr;
for(int i = 0;;i++){
char *p = phost->h_addr_list[i];
if(p==NULL){
break;
}
memcpy(&addr.S_un.S_addr,p,phost->h_length);
char *szIp = ::inet_ntoa(addr);
printf("%s \n",szIp);
}
}
This is the error:
mingw32-make.exe -f "D:\project\c_program\Makefile.win" all
g++.exe GetAllIPs.o -o win_socket.exe -L"D:/tools/develepment/Dev-Cpp/MinGW64/x86_64- w64-mingw32/lib" -L"D:/tools/develepment/Dev-Cpp/MinGW64/lib32" -static-libgcc -mwindows -g3
GetAllIPs.o: In function `main':
D:\project\c_program/GetAllIPs.cpp:6: undefined reference to `__imp_gethostname'
D:\project\c_program/GetAllIPs.cpp:7: undefined reference to `__imp_gethostbyname'
D:\project\c_program/GetAllIPs.cpp:15: undefined reference to `__imp_inet_ntoa'
GetAllIPs.o: In function `CInitSock::CInitSock(unsigned char, unsigned char)':
D:\project\c_program/CInitSock.h:10: undefined reference to `__imp_WSAStartup'
GetAllIPs.o: In function `CInitSock::~CInitSock()':
D:\project\c_program/CInitSock.h:16: undefined reference to `__imp_WSACleanup'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe: *** [win_socket.exe] Error 1
Now I'm totally confused...
The pragma you use only works for the Visual C++ Compiler and will be ignored by the gcc
#pragma comment(lib, "ws2_32")
you have to add the ws2_32.lib it manually in the makefile.
like:
-L"ws2_32"
(I guess it was without the ".lib" at the end)
at the end of the g++ line. You have of course add the full path which I have not by hand at the moment.
I met the same problem with you. I solved it by adding a command -lwsock32.
you can add the command according follow steps:
tools
compiler options
choose general
click add the following commands when calling the compilers
then you can add the above command -lwsock32.
add
-lwsock32
to your command-line instead of #pragma when compiling with MinGW
g++ src/main.cpp -o release/myApp.exe -lwsock32
In DevC++, navigate to Project >> Project Options (or via usually ctrl+h); then in the "Parameters" tab there is a button "Add Library or Object" and then add libws2_32.a.
For codeblocks under wnidows : go to
Project -> Build Options -> Linker settings (make sure the project is selected on the left, not a build target) and add (type in) in the left list the library "ws2_32"