How to link libssh library in Makefile - c++

I get following error, when i tried to compile my test program
g++ main.cpp -o main
/tmp/ccICBwKO.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `ssh_new'
main.cpp:(.text+0x2d): undefined reference to `ssh_free'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
In my make file i have:
all: main
$main: main.cpp
g++ main.cpp -o main -lssh
Thanks for help. (I really hate C/C++...)

As you can see from the log, make is not using the rule you have given for $main. I suspect that you want to name it either main or $(main).
$main concatenates the value of variable $m (defaults to nothing as m is not defined) with ain, so that you've created a rule for ain instead of main. make thus rely on its default rules for creating an executable from a .cpp file, which does not include linking with libssh.

Related

Building multiple CPP files with mpic++ fails

I'm a newbie in using MPI, and I want to run my cpp project with mpi. I have a main cpp file which includes the main mpi stuff, and two other cpp files that are needed (and being called) in the main one.
In Ubuntu, I am doing:
mpic++ -o mainFile.cpp otherFile1.cpp otherFile2.cpp
But I am getting this error:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: in function `_start': (.text+0x24): undefined reference to `main' collect2: error: ld returned 1 exit status
I have mpic++ installed and I can run some other (one cpp file and very simple) projects, but not this one.
Could you please help?

How to link/include c++ libraries correctly

I tried to compile program with https://github.com/yhirose/cpp-httplib "one-header" library. I wrote
#include "httplib.h"
/* example code from main github page */
and when i tried to compile program
g++ -std=c++11 -o test test.cc
i got this error:
/usr/bin/ld: /tmp/ccYMj4l8.o: in function `std::thread::thread<httplib::ThreadPool::worker, , void>(httplib::ThreadPool::worker&&)':
test.cc:(.text._ZNSt6threadC2IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_[_ZNSt6threadC5IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_]+0x2f): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
What can i do?
And how to link libraries that have include and src directories, e.g. libcurl
It's gcc's known particular feature, its std::thread implementation is built upon pthreads so it requires specifying -pthread to correctly link programs with threads.

g++ error linking functions from static library

I have a Qt5 C++ program and I'm trying to link to a static library. The static library is senselock/libsenseEIV.a (relative to the main.cpp file). When I compile I see the output below:
^
g++ -Wl,-rpath,/opt/Qt/5.7/gcc_64/lib -o test1 main.o -Lsenselock/libsenseEIV.a -L/opt/Qt/5.7/gcc_64/lib -lQt5Core -lpthread
main.o: In function `test1()':
/test1/main.cpp:31: undefined reference to `S4Enum'
/test1/main.cpp:58: undefined reference to `S4Enum'
/test1/main.cpp:71: undefined reference to `S4Open'
/test1/main.cpp:83: undefined reference to `S4Control'
/test1/main.cpp:102: undefined reference to `S4Close'
collect2: error: ld returned 1 exit status
make: *** [Makefile:216: test1] Error 1
11:55:27: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project test1 (kit: Desktop Qt 5.7.1 GCC 64bit)
When executing step "Make"
and in my .pro file I have
LIBS += -Lsenselock/libsenseEIV.a
in case that matters. Can someone explain how to fix this error? The undefined reference errors all relate to functions located in that libsenseEIV.a library.
I don't understand if the compiler can't find the .a file or there's some other error.
UPDATE: I tried this syntax
LIBS += -Lsenselock -lsenseEIV
but it generates error
/usr/bin/ld: cannot find -lsenseEIV
Am I using the wrong library name? If so how would I find the name? (assuming this is compiled into the .a file)
This part of your command line is wrong:
-Lsenselock/libsenseEIV.a
Should be:
senselock/libsenseEIV.a
(The -Lfoo/bar.a tells linker to search directory foo/bar.a/ for libraries, which is not at all what you want.)
No -l prefix?
You can specify linking with libsenseEIV.a in the following (mostly equivalent) ways:
senselock/libsenseEIV.a
-Lsenselock -lsenseEIV
-Lsenselock -l:libsenseEIV.a
You're using the linker flags wrong, you should specify the library path after -L and the library name after -l. In other words you need to have both in the assignment to LIBS variable in your .pro file.
After all it turns out that you can use LIBS += -L$$PWD/senselock/ -lsenseEIV for your case

g++ undefined reference though all files included

I have the problem that as the g++ tries to link the object files I receive the following error:
11:29:13 **** Build of configuration Debug for project daytime ****
make all
'Building target: daytime'
'Invoking: Cross G++ Linker'
g++ -o "daytime" ./tcf/services/daytime.o ./tcf/main/main.o
./tcf/services/daytime.o: In function `command_get_time_of_day':
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:38: undefined reference to `json_read_string'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:40: undefined reference to `exception'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:43: undefined reference to `exception'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:52: undefined reference to `write_stringz'
makefile:46: recipe for target 'daytime' failed
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:54: undefined reference to `write_stringz'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:56: undefined reference to `write_errno'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:58: undefined reference to `json_write_string'
./tcf/services/daytime.o: In function `ini_daytime_service':
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:70: undefined reference to `add_command_handler'
collect2.exe: error: ld returned 1 exit status
make: *** [daytime] Error 1
I have no idea why this is the case since e.g. #include <tcf/framework/json.h>is included and found.
Didn't gcc compile the corresponding *.c files such that this linker error occurs?
What is the problem here?
Thank you.
It is not enough to include the header files; you also have to specify the libraries where those functions are defined.
To make the linker find all those methods/classes (json_read_string, write_stringz, exception) you need to reference the library. If e.g. they are contained in a library called libjson.so, you should do:
g++ -ljson -o "daytime" ./tcf/services/daytime.o ./tcf/main/main.o
(or add the library to the project options, if eclipse is managing your make files).
Or if it's another .o file, include that in the compilation (-> or in the project, if eclipse is creating the make file).

with -lpthread, g++ compiler error, "undefined reference to " semaphore calls such as `sem_open'

I am new to posix thread library, and I tried to compile a sample code from a tutorial with:
g++ -lpthread agreement.cpp -o agreement
however I was not able to compile the code and got the following error message:
a3q2.cpp:(.text+0x7e): undefined reference to `sem_open'
a3q2.cpp:(.text+0xab): undefined reference to `sem_wait'
a3q2.cpp:(.text+0x290): undefined reference to `sem_post'
a3q2.cpp:(.text+0x2af): undefined reference to `sem_close'
a3q2.cpp:(.text+0x2bb): undefined reference to `sem_unlink'
collect2: ld returned 1 exit status
make: *** [a3q2_exe] Error 1
I am aware that -lpthread is needed for compilation to work, but is there any other options i might need to solve the problem? if not how do I have to install the "proper" pthread library?
Thanks for your help!
You want the compile option -pthread (if you are really using pthreads). If you just need those functions they are in librt so use -lrt