/usr/bin/ld: cannot find during linking g++ - c++

This question has already been here so many times. But I didn't find the answer.
I have this .cpp file
#include <clickhouse/client.h>
#include <iostream>
using namespace clickhouse;
int main(){
/// Initialize client connection.
Client client(ClientOptions().SetHost("localhost"));
client.Select("SELECT l.a, l.b from table", [] (const Block& block)
{
for (size_t i = 0; i < block.GetRowCount(); ++i) {
std::cout << block[0]->As<ColumnUInt64>()->At(i) << " "
<< block[1]->As<ColumnString>()->At(i) << "\n";
}
}
);
return 0;
}
and I have instantiated SO library, like written here.
after that i got the following structure of /usr/local/lib directory:
~/$ ls /usr/local/lib
>>libclickhouse-cpp-lib-static.a libclickhouse-cpp-lib.so
in next step I trying execute compilation with g++
~/$ g++ run.cpp -std=c++17 -o result -llibclickhouse-cpp-lib -L/usr/local/lib
>>/usr/bin/ld: cannot find -llibclickhouse-cpp-lib
>>collect2: error: ld returned 1 exit status
I don't know what hinders create links.
thank You for Your help!

ld's manual page describes the -l option as follows (irrelevant details omitted):
-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of
files to link. [...] ld will search a directory for a library called
libnamespec.so
If you read this very carefully, you will reach the conclusion that -llibclickhouse-cpp-lib instructs ld to search for a library named liblibclickhouse-cpp-lib.so which, obviously, does not exist.
This should simply be -lclickhouse-cpp-lib.

in my case, the problem was solved when I change the Cmake version to 2.9

Try this:
g++ -std=c++11 -I./ -I./contrib -L./build/clickhouse/ -lclickhouse-cpp-lib-static -o demo demo.cpp ./build/clickhouse/libclickhouse-cpp-lib-static.a ./build/contrib/lz4/liblz4-lib.a ./build/contrib/cityhash/libcityhash-lib.a ./build/contrib/absl/libabsl-lib.a

Related

