libssh problems compile problem undefined errors [duplicate] - c++

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.

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;
}

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

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.

SDL2 Windows undefined reference to `WinMain#16' with Eclipse [duplicate]

This question already has answers here:
undefined reference to WinMain#16 C++, SDL-2
(2 answers)
undefined reference to `WinMain#16'
(7 answers)
Closed 2 years ago.
I have this snippet of code:
#include <SDL2/SDL.h>
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "!!!Hello World!!!" << std::endl;
return 0;
}
The error reported is this:
Info: Internal Builder is used for build g++ -o
cavestory-developement.exe "src\cavestory-developement.o" -lmingw32
-lSDL2main -lSDL2 c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(main.o):(.text.startup+0xc0):
undefined reference to `WinMain#16' collect2.exe: error: ld returned 1
exit status
17:13:45 Build Failed. 1 errors, 0 warnings. (took 205ms)
Any help solving this problem would be greatly appreciated.

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

Linking error with boost process (and other boost libs)

Here's my code. I'm just testing Boost::process so I'll be able to use it if/when I need to. I don't know why I'm getting the linking error that I am getting. I'm a rather novice C++ programmer. I know the concepts, but I make frequent errors in practice and am bad at debugging. I appreciate any help I can get with this.
#include<iostream>
#include<boost/process.hpp>
#include<boost/iostreams/device/file_descriptor.hpp>
namespace bp = ::boost::process;
namespace bpi = ::boost::process::initializers;
namespace bio = ::boost::iostreams;
int main(int argc, char *argv[])
{
bp::pipe p = bp::create_pipe();
bio::file_descriptor_sink sink(p.sink, bio::close_handle);
bp::execute(
bpi::run_exe("/usr/bin/ls"),
bpi::bind_stdout(sink)
);
return(0);
}
And here is my error…
/tmp/cc7cmrV8.o: In function `main':
test.cpp:(.text+0x2b): undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(int, boost::iostreams::file_descriptor_flags)'
/tmp/cc7cmrV8.o: In function `boost::process::posix::initializers::bind_stdout::bind_stdout(boost::iostreams::file_descriptor_sink const&)':
test.cpp:(.text._ZN5boost7process5posix12initializers11bind_stdoutC2ERKNS_9iostreams20file_descriptor_sinkE[_ZN5boost7process5posix12initializers11bind_stdoutC5ERKNS_9iostreams20file_descriptor_sinkE]+0x2b): undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(boost::iostreams::file_descriptor_sink const&)'
/tmp/cc7cmrV8.o: In function `void boost::process::posix::initializers::bind_stdout::on_exec_setup<boost::process::posix::executor>(boost::process::posix::executor&) const':
test.cpp:(.text._ZNK5boost7process5posix12initializers11bind_stdout13on_exec_setupINS1_8executorEEEvRT_[_ZNK5boost7process5posix12initializers11bind_stdout13on_exec_setupINS1_8executorEEEvRT_]+0x18): undefined reference to `boost::iostreams::file_descriptor::handle() const'
collect2: error: ld returned 1 exit status
Platform: Linux 64-bit
Boost: 1.55 (installed via pacman)
Boost::process: 0.5
Compile command: g++ -Wall test.cpp -o spegh.elf -lboost_system
A simple search threw me at -This-.
Seeing you posted your compile command, I'm guessing you are simply missing -lboost_iostreams in your linker settings.