undefined reference to `sqlite3_open'. I out of ideas

Im sorry for the bad title.
I want to get started using sqlite with c++. So i downloaded the amalgamation from the site and compiled to get the .dll
gcc -shared sqlite3.c -o sqlite3.dll
I included the sqlite.h file in my project and the .dll file too. I compiled:
g++ prueba.cpp
and got this error message
C:\Users\PABLOS~1\AppData\Local\Temp\ccUI3YAt.o:prueba.cpp:(.text+0x2d): undefined reference to `sqlite3_open'
C:\Users\PABLOS~1\AppData\Local\Temp\ccUI3YAt.o:prueba.cpp:(.text+0x41): undefined reference to `sqlite3_errmsg'
collect2.exe: error: ld returned 1 exit status
Ok I said, lets see in stack overflow. In some question that I read they recomended to do this:
g++ main.cpp sqlite3.c
But the output was a really long list of error messages. I kept on reading but most of the questions where solved by:
sudo apt install libsqlite3-dev
or
gcc main.c -lsqlite3
In one of the questions the same guy that asked answered that he didnt include the .a file. So i googled about it and followed the instructions in this article. I created the .def file:
dlltool -z sqlite3.def --export-all-symbols sqlite3.dll
And created the .a file
dlltool -d sqlite3.def -l libsqlite3dll.a
Then included it in C:\MinGW\lib and tried again to compile
g++ prueba.cpp -lsqlite3dll
And i got the same error message. At this point im kind of lost (Im new to programing), and i dont know what to do next. Can you give me a pointer in the direction I should head in?
Edit: Answered a question form the coments
// This is my code
#include <iostream>
#include "sqlite3.h"
int main(int argc, char **argv) {
// Esto es lo que necesitamos para abrir la base de datos
sqlite3 *db;
char *zErrMsg = NULL;
int rc;
// La abrimos y revisamos por errores
rc = sqlite3_open("test.db", &db);
if (rc) {
std::cerr << "No se pudo abrir la base de datos: " << sqlite3_errmsg(db);
return 1;
}
std::cout << "Se pudo abrir la base de datos!"<< std::endl;
std::cin.get();
return 0;
}
Bitten by this issue again today when working with g++.
Ensure the following:
'lib' prefix is part of the static link library name "libsqlite3.lib" (my issue was that I was compiling sqlite3.def into sqlite3.lib and when passed to g++ using -l option it was failing to resolve function references during linking, the g++ compiler requires the library name to be prefixed with 'lib' as in libsqlite3.lib and associated g++ command should be of the form: g++ -lsqlite3 ...
Ensure Library search path is valid when using -L option
ex: g++ -Lc:\static_libs -lsqlite3 -o app.exe main.cpp.
In this case the library 'libsqlite3.lib' must be located as 'c:\static_libs\libsqlite3.lib'
Or specify full path to the lib file without using -l or -L options
ex: g++ -o app.exe main.cpp c:/static_export_libs/sqlite3.lib
In case you only have the precompiled shared library sqlite3.dll and 'sqlite3.def', you can simply create exports file libsqlite3.exp and its associated static link library libsqlite3.lib using following sample command:
lib.exe" /machine:x64 /def:sqlite3_x64/sqlite3.def /out:sqlite3_x64/libsqlite3.lib
lib.exe" /machine:x86 /def:sqlite3_x86/sqlite3.def /out:sqlite3_x86/libsqlite3.lib
Instead of:
gcc -shared sqlite3.c -o sqlite3.dll
run:
gcc -shared sqlite3.c -o sqlite3.dll -Wl,--out-implib,libsqlite3.dll.a
This will give you both a .dll binary and a .dll.a library file.
Then instead of:
g++ prueba.cpp
run:
g++ -shared -o prueba.exe prueba.cpp -lsqlite3
If your .exe and .dll are in the same folder you should be able to run the .exe.

Linking static C++ archive file in Hello World app

I'm trying to link an archive file into my simple hello world app, to make sure I understand the process. However, I obviously don't, because the library isn't linking correctly.
Here is my simple app (hello.cc):
#include <iostream>
#include "firebase/app.h"
int main()
{
std::cout << "Hello, world!\n";
std::cout << std::endl;
return 0;
}
And here is my compilation command with linking:
gcc hello.cc -L /tmp -l app -o hello -lstdc++
I've moved my archive file (libapp.a) to /tmp.
My understanding is that -L <dir> adds directories to search for lib*.a files and -l <name> indicates the name of the archive files, in the form of lib<name>.a. I know it isn't linking the archive file, because I get this error:
hello.cc:3:26: fatal error: firebase/app.h: No such file or directory
#include "firebase/app.h"
However, I also don't think the compilation command does what I think it does, because I renamed libapp.a to libapp.a2 and the same error was returned, not something indicating that the archive file was missing.
Can somebody help me with a) the command to link the library file /tmp/libapp.a and b) explain what I'm doing wrong?
EDIT
I forgot to include the headers, which caused the initial error I think. Now that I'm including the headers, with the following:
gcc hello.cc -L /tmp -l app -I /tmp/firebase/include -o hello -lstdc++
I get this new error:
/usr/bin/ld: cannot find -lapp
collect2: error: ld returned 1 exit status
So, I think I'm not using -L and -l correctly, but not sure what exactly I'm doing wrong.
EDIT2
Fixed. Forgot I had renamed the lib file while trying to figure out the first issue. Once I named it back to libapp.a the app worked as expected.

/usr/bin/ld: cannot find -llibboost

Alright So right now I am attempting use the boost C++ libraries in Linux (Ubuntu 12.04) as I have previously used them in Windows. So using some example code from the Boost's site
testfile.cpp
#include <boost/filesystem/convenience.hpp>
#include <boost/foreach.hpp>
#include <boost/range.hpp>
#include <iostream>
int main(int, char**)
{
namespace bf = boost::filesystem;
BOOST_FOREACH(bf::path path,
boost::make_iterator_range(
bf::recursive_directory_iterator(bf::path("/home")),
bf::recursive_directory_iterator())) {
std::cout << path.string() << std::endl;
}
return 0;
}
Should very easily compile using this command
g++ -L/usr/local/lib -o "testfile" -llibboost_filesystem
My problem I am getting the linker error
/usr/bin/ld: cannot find -llibboost_filesystem
and cannot seem to figure out what I am missing. Please Help.
By convention, library names use the lib prefix on most Linux distributions. You should remove this prefix when instructing the linker which libraries to search for. Assuming the gnu ld linker, the documentation says
-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of files to
link. This option may be used any number of times. If namespec is of the
form :filename, ld will search the library path for a file called filename,
otherwise it will search the library path for a file called libnamespec.a.
so you either want
g++ -L/usr/local/lib -o "testfile" -lboost_filesystem
or
g++ -L/usr/local/lib -o "testfile" -l :libboost_filesystem.so

g++ encounters a fatal error linking to crt1.o in function __start

I have been trying to compile and link Qt5, which means I have been messing with some ldconfig and include path defaults that I don't completely understand. I will do my best to limit my question to a very specific one, as my priority is fixing my compiler.
I have used a command to try
`gcc -print-prog-name=cc1plus` -v
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.7
/usr/include/c++/4.7/x86_64-linux-gnu
/usr/include/c++/4.7/backward
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include
End of search list.
There is a problematic line at the beginning, about ignoring a non-existent directory when searching for the header. Unfortunately, this is exactly where g++ thinks it's a good idea to look for crt1.o, which I assume is some sort of binary that morphs the main() function into an executable:
g++ -Isrc --std=c++11 -g -c -o src/tissuecell.o src/tissuecell.cpp
g++ -Isrc --std=c++11 -g -c -o src/analyze.o src/analyze.cpp
g++ -o TissueCells src/tissuecell.o src/analyze.o
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
My compiler has not done this until a few hours ago (previously, it would compile!), and what comes to mind in terms of "irreversible commands" that I typed was a single:
sudo ldconfig
Can anyone get me out of this terrible pit of linking and compilation problems, and remove this pesky line from my include path?
Some questions that you might also be able to answer that would make me very happy:
How do I view & edit the paths that my compiler searches for includes? (Not -I)
How do I view & edit the paths that my compiler searches for libraries? (Not -L, and ld should be able to find the libraries as it needs them)
What relation does ld have to this whole process, and is there a way to pass ld some flags through my g++ call (for example, -rpath)?
Oh gosh, this is embarrassing. My problem was that I did not fix my Makefile after giving up on the new build; the error was coming from a lack of main() function in my codebase.
The following command allowed me to test my compiler for internal errors:
echo "int main() { return 0; }" > /tmp/test.c; gcc -v /tmp/test.c
I pulled back to an old commit on my git, and got my code to compile, which served as a good double-check on my compiler's sanity.
First of all you can run gcc(g++) with "-v" option, for example:
echo "int main() { return 0; }" > /tmp/test.c
gcc -v /tmp/test.c
You find in ouput the things like:
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4
plus
#include <...> search starts here:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include-fixed
/usr/include
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/:/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../:/lib/:/usr/lib/
and many other stuff.
The second variant you can use "strace -f" in front of your command, grep ouput with pattern open to find out where and what file is used.

Compiling parts of Festival code written in C++ in a stand-alone C++ program

I am trying to use selective parts of the Festival code (written in C++) and trying to use them in my own C++ programs. Note that this question is not about using the Festival API but about functions within Festival that can be used directly.
The program I wrote takes in a C++ style string and tries to initialize an object of type EST_String (an internal implementation of the String class in Festival). I then try to print the object.
The code I have:
/*EST_String is a string implementation for the festival project.
* This program simply takes in a C++-style string and returns an EST_String.
* This program is being used to test for makefiles etc.
*/
#include <iostream>
#include <EST_String.h>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[]) {
if(argc != 2) {
cout << "Correct usage: <binary> <string>" << endl;
exit(5);
}
string word(argv[1]);
EST_String e(word.c_str()); //init EST_String.
cout << "C++ String = " << word << endl;
cout << "EST_String = ";
cout << e;
return 0;
}
I am trying to compile it (from the command line directly (no makefile at present)) like so:
g++ -I../../speech_tools/include -I../../speech_tools/base_class/string -L../../speech_tools/lib/ -lestbase -lncurses -lasound -leststring -lestools usingESTString.C -o usingESTString
The error I get is:
/tmp/cczyGTfm.o: In function `main':
usingESTString.C:(.text+0x91): undefined reference to `EST_String::EST_String(char const*)'
/tmp/cczyGTfm.o: In function `EST_Chunk::operator--()':
usingESTString.C:(.text._ZN9EST_ChunkmmEv[EST_Chunk::operator--()]+0x3e): undefined reference to `EST_Chunk::~EST_Chunk()'
usingESTString.C:(.text._ZN9EST_ChunkmmEv[EST_Chunk::operator--()]+0x49): undefined reference to `EST_Chunk::operator delete(void*)'
collect2: ld returned 1 exit status
How can I get the code to compile properly? Where am I going wrong?
Try putting the libraries you link with last on the line.
The linker often resolves references kind of "backwards", meaning that the order of files presented on the command line is important: It wants files containing references first, then the libraries containing those references.
Try adding this to end of your g++ link command: -I/usr/include/festival
-I/usr/include/speech_tools -I/usr/include/boost -lFestival -lestools
-lestbase -leststring
Making sure that the festival and speech_tools headers directory lives at: /usr/include
cd /usr/include
ls festival
ls speech_tools
I am trying to rebuild cogita with festival support, and my program linked successfully after compiling the object files using this line
g++ -Wall -fPIC -Wno-variadic-macros -fopenmp -std=gnu++0x -O2 -g -fstack-protector cogitaconfig.o go-irc.o irc.o whirr-sockets.o -o cogIRCProgram
-rdynamic /usr/local/lib/libcogutil.so -Wl,-rpath,/usr/local/lib
-I/usr/include/festival -I/usr/include/speech_tools -I/usr/include/boost
-lFestival -lestools -lestbase -leststring
I've been trying to link to festival's API too, and the Makefile I wrote executes the following link command
g++ -lpthread build/fetch/festival/src/lib/libFestival.a build/fetch/speech_tools/lib/libestools.a build/fetch/speech_tools/lib/libestbase.a build/fetch/speech_tools/lib/libeststring.a -lcurses -ldl -lm -lstdc++ build/test/speaker.o build/common/message-queue.o build/speaker/speaker-public.o build/fetch/festival/src/lib/libFestival.a -o build/bin/speaker-test
and I get a huge (25k lines) linker error full of undefined references (a part of which is here: http://pastebin.com/PCyV8xAH). I can assert that the *.a files exist (though I'm not sure if they've been built correctly or not). I compile speech_tools with make -j7 and festival with make.
Any suggestions?
I'm running Debian wheezy.